2020-12-02 20:41:24 +01:00
|
|
|
import { createAsyncMiddleware, mergeMiddleware } from 'json-rpc-engine'
|
2020-01-09 04:34:58 +01:00
|
|
|
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
|
|
|
|
2020-10-12 21:05:40 +02:00
|
|
|
const inTest = process.env.IN_TEST === 'true'
|
2020-11-03 00:41:28 +01:00
|
|
|
const blockTrackerOpts = inTest ? { pollingInterval: 1000 } : {}
|
2020-10-12 21:05:40 +02:00
|
|
|
const getTestMiddlewares = () => {
|
2020-11-03 00:41:28 +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 }) {
|
2018-05-17 05:46:34 +02:00
|
|
|
const fetchMiddleware = createFetchMiddleware({ rpcUrl })
|
|
|
|
const blockProvider = providerFromMiddleware(fetchMiddleware)
|
2020-10-12 21:05:40 +02:00
|
|
|
const blockTracker = new BlockTracker({
|
|
|
|
...blockTrackerOpts,
|
|
|
|
provider: blockProvider,
|
|
|
|
})
|
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,
|
|
|
|
])
|
2020-10-12 21:05:40 +02:00
|
|
|
|
2018-05-17 05:46:34 +02:00
|
|
|
return { networkMiddleware, blockTracker }
|
|
|
|
}
|
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') {
|
|
|
|
res.result = chainId
|
|
|
|
return end()
|
|
|
|
}
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
}
|
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') {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
|
|
}
|
|
|
|
return next()
|
|
|
|
})
|
|
|
|
}
|