2021-02-04 19:15:23 +01:00
|
|
|
import { createAsyncMiddleware } from 'json-rpc-engine';
|
|
|
|
import { formatTxMetaForRpcResult } from '../util';
|
2019-10-30 23:15:54 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function createPendingNonceMiddleware({ getPendingNonce }) {
|
2019-10-30 23:15:54 +01:00
|
|
|
return createAsyncMiddleware(async (req, res, next) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { method, params } = req;
|
2019-11-20 01:03:20 +01:00
|
|
|
if (method !== 'eth_getTransactionCount') {
|
2021-02-04 19:15:23 +01:00
|
|
|
next();
|
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const [param, blockRef] = params;
|
2019-11-20 01:03:20 +01:00
|
|
|
if (blockRef !== 'pending') {
|
2021-02-04 19:15:23 +01:00
|
|
|
next();
|
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
res.result = await getPendingNonce(param);
|
|
|
|
});
|
2019-10-30 23:15:54 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function createPendingTxMiddleware({ getPendingTransactionByHash }) {
|
2019-10-30 23:15:54 +01:00
|
|
|
return createAsyncMiddleware(async (req, res, next) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { method, params } = req;
|
2019-11-20 01:03:20 +01:00
|
|
|
if (method !== 'eth_getTransactionByHash') {
|
2021-02-04 19:15:23 +01:00
|
|
|
next();
|
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const [hash] = params;
|
|
|
|
const txMeta = getPendingTransactionByHash(hash);
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!txMeta) {
|
2021-02-04 19:15:23 +01:00
|
|
|
next();
|
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
res.result = formatTxMetaForRpcResult(txMeta);
|
|
|
|
});
|
2019-10-30 23:15:54 +01:00
|
|
|
}
|