2018-05-17 05:46:34 +02:00
|
|
|
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware')
|
2018-11-05 20:13:37 +01:00
|
|
|
const createScaffoldMiddleware = require('json-rpc-engine/src/createScaffoldMiddleware')
|
2018-10-08 17:55:07 +02:00
|
|
|
const createBlockReRefMiddleware = require('eth-json-rpc-middleware/block-ref')
|
|
|
|
const createRetryOnEmptyMiddleware = require('eth-json-rpc-middleware/retryOnEmpty')
|
2018-05-17 06:08:19 +02:00
|
|
|
const createBlockCacheMiddleware = require('eth-json-rpc-middleware/block-cache')
|
2018-05-17 07:46:11 +02:00
|
|
|
const createInflightMiddleware = require('eth-json-rpc-middleware/inflight-cache')
|
2018-06-12 20:12:32 +02:00
|
|
|
const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector')
|
2018-05-17 06:08:19 +02:00
|
|
|
const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware')
|
|
|
|
const createInfuraMiddleware = require('eth-json-rpc-infura')
|
2019-02-20 01:42:08 +01:00
|
|
|
const createBlockTracker = require('./createBlockTracker')
|
2018-05-17 05:46:34 +02:00
|
|
|
|
|
|
|
module.exports = createInfuraClient
|
|
|
|
|
2019-02-20 01:42:08 +01:00
|
|
|
function createInfuraClient ({ network, platform }) {
|
2019-01-25 01:56:55 +01:00
|
|
|
const infuraMiddleware = createInfuraMiddleware({ network, maxAttempts: 5, source: 'metamask' })
|
2018-10-08 17:55:07 +02:00
|
|
|
const infuraProvider = providerFromMiddleware(infuraMiddleware)
|
2019-02-20 01:42:08 +01:00
|
|
|
const blockTracker = createBlockTracker({ provider: infuraProvider }, platform)
|
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,
|
|
|
|
})
|
|
|
|
}
|