2020-01-09 04:34:58 +01:00
|
|
|
import { formatTxMetaForRpcResult } from '../util'
|
|
|
|
import createAsyncMiddleware from 'json-rpc-engine/src/createAsyncMiddleware'
|
2019-10-30 23:15:54 +01:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function createPendingNonceMiddleware ({ getPendingNonce }) {
|
2019-10-30 23:15:54 +01:00
|
|
|
return createAsyncMiddleware(async (req, res, next) => {
|
2019-12-03 21:50:55 +01:00
|
|
|
const { method, params } = req
|
2019-11-20 01:03:20 +01:00
|
|
|
if (method !== 'eth_getTransactionCount') {
|
|
|
|
return next()
|
|
|
|
}
|
2019-10-30 23:15:54 +01:00
|
|
|
const [param, blockRef] = params
|
2019-11-20 01:03:20 +01:00
|
|
|
if (blockRef !== 'pending') {
|
|
|
|
return next()
|
|
|
|
}
|
2019-10-30 23:15:54 +01:00
|
|
|
res.result = await getPendingNonce(param)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export function createPendingTxMiddleware ({ getPendingTransactionByHash }) {
|
2019-10-30 23:15:54 +01:00
|
|
|
return createAsyncMiddleware(async (req, res, next) => {
|
2019-12-03 21:50:55 +01:00
|
|
|
const { method, params } = req
|
2019-11-20 01:03:20 +01:00
|
|
|
if (method !== 'eth_getTransactionByHash') {
|
|
|
|
return next()
|
|
|
|
}
|
2019-10-30 23:15:54 +01:00
|
|
|
const [hash] = params
|
|
|
|
const txMeta = getPendingTransactionByHash(hash)
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!txMeta) {
|
|
|
|
return next()
|
|
|
|
}
|
2019-10-30 23:15:54 +01:00
|
|
|
res.result = formatTxMetaForRpcResult(txMeta)
|
|
|
|
})
|
|
|
|
}
|