From ce36d17764ce01297438b2de02aee285af84f9b6 Mon Sep 17 00:00:00 2001 From: Matt Grote Date: Mon, 17 Aug 2026 16:36:22 +0200 Subject: [PATCH] fix: return all transaction history rows unless a limit is asked for --- src/core/wallet-storage.ts | 2 +- src/wallet/queries.ts | 15 +++++++++------ test/contract/wallet-storage.contract.ts | 12 ++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/core/wallet-storage.ts b/src/core/wallet-storage.ts index 2e8b9c7..a933228 100644 --- a/src/core/wallet-storage.ts +++ b/src/core/wallet-storage.ts @@ -198,7 +198,7 @@ type WalletStorage = { * descending by block number. * @param walletId - Identifier of the wallet. * @param chainId - Chain identifier. - * @param limit - Maximum number of records to return. + * @param limit - Maximum number of rows to return. Omit for all of them. * @returns Transaction history rows ordered by block descending. */ getTxHistory (walletId: string, chainId: number, limit?: number): Promise diff --git a/src/wallet/queries.ts b/src/wallet/queries.ts index 3dc4454..e78c291 100644 --- a/src/wallet/queries.ts +++ b/src/wallet/queries.ts @@ -496,26 +496,29 @@ async function insertTxHistoryBatch (db: WalletDatabase, txs: DBNewTxHistory[]): } /** - * Retrieve recent transaction history for a wallet on a given chain. + * Retrieve transaction history for a wallet on a given chain. + * + * Every row is returned unless the caller asks for fewer, so a caller that + * wants the whole history does not have to know a row count to ask for it. * @param db - Wallet database instance. * @param walletId - Identifier of the wallet. * @param chainId - Chain identifier. - * @param limit - Maximum number of records to return (default 100). + * @param limit - Maximum number of rows to return. Omit for all of them. * @returns - Transaction history rows ordered by block desc. */ async function getTxHistory ( db: WalletDatabase, walletId: string, chainId: number, - limit: number = 100 + limit?: number ) { - return db + const query = db .select() .from(txHistory) .where(and(eq(txHistory.walletId, walletId), eq(txHistory.chainId, chainId))) .orderBy(sql`${txHistory.blockNumber} DESC`) - .limit(limit) - .all() + + return limit === undefined ? query.all() : query.limit(limit).all() } /** diff --git a/test/contract/wallet-storage.contract.ts b/test/contract/wallet-storage.contract.ts index 13ef212..28e2cb6 100644 --- a/test/contract/wallet-storage.contract.ts +++ b/test/contract/wallet-storage.contract.ts @@ -186,6 +186,18 @@ function runWalletStorageContract (name: string, makeHarness: WalletHarnessFacto assert.equal((await storage.getScanState(WALLET_ID, 1))?.lastScannedBlock, 300n) })) + test('transaction history returns every row until a limit is asked for', withWallet(async ({ storage }) => { + const ROWS = 120 + await storage.insertTxHistoryBatch( + Array.from({ length: ROWS }, (_, index) => + createTestTxHistory({ id: `tx-${index}`, blockNumber: BigInt(index) }) + ) + ) + + assert.equal((await storage.getTxHistory(WALLET_ID, 1)).length, ROWS) + assert.equal((await storage.getTxHistory(WALLET_ID, 1, 10)).length, 10) + })) + test('transaction history dedupes and returns newest first', withWallet(async ({ storage }) => { assert.equal(await storage.insertTxHistoryBatch([]), 0) await storage.insertTxHistory(createTestTxHistory({ id: 'tx-1', blockNumber: 10n }))