2021-02-04 19:15:23 +01:00
|
|
|
import { createAsyncMiddleware, mergeMiddleware } from 'json-rpc-engine';
|
|
|
|
import createFetchMiddleware from 'eth-json-rpc-middleware/fetch';
|
|
|
|
import createBlockRefRewriteMiddleware from 'eth-json-rpc-middleware/block-ref-rewrite';
|
|
|
|
import createBlockCacheMiddleware from 'eth-json-rpc-middleware/block-cache';
|
|
|
|
import createInflightMiddleware from 'eth-json-rpc-middleware/inflight-cache';
|
|
|
|
import createBlockTrackerInspectorMiddleware from 'eth-json-rpc-middleware/block-tracker-inspector';
|
|
|
|
import providerFromMiddleware from 'eth-json-rpc-middleware/providerFromMiddleware';
|
2021-03-26 18:03:44 +01:00
|
|
|
import { PollingBlockTracker } from 'eth-block-tracker';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { SECOND } from '../../../../shared/constants/time';
|
2018-05-17 05:46:34 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const inTest = process.env.IN_TEST === 'true';
|
2021-06-10 21:27:03 +02:00
|
|
|
const blockTrackerOpts = inTest ? { pollingInterval: SECOND } : {};
|
2020-10-12 21:05:40 +02:00
|
|
|
const getTestMiddlewares = () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return inTest ? [createEstimateGasDelayTestMiddleware()] : [];
|
|
|
|
};
|
2020-10-12 21:05:40 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function createJsonRpcClient({ rpcUrl, chainId }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchMiddleware = createFetchMiddleware({ rpcUrl });
|
|
|
|
const blockProvider = providerFromMiddleware(fetchMiddleware);
|
2021-03-26 18:03:44 +01:00
|
|
|
const blockTracker = new PollingBlockTracker({
|
2020-10-12 21:05:40 +02:00
|
|
|
...blockTrackerOpts,
|
|
|
|
provider: blockProvider,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-05-17 05:46:34 +02:00
|
|
|
|
|
|
|
const networkMiddleware = mergeMiddleware([
|
2020-10-12 21:05:40 +02:00
|
|
|
...getTestMiddlewares(),
|
2020-10-06 19:57:02 +02:00
|
|
|
createChainIdMiddleware(chainId),
|
2018-10-08 18:39:18 +02:00
|
|
|
createBlockRefRewriteMiddleware({ blockTracker }),
|
2018-05-17 05:46:34 +02:00
|
|
|
createBlockCacheMiddleware({ blockTracker }),
|
|
|
|
createInflightMiddleware(),
|
|
|
|
createBlockTrackerInspectorMiddleware({ blockTracker }),
|
|
|
|
fetchMiddleware,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-12 21:05:40 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return { networkMiddleware, blockTracker };
|
2018-05-17 05:46:34 +02:00
|
|
|
}
|
2020-10-06 19:57:02 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function createChainIdMiddleware(chainId) {
|
2020-10-06 19:57:02 +02:00
|
|
|
return (req, res, next, end) => {
|
|
|
|
if (req.method === 'eth_chainId') {
|
2021-02-04 19:15:23 +01:00
|
|
|
res.result = chainId;
|
|
|
|
return end();
|
2020-10-06 19:57:02 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return next();
|
|
|
|
};
|
2020-10-06 19:57:02 +02:00
|
|
|
}
|
2020-10-12 21:05:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For use in tests only.
|
|
|
|
* Adds a delay to `eth_estimateGas` calls.
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function createEstimateGasDelayTestMiddleware() {
|
2020-10-12 21:05:40 +02:00
|
|
|
return createAsyncMiddleware(async (req, _, next) => {
|
|
|
|
if (req.method === 'eth_estimateGas') {
|
2021-06-10 21:27:03 +02:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, SECOND * 2));
|
2020-10-12 21:05:40 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return next();
|
|
|
|
});
|
2020-10-12 21:05:40 +02:00
|
|
|
}
|