From 58e95334d39d6380a211557dd89417d41190f137 Mon Sep 17 00:00:00 2001 From: Roei Erez Date: Sun, 19 Jan 2025 18:59:32 +0200 Subject: [PATCH] fix confirmed balance (cherry picked from commit 538a100140c948a867360972c399beda79601e75) --- lib/core/src/sdk.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/core/src/sdk.rs b/lib/core/src/sdk.rs index 2af9a3c..34ada20 100644 --- a/lib/core/src/sdk.rs +++ b/lib/core/src/sdk.rs @@ -2519,21 +2519,17 @@ impl LiquidSdk { async fn update_wallet_info(&self) -> Result<()> { let transactions = self.onchain_wallet.transactions().await?; - let wallet_amount_sat = transactions - .into_iter() - .filter(|tx| tx.height.is_some()) - .map(|tx| { - tx.balance - .into_iter() - .filter_map(|(asset_id, balance)| { - if asset_id == utils::lbtc_asset_id(self.config.network) { - return Some(balance); - } - None - }) - .sum::() + let mut wallet_amount_sat: i64 = 0; + transactions.into_iter().for_each(|tx| { + tx.balance.into_iter().for_each(|(asset_id, balance)| { + if asset_id == utils::lbtc_asset_id(self.config.network) { + //consider only confirmed unspent outputs (confirmed transactions output reduced by unconfirmed spent outputs) + if tx.height.is_some() || balance < 0 { + wallet_amount_sat += balance; + } + } }) - .sum::(); + }); debug!("Onchain wallet balance: {wallet_amount_sat} sats"); let mut pending_send_sat = 0;