Skip to content

Commit 4f78af4

Browse files
committed
add txpool_content RPC
1 parent 5ae06b4 commit 4f78af4

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

packages/eth-providers/src/base-provider.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,24 @@ export interface CallReturnInfo {
264264
logs: Log[];
265265
}
266266

267+
export interface PendingTx {
268+
blockHash: string;
269+
blockNumber: null;
270+
from: string;
271+
gas: string;
272+
gasPrice: string;
273+
hash: string;
274+
input: string;
275+
nonce: string;
276+
to: string;
277+
transactionIndex: null;
278+
value: string;
279+
}
280+
export interface TxpoolContent {
281+
pending: { [from: string]: { [nonce: string]: PendingTx } };
282+
queued: { [from: string]: { [nonce: string]: PendingTx } };
283+
}
284+
267285
export abstract class BaseProvider extends AbstractProvider {
268286
readonly _api?: ApiPromise;
269287
readonly subql?: SubqlProvider;
@@ -2070,6 +2088,42 @@ export abstract class BaseProvider extends AbstractProvider {
20702088
return found;
20712089
};
20722090

2091+
txpoolContent = async (): Promise<TxpoolContent> => {
2092+
const pendingExtrinsics = await this.api.rpc.author.pendingExtrinsics();
2093+
const pendingTxs = await Promise.all(pendingExtrinsics
2094+
.filter(isEvmExtrinsic)
2095+
.map(async extrinsic => {
2096+
const from = await this.getEvmAddress(extrinsic.signer.toString());
2097+
const { value, gas, input, to, nonce } = parseExtrinsic(extrinsic);
2098+
2099+
return {
2100+
blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
2101+
blockNumber: null,
2102+
from,
2103+
gas,
2104+
gasPrice: 0, // hard to calculate
2105+
hash: extrinsic.toHex(),
2106+
input,
2107+
nonce,
2108+
to,
2109+
transactionIndex: null,
2110+
value,
2111+
};
2112+
}));
2113+
2114+
const pending = pendingTxs.reduce((res, tx) => {
2115+
res[tx.from] ??= {};
2116+
res[tx.from][tx.nonce] = hexlifyRpcResult(tx);
2117+
2118+
return res;
2119+
}, {} as TxpoolContent['pending']);
2120+
2121+
return {
2122+
pending,
2123+
queued: {},
2124+
};
2125+
};
2126+
20732127
on = (_eventName: EventType, _listener: Listener): Provider => throwNotImplemented('on');
20742128
once = (_eventName: EventType, _listener: Listener): Provider => throwNotImplemented('once');
20752129
emit = (_eventName: EventType, ..._args: Array<any>): boolean => throwNotImplemented('emit');

packages/eth-providers/src/utils/receiptHelper.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TX } from '../base-provider';
66
import { TransactionReceipt as TransactionReceiptSubql } from './gqlTypes';
77
import { hexToU8a, nToU8a } from '@polkadot/util';
88
import { hexValue } from '@ethersproject/bytes';
9-
import { isOrphanEvmEvent } from './utils';
9+
import { isEvmExtrinsic, isOrphanEvmEvent } from './utils';
1010
import { keccak256 } from '@ethersproject/keccak256';
1111
import { logger } from './logger';
1212
import type { EventRecord, SignedBlock } from '@polkadot/types/interfaces';
@@ -176,7 +176,6 @@ export const findEvmEvent = (events: EventRecord[]): EventRecord | undefined =>
176176
};
177177

178178
// parse info that can be extracted from extrinsic alone
179-
// only works for EVM extrinsics
180179
export const parseExtrinsic = (
181180
extrinsic: GenericExtrinsic
182181
): {
@@ -200,7 +199,7 @@ export const parseExtrinsic = (
200199
...DUMMY_V_R_S, // TODO: get correct VRS
201200
};
202201

203-
if (extrinsic.method.section.toUpperCase() !== 'EVM') {
202+
if (!isEvmExtrinsic(extrinsic)){
204203
return NONE_EVM_TX_DEFAULT_DATA;
205204
}
206205

packages/eth-rpc-adapter/src/eip1193-bridge.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,9 @@ class Eip1193BridgeImpl {
495495

496496
return this.#provider.removeEventListener(params[0]);
497497
}
498+
499+
async txpool_content(params: any[]): Promise<any> {
500+
validate([], params);
501+
return this.#provider.txpoolContent();
502+
}
498503
}

0 commit comments

Comments
 (0)