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

Commit

Permalink
feat: txpool_status
Browse files Browse the repository at this point in the history
  • Loading branch information
amandesai01 committed Sep 24, 2022
1 parent f053f6d commit 46609dc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/chains/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3430,12 +3430,49 @@ export default class EthereumApi implements Api {
const {
transactionPool: { executables, origins, processMap }
} = transactions;

return {
pending: processMap(executables.pending),
queued: processMap(origins)
};
}

/**
* 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.
*
* @returns The count of transactions currently pending or queued in the transaction pool.
* @example
* ```javascript
* const [from] = await provider.request({ method: "eth_accounts", params: [] });
* const pendingTx = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", nonce:"0x0" }] });
* const queuedTx = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", nonce:"0x2" }] });
* const pool = await provider.send("txpool_status");
* console.log(pool);
* ```
*/
@assertArgLength(0)
async txpool_status(): Promise<{pending: number, queued: number}> {
const { transactions } = this.#blockchain;
const {
transactionPool: { executables, origins }
} = transactions;

let pending = 0;
let queued = 0;

for (const [_, transactions] of executables.pending) {
pending += transactions?.array.length || 0;
}

for (const [_, transactions] of origins) {
queued += transactions?.array.length || 0;
}

return {
pending,
queued
};
}

//#endregion
}
48 changes: 48 additions & 0 deletions src/chains/ethereum/ethereum/tests/api/txpool/txpool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,52 @@ describe("txpool", () => {
assert.deepStrictEqual(queued, {});
});
});

describe("status", () => {
let provider: EthereumProvider;
let accounts: string[];
beforeEach(async () => {
provider = await getProvider({
miner: { blockTime: 1000 }
});
accounts = await provider.send("eth_accounts");
});

it("handles pending and queued transactions", async () => {
const pendingTransactions = await Promise.all([
provider.send("eth_sendTransaction", [
{
from: accounts[1],
to: accounts[2]
}
]),
provider.send("eth_sendTransaction", [
{
from: accounts[2],
to: accounts[3]
}
]),
provider.send("eth_sendTransaction", [
{
from: accounts[1],
to: accounts[2]
}
])
]);

const queuedTransactions = await Promise.all([
provider.send("eth_sendTransaction", [
{
from: accounts[1],
to: accounts[2],
nonce: "0x123",
}
])
]);

const { pending, queued } = await provider.send("txpool_status");
assert.strictEqual(pending, pendingTransactions.length);
assert.strictEqual(queued, queuedTransactions.length);
});
});
});

0 comments on commit 46609dc

Please sign in to comment.