From 099c9fa1f91b893537b8cdcd2d1d8ddbd0d2bb51 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sat, 13 Jan 2018 12:53:56 +0900 Subject: [PATCH] Fix balance calculation when there is unconfirmed tx --- BTCPayServer/Services/Wallets/BTCPayWallet.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/BTCPayServer/Services/Wallets/BTCPayWallet.cs b/BTCPayServer/Services/Wallets/BTCPayWallet.cs index f87dc77db..90956226c 100644 --- a/BTCPayServer/Services/Wallets/BTCPayWallet.cs +++ b/BTCPayServer/Services/Wallets/BTCPayWallet.cs @@ -108,9 +108,17 @@ namespace BTCPayServer.Services.Wallets public async Task GetBalance(DerivationStrategyBase derivationStrategy) { var result = await _Client.SyncAsync(derivationStrategy, null, true); - return result.Confirmed.UTXOs.Select(u => u.Value) - .Concat(result.Unconfirmed.UTXOs.Select(u => u.Value)) - .Sum(); + + Dictionary received = new Dictionary(); + foreach(var utxo in result.Confirmed.UTXOs.Concat(result.Unconfirmed.UTXOs)) + { + received.TryAdd(utxo.Outpoint, utxo); + } + foreach (var utxo in result.Confirmed.SpentOutpoints.Concat(result.Unconfirmed.SpentOutpoints)) + { + received.Remove(utxo); + } + return received.Values.Select(c => c.Value).Sum(); } } }