2020-12-02 20:41:24 +01:00
|
|
|
import { createAsyncMiddleware } from 'json-rpc-engine'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { formatTxMetaForRpcResult } from '../util'
|
2019-10-30 23:15:54 +01:00
|
|
|
|
2020-11-03 00:41:28 +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') {
|
2020-08-12 21:06:57 +02:00
|
|
|
next()
|
|
|
|
return
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2019-10-30 23:15:54 +01:00
|
|
|
const [param, blockRef] = params
|
2019-11-20 01:03:20 +01:00
|
|
|
if (blockRef !== 'pending') {
|
2020-08-12 21:06:57 +02:00
|
|
|
next()
|
|
|
|
return
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2019-10-30 23:15:54 +01:00
|
|
|
res.result = await getPendingNonce(param)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +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') {
|
2020-08-12 21:06:57 +02:00
|
|
|
next()
|
|
|
|
return
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
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) {
|
2020-08-12 21:06:57 +02:00
|
|
|
next()
|
|
|
|
return
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2019-10-30 23:15:54 +01:00
|
|
|
res.result = formatTxMetaForRpcResult(txMeta)
|
|
|
|
})
|
|
|
|
}
|