1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 08:09:15 +01:00
metamask-extension/app/scripts/controllers/network/createInfuraClient.js

47 lines
1.9 KiB
JavaScript
Raw Normal View History

import { createScaffoldMiddleware, mergeMiddleware } from 'json-rpc-engine';
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-12-07 20:39:39 +01:00
import { NETWORK_TYPE_TO_ID_MAP } from '../../../../shared/constants/network';
2020-11-03 00:41:28 +01:00
export default function createInfuraClient({ network, projectId }) {
const infuraMiddleware = createInfuraMiddleware({
network,
projectId,
maxAttempts: 5,
source: 'metamask',
});
const infuraProvider = providerFromMiddleware(infuraMiddleware);
const blockTracker = new BlockTracker({ provider: infuraProvider });
const networkMiddleware = mergeMiddleware([
createNetworkAndChainIdMiddleware({ network }),
createBlockCacheMiddleware({ blockTracker }),
createInflightMiddleware(),
createBlockReRefMiddleware({ blockTracker, provider: infuraProvider }),
createRetryOnEmptyMiddleware({ blockTracker, provider: infuraProvider }),
createBlockTrackerInspectorMiddleware({ blockTracker }),
infuraMiddleware,
]);
return { networkMiddleware, blockTracker };
}
2020-11-03 00:41:28 +01:00
function createNetworkAndChainIdMiddleware({ network }) {
2020-12-07 20:39:39 +01:00
if (!NETWORK_TYPE_TO_ID_MAP[network]) {
throw new Error(`createInfuraClient - unknown network "${network}"`);
}
const { chainId, networkId } = NETWORK_TYPE_TO_ID_MAP[network];
2020-12-07 20:39:39 +01:00
return createScaffoldMiddleware({
eth_chainId: chainId,
2020-12-07 20:39:39 +01:00
net_version: networkId,
});
}