Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/wallet-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DBTxHistory[]>
Expand Down
15 changes: 9 additions & 6 deletions src/wallet/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/contract/wallet-storage.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down
Loading