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

Commit 1b76dc4

Browse files
committed
feat: txpool_contentFrom
1 parent 46609dc commit 1b76dc4

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,6 +3437,36 @@ export default class EthereumApi implements Api {
34373437
};
34383438
}
34393439

3440+
3441+
/**
3442+
* Returns the number of transactions created by specified address currently pending for inclusion in the next block(s), as well as the ones that are being scheduled for future execution only.
3443+
*
3444+
* @param address - The account address
3445+
* @returns The transactions currently pending or queued in the transaction pool by address.
3446+
* @example
3447+
* ```javascript
3448+
* const [from] = await provider.request({ method: "eth_accounts", params: [] });
3449+
* const pendingTx = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", nonce:"0x0" }] });
3450+
* const queuedTx = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", nonce:"0x2" }] });
3451+
* const pool = await provider.send("txpool_contentFrom", [from]);
3452+
* console.log(pool);
3453+
* ```
3454+
*/
3455+
@assertArgLength(1)
3456+
async txpool_contentFrom(address: DATA): Promise<Ethereum.Pool.Content<"private">> {
3457+
const { transactions } = this.#blockchain;
3458+
const {
3459+
transactionPool: { executables, origins, processMap }
3460+
} = transactions;
3461+
3462+
const fromAddress = Address.from(address);
3463+
3464+
return {
3465+
pending: processMap(executables.pending, fromAddress),
3466+
queued: processMap(origins, fromAddress)
3467+
};
3468+
}
3469+
34403470
/**
34413471
* 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.
34423472
*

src/chains/ethereum/ethereum/src/transaction-pool.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { EthereumInternalOptions } from "@ganache/ethereum-options";
1515
import { Executables } from "./miner/executables";
1616
import { TypedTransaction } from "@ganache/ethereum-transaction";
1717
import { Ethereum } from "./api-types";
18+
import { Address } from "@ganache/ethereum-address";
1819

1920
/**
2021
* Checks if the `replacer` is eligible to replace the `replacee` transaction
@@ -454,7 +455,7 @@ export default class TransactionPool extends Emittery<{ drain: undefined }> {
454455
return null;
455456
}
456457

457-
public processMap(map: Map<string, Heap<TypedTransaction>>) {
458+
public processMap(map: Map<string, Heap<TypedTransaction>>, address?: Address) {
458459
let res: Record<
459460
string,
460461
Record<string, Ethereum.Pool.Transaction<"private">>
@@ -463,6 +464,11 @@ export default class TransactionPool extends Emittery<{ drain: undefined }> {
463464
for (let [_, { array, length }] of map) {
464465
for (let i = 0; i < length; ++i) {
465466
const transaction = array[i];
467+
468+
if(address && transaction.from.toString() != address.toString()) {
469+
continue;
470+
}
471+
466472
const from = transaction.from.toString();
467473
if (res[from] === undefined) {
468474
res[from] = {};

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,64 @@ describe("txpool", () => {
121121
});
122122
});
123123

124+
describe("contentFrom", () => {
125+
let provider: EthereumProvider;
126+
let accounts: string[];
127+
128+
beforeEach(async () => {
129+
provider = await getProvider({
130+
miner: { blockTime: 1000 }
131+
});
132+
accounts = await provider.send("eth_accounts");
133+
});
134+
135+
it("handles pending and queued transactions", async () => {
136+
const pendingTransactions = await Promise.all([
137+
provider.send("eth_sendTransaction", [
138+
{
139+
from: accounts[1],
140+
to: accounts[2]
141+
}
142+
]),
143+
provider.send("eth_sendTransaction", [
144+
{
145+
from: accounts[2],
146+
to: accounts[3]
147+
}
148+
]),
149+
provider.send("eth_sendTransaction", [
150+
{
151+
from: accounts[1],
152+
to: accounts[2]
153+
}
154+
])
155+
]);
156+
157+
const queuedTransactions = await Promise.all([
158+
provider.send("eth_sendTransaction", [
159+
{
160+
from: accounts[1],
161+
to: accounts[2],
162+
nonce: "0x123",
163+
}
164+
])
165+
]);
166+
167+
const { pending, queued } = await provider.send("txpool_contentFrom", [accounts[1]]);
168+
169+
const pendingAddresses = Object.keys(pending);
170+
const queuedAddresses = Object.keys(queued);
171+
172+
assert(pendingAddresses.findIndex((value) => value === accounts[1]) == 0)
173+
assert(pendingAddresses.findIndex((value) => value === accounts[2]) == -1)
174+
assert(pendingAddresses.length == 1)
175+
176+
assert(queuedAddresses.findIndex((value) => value === accounts[1]) == 0)
177+
assert(queuedAddresses.findIndex((value) => value === accounts[2]) == -1)
178+
assert(queuedAddresses.length == 1)
179+
})
180+
})
181+
124182
describe("status", () => {
125183
let provider: EthereumProvider;
126184
let accounts: string[];

0 commit comments

Comments
 (0)