From dd7401a198f4b44fc5705de30ff3359d6f17b36c Mon Sep 17 00:00:00 2001 From: eauxxs Date: Sat, 25 May 2024 20:39:03 +0800 Subject: [PATCH] fix bug: esplora negative balance --- src/indexers/esplora.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/indexers/esplora.rs b/src/indexers/esplora.rs index 31a89eb..822ecfd 100644 --- a/src/indexers/esplora.rs +++ b/src/indexers/esplora.rs @@ -23,7 +23,7 @@ use std::collections::BTreeMap; use std::num::NonZeroU32; -use bpstd::{Address, LockTime, Outpoint, SeqNo, Witness}; +use bpstd::{Address, LockTime, Outpoint, ScriptPubkey, SeqNo, Witness}; use descriptors::Descriptor; use esplora::{BlockingClient, Error}; @@ -97,6 +97,29 @@ impl From for WalletTx { } } +fn get_scripthash_txs_all( + client: &BlockingClient, + script: &ScriptPubkey, +) -> Result, Error> { + const PAGE_SIZE: usize = 25; + let mut res = Vec::new(); + let mut last_seen = None; + loop { + let r = client.scripthash_txs(script, last_seen)?; + match &r[..] { + [a @ .., esplora::Tx { txid, .. }] if a.len() >= PAGE_SIZE - 1 => { + last_seen = Some(*txid); + res.extend(r); + } + _ => { + res.extend(r); + break; + } + } + } + Ok(res) +} + impl Indexer for BlockingClient { type Error = Error; @@ -116,7 +139,7 @@ impl Indexer for BlockingClient { eprint!("."); let mut txids = Vec::new(); - match self.scripthash_txs(&script, None) { + match get_scripthash_txs_all(self, &script) { Err(err) => { errors.push(err); break;