-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,790 additions
and
1,094 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/network-controller/src/clients/createInfuraClient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { | ||
createScaffoldMiddleware, | ||
JsonRpcMiddleware, | ||
mergeMiddleware, | ||
} from 'json-rpc-engine'; | ||
import { | ||
createBlockRefMiddleware, | ||
createRetryOnEmptyMiddleware, | ||
createBlockCacheMiddleware, | ||
createInflightCacheMiddleware, | ||
createBlockTrackerInspectorMiddleware, | ||
} from '@metamask/eth-json-rpc-middleware'; | ||
import { providerFromMiddleware } from '@metamask/eth-json-rpc-provider'; | ||
|
||
import { NetworksChainId } from '@metamask/controller-utils'; | ||
|
||
import { createInfuraMiddleware } from '@metamask/eth-json-rpc-infura'; | ||
import { PollingBlockTracker } from 'eth-block-tracker'; | ||
|
||
// could improve this type... this is not a very complete list | ||
export type InfuraNetworkType = 'mainnet' | 'goerli' | 'sepolia'; | ||
|
||
/** | ||
* Construct middleware to manage calls to infura. | ||
* | ||
* @param network - type of network | ||
* @param projectId - infura project id (api key) | ||
* @returns middleware and a blockTracker | ||
*/ | ||
export function createInfuraClient( | ||
network: InfuraNetworkType, | ||
projectId: string, | ||
) { | ||
const infuraMiddleware = createInfuraMiddleware({ | ||
network, | ||
projectId, | ||
maxAttempts: 5, | ||
source: 'metamask', | ||
}); | ||
const infuraProvider = providerFromMiddleware(infuraMiddleware); | ||
const blockTracker = new PollingBlockTracker({ provider: infuraProvider }); | ||
|
||
const networkMiddleware = mergeMiddleware([ | ||
createNetworkAndChainIdMiddleware(network), | ||
createBlockCacheMiddleware({ blockTracker }), | ||
createInflightCacheMiddleware(), | ||
createBlockRefMiddleware({ blockTracker, provider: infuraProvider }), | ||
createRetryOnEmptyMiddleware({ blockTracker, provider: infuraProvider }), | ||
createBlockTrackerInspectorMiddleware({ blockTracker }), | ||
infuraMiddleware, | ||
]); | ||
return { networkMiddleware, blockTracker }; | ||
} | ||
|
||
/** | ||
* Create middleware to implement static / scafolded methods. | ||
* | ||
* @param network - type of network | ||
* @returns the middleware | ||
*/ | ||
function createNetworkAndChainIdMiddleware( | ||
network: InfuraNetworkType, | ||
): JsonRpcMiddleware<unknown, unknown> { | ||
const chainId = NetworksChainId[network]; | ||
|
||
if (typeof chainId === undefined) { | ||
throw new Error(`createInfuraClient - unknown network "${network}"`); | ||
} | ||
|
||
return createScaffoldMiddleware({ | ||
eth_chainId: `0x${parseInt(chainId, 10).toString(16)}`, | ||
net_version: chainId, | ||
}); | ||
} |
91 changes: 91 additions & 0 deletions
91
packages/network-controller/src/clients/createJsonRpcClient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
createAsyncMiddleware, | ||
JsonRpcMiddleware, | ||
mergeMiddleware, | ||
} from 'json-rpc-engine'; | ||
import { | ||
createFetchMiddleware, | ||
createBlockRefRewriteMiddleware, | ||
createBlockCacheMiddleware, | ||
createInflightCacheMiddleware, | ||
createBlockTrackerInspectorMiddleware, | ||
} from '@metamask/eth-json-rpc-middleware'; | ||
import { PollingBlockTracker } from 'eth-block-tracker'; | ||
import { providerFromMiddleware } from '@metamask/eth-json-rpc-provider'; | ||
import { CreateClientResult } from './types'; | ||
|
||
const SECOND = 1000; | ||
|
||
/** | ||
* Create client middleware for a custom rpc endpoint. | ||
* | ||
* @param rpcUrl - url of the rpc endpoint. | ||
* @param chainId - the chain id for the rpc endpoint. This value will always be returned by eth_chainId. | ||
* @returns The network middleware and the block tracker. | ||
*/ | ||
export function createJsonRpcClient( | ||
rpcUrl: string, | ||
chainId?: string, | ||
): CreateClientResult { | ||
const blockTrackerOpts = process.env.IN_TEST // eslint-disable-line node/no-process-env | ||
? { pollingInterval: SECOND } | ||
: {}; | ||
const fetchMiddleware = createFetchMiddleware({ | ||
rpcUrl, | ||
fetch, | ||
btoa, | ||
}); | ||
const blockProvider = providerFromMiddleware(fetchMiddleware); | ||
const blockTracker = new PollingBlockTracker({ | ||
...blockTrackerOpts, | ||
provider: blockProvider, | ||
}); | ||
const testMiddlewares = process.env.IN_TEST // eslint-disable-line node/no-process-env | ||
? [createEstimateGasDelayTestMiddleware()] | ||
: []; | ||
|
||
const networkMiddleware = mergeMiddleware([ | ||
...testMiddlewares, | ||
createChainIdMiddleware(chainId), | ||
createBlockRefRewriteMiddleware({ blockTracker }), | ||
createBlockCacheMiddleware({ blockTracker }), | ||
createInflightCacheMiddleware(), | ||
createBlockTrackerInspectorMiddleware({ blockTracker }), | ||
fetchMiddleware, | ||
]); | ||
|
||
return { networkMiddleware, blockTracker }; | ||
} | ||
|
||
/** | ||
* Create middleware to catch calls to eth_chainId. | ||
* | ||
* @param chainId - the chain id to use as the response | ||
* @returns the middleware | ||
*/ | ||
function createChainIdMiddleware( | ||
chainId?: string, | ||
): JsonRpcMiddleware<unknown, unknown> { | ||
return (req, res, next, end) => { | ||
if (req.method === 'eth_chainId') { | ||
res.result = chainId; | ||
return end(); | ||
} | ||
return next(); | ||
}; | ||
} | ||
|
||
/** | ||
* For use in tests only. | ||
* Adds a delay to `eth_estimateGas` calls. | ||
* | ||
* @returns the middleware implementing estimate gas | ||
*/ | ||
function createEstimateGasDelayTestMiddleware() { | ||
return createAsyncMiddleware(async (req, _, next) => { | ||
if (req.method === 'eth_estimateGas') { | ||
await new Promise((resolve) => setTimeout(resolve, SECOND * 2)); | ||
} | ||
return next(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { PollingBlockTracker } from 'eth-block-tracker'; | ||
import { JsonRpcMiddleware } from 'json-rpc-engine'; | ||
|
||
export type CreateClientResult = { | ||
networkMiddleware: JsonRpcMiddleware<any, any>; | ||
blockTracker: PollingBlockTracker; | ||
}; |
Oops, something went wrong.