2020-01-09 04:34:58 +01:00
|
|
|
import mergeMiddleware from 'json-rpc-engine/src/mergeMiddleware'
|
|
|
|
import createScaffoldMiddleware from 'json-rpc-engine/src/createScaffoldMiddleware'
|
|
|
|
import createBlockReRefMiddleware from 'eth-json-rpc-middleware/block-ref'
|
|
|
|
import createRetryOnEmptyMiddleware from 'eth-json-rpc-middleware/retryOnEmpty'
|
|
|
|
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 createInfuraMiddleware from 'eth-json-rpc-infura'
|
|
|
|
import BlockTracker from 'eth-block-tracker'
|
2018-05-17 05:46:34 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default createInfuraClient
|
2018-05-17 05:46:34 +02:00
|
|
|
|
2019-09-23 11:54:41 +02:00
|
|
|
function createInfuraClient ({ network, onRequest }) {
|
|
|
|
const infuraMiddleware = mergeMiddleware([
|
|
|
|
createRequestHookMiddleware(onRequest),
|
|
|
|
createInfuraMiddleware({ network, maxAttempts: 5, source: 'metamask' }),
|
|
|
|
])
|
2018-10-08 17:55:07 +02:00
|
|
|
const infuraProvider = providerFromMiddleware(infuraMiddleware)
|
2019-05-03 19:32:05 +02:00
|
|
|
const blockTracker = new BlockTracker({ provider: infuraProvider })
|
2018-05-17 05:46:34 +02:00
|
|
|
|
|
|
|
const networkMiddleware = mergeMiddleware([
|
2018-11-05 20:13:37 +01:00
|
|
|
createNetworkAndChainIdMiddleware({ network }),
|
2018-05-17 05:46:34 +02:00
|
|
|
createBlockCacheMiddleware({ blockTracker }),
|
|
|
|
createInflightMiddleware(),
|
2018-10-08 17:55:07 +02:00
|
|
|
createBlockReRefMiddleware({ blockTracker, provider: infuraProvider }),
|
|
|
|
createRetryOnEmptyMiddleware({ blockTracker, provider: infuraProvider }),
|
2018-05-17 05:46:34 +02:00
|
|
|
createBlockTrackerInspectorMiddleware({ blockTracker }),
|
|
|
|
infuraMiddleware,
|
|
|
|
])
|
|
|
|
return { networkMiddleware, blockTracker }
|
|
|
|
}
|
2018-11-05 20:13:37 +01:00
|
|
|
|
2018-11-17 01:54:55 +01:00
|
|
|
function createNetworkAndChainIdMiddleware ({ network }) {
|
2018-11-05 20:13:37 +01:00
|
|
|
let chainId
|
|
|
|
let netId
|
|
|
|
|
|
|
|
switch (network) {
|
|
|
|
case 'mainnet':
|
|
|
|
netId = '1'
|
|
|
|
chainId = '0x01'
|
|
|
|
break
|
|
|
|
case 'ropsten':
|
|
|
|
netId = '3'
|
|
|
|
chainId = '0x03'
|
|
|
|
break
|
|
|
|
case 'rinkeby':
|
|
|
|
netId = '4'
|
|
|
|
chainId = '0x04'
|
|
|
|
break
|
|
|
|
case 'kovan':
|
|
|
|
netId = '42'
|
|
|
|
chainId = '0x2a'
|
|
|
|
break
|
2019-04-17 19:34:49 +02:00
|
|
|
case 'goerli':
|
|
|
|
netId = '5'
|
|
|
|
chainId = '0x05'
|
|
|
|
break
|
2018-11-05 20:13:37 +01:00
|
|
|
default:
|
|
|
|
throw new Error(`createInfuraClient - unknown network "${network}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return createScaffoldMiddleware({
|
|
|
|
eth_chainId: chainId,
|
|
|
|
net_version: netId,
|
|
|
|
})
|
|
|
|
}
|
2019-09-23 11:54:41 +02:00
|
|
|
|
|
|
|
function createRequestHookMiddleware (onRequest) {
|
|
|
|
return (req, _, next) => {
|
|
|
|
onRequest(req)
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|