1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/lib/middleware/pending.js
Mark Stacey 87ce653c86
Move MetaMask middleware out of network controller (#16863)
The "MetaMask middleware" is the set of middleware for handling methods that
we don't send to the network. This includes signing, encryption, `getAccounts`,
and methods that rely on pending transaction state.

Previously this middleware was setup as part of the network client, despite
having nothing to do with the network. They have been moved into the main RPC
pipeline established in `metamask-controller.js` instead.

This is a pure refactor, and should have no functional changes. The middleware
are still run in exactly the same order with the same arguments.
2023-01-06 13:44:50 -03:30

36 lines
965 B
JavaScript

import { createAsyncMiddleware } from 'json-rpc-engine';
import { formatTxMetaForRpcResult } from '../util';
export function createPendingNonceMiddleware({ getPendingNonce }) {
return createAsyncMiddleware(async (req, res, next) => {
const { method, params } = req;
if (method !== 'eth_getTransactionCount') {
next();
return;
}
const [param, blockRef] = params;
if (blockRef !== 'pending') {
next();
return;
}
res.result = await getPendingNonce(param);
});
}
export function createPendingTxMiddleware({ getPendingTransactionByHash }) {
return createAsyncMiddleware(async (req, res, next) => {
const { method, params } = req;
if (method !== 'eth_getTransactionByHash') {
next();
return;
}
const [hash] = params;
const txMeta = getPendingTransactionByHash(hash);
if (!txMeta) {
next();
return;
}
res.result = formatTxMetaForRpcResult(txMeta);
});
}