Skip to content

Commit 6b4a9cf

Browse files
committed
Simplifying iterateAddressHistory to be more opinionated about direction and pageSize since it's a helper method anyway.
1 parent 8a3a0eb commit 6b4a9cf

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src.ts/_tests/test-providers-otterscan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ describe("Test Otterscan Provider", function () {
346346

347347
it("should have async iterator for address history", function () {
348348
const provider = createMockOtsProvider();
349-
const iterator = provider.iterateAddressHistory("0x123", "before", 4096);
349+
const iterator = provider.iterateAddressHistory("0x123", 4000, 4096);
350350

351351
assert(typeof iterator[Symbol.asyncIterator] === "function", "should be async iterable");
352352
});

src.ts/providers/provider-otterscan.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import type { FetchRequest } from "../utils/index.js";
2121
import type { BlockParams, TransactionReceiptParams, TransactionResponseParams } from "./formatting.js";
2222
import type { Fragment } from "../abi/index.js";
2323

24+
// Maximum page size for Otterscan queries - API limitation
25+
const OTS_MAX_PAGE_SIZE = 25;
26+
2427
// Formatted Otterscan receipt (extends standard receipt with timestamp)
2528
export interface OtsTransactionReceiptParams extends TransactionReceiptParams {
2629
timestamp: number; // Otterscan adds a Unix timestamp
@@ -357,39 +360,43 @@ export class OtterscanProvider extends JsonRpcProvider {
357360
}
358361

359362
/**
360-
* Iterate through transaction history for an address
363+
* Iterate through transaction history for an address between block ranges
361364
* @param address - Address to search
362-
* @param direction - Search direction ("before" or "after")
363-
* @param startBlock - Starting block number
364-
* @param pageSize - Soft limit on results per page (default: 25, actual results may exceed this if a block contains more transactions)
365-
* @yields Object with tx and receipt for each transaction
365+
* @param startBlock - Starting block number (inclusive)
366+
* @param endBlock - Ending block number (inclusive)
367+
* @yields Object with tx and receipt for each transaction in ascending block order
366368
*/
367369
async *iterateAddressHistory(
368370
address: string,
369-
direction: "before" | "after",
370371
startBlock: number,
371-
pageSize: number = 25
372+
endBlock: number
372373
): AsyncGenerator<{ tx: TransactionResponseParams; receipt: OtsTransactionReceiptParams }, void, unknown> {
373374
let currentBlock = startBlock;
374375

375-
while (true) {
376-
const page =
377-
direction === "before"
378-
? await this.searchTransactionsBefore(address, currentBlock, pageSize)
379-
: await this.searchTransactionsAfter(address, currentBlock, pageSize);
376+
while (currentBlock <= endBlock) {
377+
const page = await this.searchTransactionsAfter(address, currentBlock, OTS_MAX_PAGE_SIZE);
380378

381-
// Yield each transaction with its receipt
379+
// Filter and yield transactions within our range
382380
for (let i = 0; i < page.txs.length; i++) {
383-
yield {
384-
tx: page.txs[i],
385-
receipt: page.receipts[i]
386-
};
381+
const tx = page.txs[i];
382+
const blockNum = Number(tx.blockNumber);
383+
384+
// Only yield transactions within the specified range
385+
if (blockNum >= startBlock && blockNum <= endBlock) {
386+
yield {
387+
tx: tx,
388+
receipt: page.receipts[i]
389+
};
390+
}
391+
392+
// Stop if we've gone past the end block
393+
if (blockNum > endBlock) return;
387394
}
388395

389-
// Check if we've reached the end
390-
if (direction === "before" ? page.lastPage : page.firstPage) break;
396+
// Check if we've reached the end of available data
397+
if (page.lastPage) break;
391398

392-
// Update block cursor for next iteration
399+
// Move to the next block after the last transaction we saw
393400
const lastTx = page.txs[page.txs.length - 1];
394401
if (!lastTx) break;
395402

@@ -400,7 +407,8 @@ export class OtterscanProvider extends JsonRpcProvider {
400407
throw new Error(`Iterator stuck on block ${currentBlock}. API returned same block number.`);
401408
}
402409

403-
currentBlock = nextBlock;
410+
// Move cursor forward
411+
currentBlock = nextBlock + 1;
404412
}
405413
}
406414
}

0 commit comments

Comments
 (0)