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'
|
2020-07-03 20:10:58 +02:00
|
|
|
import * as networkEnums from './enums'
|
2018-05-17 05:46:34 +02:00
|
|
|
|
2020-09-10 18:16:00 +02:00
|
|
|
export default function createInfuraClient ({ network, projectId }) {
|
|
|
|
const infuraMiddleware = createInfuraMiddleware({
|
|
|
|
network,
|
|
|
|
projectId,
|
|
|
|
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':
|
2020-07-08 23:05:09 +02:00
|
|
|
netId = networkEnums.MAINNET_NETWORK_ID
|
2018-11-05 20:13:37 +01:00
|
|
|
chainId = '0x01'
|
|
|
|
break
|
|
|
|
case 'ropsten':
|
2020-07-08 23:05:09 +02:00
|
|
|
netId = networkEnums.ROPSTEN_NETWORK_ID
|
2018-11-05 20:13:37 +01:00
|
|
|
chainId = '0x03'
|
|
|
|
break
|
|
|
|
case 'rinkeby':
|
2020-07-08 23:05:09 +02:00
|
|
|
netId = networkEnums.RINKEBY_NETWORK_ID
|
2018-11-05 20:13:37 +01:00
|
|
|
chainId = '0x04'
|
|
|
|
break
|
|
|
|
case 'kovan':
|
2020-07-08 23:05:09 +02:00
|
|
|
netId = networkEnums.KOVAN_NETWORK_ID
|
2020-07-03 20:10:58 +02:00
|
|
|
chainId = networkEnums.KOVAN_CHAIN_ID
|
2018-11-05 20:13:37 +01:00
|
|
|
break
|
2019-04-17 19:34:49 +02:00
|
|
|
case 'goerli':
|
2020-07-08 23:05:09 +02:00
|
|
|
netId = networkEnums.GOERLI_NETWORK_ID
|
2019-04-17 19:34:49 +02:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|