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';
|
|
|
|
import BlockTracker from 'eth-block-tracker';
|
2018-05-17 05:46:34 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const inTest = process.env.IN_TEST === 'true';
|
|
|
|
const blockTrackerOpts = inTest ? { pollingInterval: 1000 } : {};
|
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);
|
2020-10-12 21:05:40 +02:00
|
|
|
const blockTracker = new BlockTracker({
|
|
|
|
...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-02-04 19:15:23 +01:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
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
|
|
|
}
|