parent
708422432c
commit
088d7930e0
@ -0,0 +1,34 @@ |
||||
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') |
||||
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware') |
||||
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-ref') |
||||
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-cache') |
||||
const BlockTracker = require('eth-block-tracker') |
||||
|
||||
module.exports = createInfuraClient |
||||
|
||||
function createInfuraClient({ network }) { |
||||
const infuraMiddleware = createInfuraMiddleware({ network }) |
||||
const blockProvider = providerFromMiddleware(infuraMiddleware) |
||||
const blockTracker = new BlockTracker({ provider: blockProvider }) |
||||
|
||||
const networkMiddleware = mergeMiddleware([ |
||||
createBlockRefMiddleware({ blockTracker }), |
||||
createBlockCacheMiddleware({ blockTracker }), |
||||
createInflightMiddleware(), |
||||
createBlockTrackerInspectorMiddleware({ blockTracker }), |
||||
infuraMiddleware, |
||||
]) |
||||
return { networkMiddleware, blockTracker } |
||||
} |
||||
|
||||
// inspect if response contains a block ref higher than our latest block
|
||||
const futureBlockRefRequests = ['eth_getTransactionByHash', 'eth_getTransactionReceipt'] |
||||
function createBlockTrackerInspectorMiddleware ({ blockTracker }) { |
||||
return createAsyncMiddleware(async (req, res, next) => { |
||||
if (!futureBlockRefRequests.includes(req.method)) return next() |
||||
await next() |
||||
const blockNumber = Number.parseInt(res.result.blockNumber, 16) |
||||
const currentBlockNumber = Number.parseInt(blockTracker.getCurrentBlock(), 16) |
||||
if (blockNumber > currentBlockNumber) await blockTracker.checkForLatestBlock() |
||||
}) |
||||
} |
@ -0,0 +1,35 @@ |
||||
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') |
||||
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware') |
||||
const createFetchMiddleware = require('eth-json-rpc-middleware/fetch') |
||||
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-ref') |
||||
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-cache') |
||||
const BlockTracker = require('eth-block-tracker') |
||||
|
||||
module.exports = createJsonRpcClient |
||||
|
||||
function createJsonRpcClient({ rpcUrl }) { |
||||
const fetchMiddleware = createFetchMiddleware({ rpcUrl }) |
||||
const blockProvider = providerFromMiddleware(fetchMiddleware) |
||||
const blockTracker = new BlockTracker({ provider: blockProvider }) |
||||
|
||||
const networkMiddleware = mergeMiddleware([ |
||||
createBlockRefMiddleware({ blockTracker }), |
||||
createBlockCacheMiddleware({ blockTracker }), |
||||
createInflightMiddleware(), |
||||
createBlockTrackerInspectorMiddleware({ blockTracker }), |
||||
fetchMiddleware, |
||||
]) |
||||
return { networkMiddleware, blockTracker } |
||||
} |
||||
|
||||
// inspect if response contains a block ref higher than our latest block
|
||||
const futureBlockRefRequests = ['eth_getTransactionByHash', 'eth_getTransactionReceipt'] |
||||
function createBlockTrackerInspectorMiddleware ({ blockTracker }) { |
||||
return createAsyncMiddleware(async (req, res, next) => { |
||||
if (!futureBlockRefRequests.includes(req.method)) return next() |
||||
await next() |
||||
const blockNumber = Number.parseInt(res.result.blockNumber, 16) |
||||
const currentBlockNumber = Number.parseInt(blockTracker.getCurrentBlock(), 16) |
||||
if (blockNumber > currentBlockNumber) await blockTracker.checkForLatestBlock() |
||||
}) |
||||
} |
@ -0,0 +1,33 @@ |
||||
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') |
||||
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware') |
||||
const createFetchMiddleware = require('eth-json-rpc-middleware/fetch') |
||||
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-ref') |
||||
const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-cache') |
||||
const BlockTracker = require('eth-block-tracker') |
||||
|
||||
module.exports = createLocalhostClient |
||||
|
||||
function createLocalhostClient() { |
||||
const fetchMiddleware = createFetchMiddleware({ rpcUrl: 'http://localhost:8545/' }) |
||||
const blockProvider = providerFromMiddleware(fetchMiddleware) |
||||
const blockTracker = new BlockTracker({ provider: blockProvider, pollingInterval: 1000 }) |
||||
|
||||
const networkMiddleware = mergeMiddleware([ |
||||
createBlockRefMiddleware({ blockTracker }), |
||||
createBlockTrackerInspectorMiddleware({ blockTracker }), |
||||
fetchMiddleware, |
||||
]) |
||||
return { networkMiddleware, blockTracker } |
||||
} |
||||
|
||||
// inspect if response contains a block ref higher than our latest block
|
||||
const futureBlockRefRequests = ['eth_getTransactionByHash', 'eth_getTransactionReceipt'] |
||||
function createBlockTrackerInspectorMiddleware ({ blockTracker }) { |
||||
return createAsyncMiddleware(async (req, res, next) => { |
||||
if (!futureBlockRefRequests.includes(req.method)) return next() |
||||
await next() |
||||
const blockNumber = Number.parseInt(res.result.blockNumber, 16) |
||||
const currentBlockNumber = Number.parseInt(blockTracker.getCurrentBlock(), 16) |
||||
if (blockNumber > currentBlockNumber) await blockTracker.checkForLatestBlock() |
||||
}) |
||||
} |
@ -0,0 +1,43 @@ |
||||
const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') |
||||
const createScaffoldMiddleware = require('json-rpc-engine/src/scaffold') |
||||
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware') |
||||
const createWalletSubprovider = require('eth-json-rpc-middleware/wallet') |
||||
|
||||
module.exports = createMetamaskMiddleware |
||||
|
||||
function createMetamaskMiddleware({ |
||||
version, |
||||
getAccounts, |
||||
processTransaction, |
||||
processEthSignMessage, |
||||
processTypedMessage, |
||||
processPersonalMessage, |
||||
getPendingNonce |
||||
}) { |
||||
const metamaskMiddleware = mergeMiddleware([ |
||||
createScaffoldMiddleware({ |
||||
// staticSubprovider
|
||||
eth_syncing: false, |
||||
web3_clientVersion: `MetaMask/v${version}`, |
||||
}), |
||||
createWalletSubprovider({ |
||||
getAccounts, |
||||
processTransaction, |
||||
processEthSignMessage, |
||||
processTypedMessage, |
||||
processPersonalMessage, |
||||
}), |
||||
createPendingNonceMiddleware({ getPendingNonce }), |
||||
}) |
||||
return metamaskMiddleware |
||||
} |
||||
|
||||
function createPendingNonceMiddleware ({ getPendingNonce }) { |
||||
return createAsyncMiddleware(async (req, res, next) => { |
||||
if (req.method !== 'eth_getTransactionCount') return next() |
||||
const address = req.params[0] |
||||
const blockRef = req.params[1] |
||||
if (blockRef !== 'pending') return next() |
||||
req.result = await getPendingNonce(address) |
||||
}) |
||||
} |
Loading…
Reference in new issue