Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 46609dc

Browse files
committed
feat: txpool_status
1 parent f053f6d commit 46609dc

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/chains/ethereum/ethereum/src/api.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3430,12 +3430,49 @@ export default class EthereumApi implements Api {
34303430
const {
34313431
transactionPool: { executables, origins, processMap }
34323432
} = transactions;
3433-
3433+
34343434
return {
34353435
pending: processMap(executables.pending),
34363436
queued: processMap(origins)
34373437
};
34383438
}
34393439

3440+
/**
3441+
* Returns the number of transactions currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only.
3442+
*
3443+
* @returns The count of transactions currently pending or queued in the transaction pool.
3444+
* @example
3445+
* ```javascript
3446+
* const [from] = await provider.request({ method: "eth_accounts", params: [] });
3447+
* const pendingTx = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", nonce:"0x0" }] });
3448+
* const queuedTx = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", nonce:"0x2" }] });
3449+
* const pool = await provider.send("txpool_status");
3450+
* console.log(pool);
3451+
* ```
3452+
*/
3453+
@assertArgLength(0)
3454+
async txpool_status(): Promise<{pending: number, queued: number}> {
3455+
const { transactions } = this.#blockchain;
3456+
const {
3457+
transactionPool: { executables, origins }
3458+
} = transactions;
3459+
3460+
let pending = 0;
3461+
let queued = 0;
3462+
3463+
for (const [_, transactions] of executables.pending) {
3464+
pending += transactions?.array.length || 0;
3465+
}
3466+
3467+
for (const [_, transactions] of origins) {
3468+
queued += transactions?.array.length || 0;
3469+
}
3470+
3471+
return {
3472+
pending,
3473+
queued
3474+
};
3475+
}
3476+
34403477
//#endregion
34413478
}

src/chains/ethereum/ethereum/tests/api/txpool/txpool.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,52 @@ describe("txpool", () => {
120120
assert.deepStrictEqual(queued, {});
121121
});
122122
});
123+
124+
describe("status", () => {
125+
let provider: EthereumProvider;
126+
let accounts: string[];
127+
beforeEach(async () => {
128+
provider = await getProvider({
129+
miner: { blockTime: 1000 }
130+
});
131+
accounts = await provider.send("eth_accounts");
132+
});
133+
134+
it("handles pending and queued transactions", async () => {
135+
const pendingTransactions = await Promise.all([
136+
provider.send("eth_sendTransaction", [
137+
{
138+
from: accounts[1],
139+
to: accounts[2]
140+
}
141+
]),
142+
provider.send("eth_sendTransaction", [
143+
{
144+
from: accounts[2],
145+
to: accounts[3]
146+
}
147+
]),
148+
provider.send("eth_sendTransaction", [
149+
{
150+
from: accounts[1],
151+
to: accounts[2]
152+
}
153+
])
154+
]);
155+
156+
const queuedTransactions = await Promise.all([
157+
provider.send("eth_sendTransaction", [
158+
{
159+
from: accounts[1],
160+
to: accounts[2],
161+
nonce: "0x123",
162+
}
163+
])
164+
]);
165+
166+
const { pending, queued } = await provider.send("txpool_status");
167+
assert.strictEqual(pending, pendingTransactions.length);
168+
assert.strictEqual(queued, queuedTransactions.length);
169+
});
170+
});
123171
});

0 commit comments

Comments
 (0)