From 89b4de2484f9cb99d3e465b0d817cbc237c4dbaa Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 30 Dec 2020 02:27:34 +0700 Subject: [PATCH] Fixed addressTransactions with pagination in electrum API. --- backend/src/api/bitcoin/electrum-api.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/src/api/bitcoin/electrum-api.ts b/backend/src/api/bitcoin/electrum-api.ts index ab805ea70..ee7fe2276 100644 --- a/backend/src/api/bitcoin/electrum-api.ts +++ b/backend/src/api/bitcoin/electrum-api.ts @@ -110,14 +110,25 @@ class BitcoindElectrsApi extends BitcoinApi implements AbstractBitcoinApi { if (!addressInfo || !addressInfo.isvalid) { return []; } - const history = await this.$getScriptHashHistory(addressInfo.scriptPubKey); const transactions: IEsploraApi.Transaction[] = []; - for (const h of history) { - const tx = await this.$getRawTransaction(h.tx_hash, false, true); + const history = await this.$getScriptHashHistory(addressInfo.scriptPubKey); + history.reverse(); + + let startingIndex = 0; + if (lastSeenTxId) { + const pos = history.findIndex((historicalTx) => historicalTx.tx_hash === lastSeenTxId); + if (pos) { + startingIndex = pos + 1; + } + } + + for (let i = startingIndex; i < Math.min(startingIndex + 10, history.length); i++) { + const tx = await this.$getRawTransaction(history[i].tx_hash, false, true); if (tx) { transactions.push(tx); } } + return transactions; }