From e31e600144ca3ac38c141b840d27fb72fc46d63f Mon Sep 17 00:00:00 2001 From: Kukks Date: Sun, 1 Dec 2019 15:30:56 +0100 Subject: [PATCH 1/3] update nbx + prep bitcoin payment data ctor change --- .../BTCPayServer.Common.csproj | 2 +- BTCPayServer/Controllers/WalletsController.cs | 4 +-- BTCPayServer/Extensions/MoneyExtensions.cs | 30 +++++++++++++++++++ .../Bitcoin/BitcoinLikePaymentData.cs | 6 ++-- .../Payments/Bitcoin/NBXplorerListener.cs | 4 +-- BTCPayServer/Services/Wallets/BTCPayWallet.cs | 2 +- 6 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 BTCPayServer/Extensions/MoneyExtensions.cs diff --git a/BTCPayServer.Common/BTCPayServer.Common.csproj b/BTCPayServer.Common/BTCPayServer.Common.csproj index 0c5a12120..ffbddfe94 100644 --- a/BTCPayServer.Common/BTCPayServer.Common.csproj +++ b/BTCPayServer.Common/BTCPayServer.Common.csproj @@ -5,6 +5,6 @@ - + diff --git a/BTCPayServer/Controllers/WalletsController.cs b/BTCPayServer/Controllers/WalletsController.cs index 48de86cb0..481d67364 100644 --- a/BTCPayServer/Controllers/WalletsController.cs +++ b/BTCPayServer/Controllers/WalletsController.cs @@ -252,7 +252,7 @@ namespace BTCPayServer.Controllers vm.Id = tx.TransactionId.ToString(); vm.Link = string.Format(CultureInfo.InvariantCulture, paymentMethod.Network.BlockExplorerLink, vm.Id); vm.Timestamp = tx.Timestamp; - vm.Positive = tx.BalanceChange >= Money.Zero; + vm.Positive = tx.BalanceChange.GetValue(wallet.Network) >= Money.Zero; vm.Balance = tx.BalanceChange.ToString(); vm.IsConfirmed = tx.Confirmations != 0; @@ -912,7 +912,7 @@ namespace BTCPayServer.Controllers } else if (command == "prune") { - var result = await ExplorerClientProvider.GetExplorerClient(walletId.CryptoCode).PruneAsync(derivationScheme.AccountDerivation, cancellationToken); + var result = await ExplorerClientProvider.GetExplorerClient(walletId.CryptoCode).PruneAsync(derivationScheme.AccountDerivation, new PruneRequest(), cancellationToken); if (result.TotalPruned == 0) { TempData[WellKnownTempData.SuccessMessage] = $"The wallet is already pruned"; diff --git a/BTCPayServer/Extensions/MoneyExtensions.cs b/BTCPayServer/Extensions/MoneyExtensions.cs new file mode 100644 index 000000000..e800b9485 --- /dev/null +++ b/BTCPayServer/Extensions/MoneyExtensions.cs @@ -0,0 +1,30 @@ +using System; +using System.Linq; +using NBitcoin; + +namespace BTCPayServer +{ + public static class MoneyExtensions + { + public static Money GetValue(this IMoney m, BTCPayNetwork network = null) + { + switch (m) + { + case Money money: + return money; + case MoneyBag mb: + return mb.Select(money => money.GetValue(network)).Sum(); +// case AssetMoney assetMoney: +// if (network is ElementsBTCPayNetwork elementsBTCPayNetwork) +// { +// return elementsBTCPayNetwork.AssetId == assetMoney.AssetId +// ? new Money(assetMoney.Quantity) +// : Money.Zero; +// } +// throw new NotSupportedException("IMoney type not supported"); + default: + throw new NotSupportedException("IMoney type not supported"); + } + } + } +} diff --git a/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentData.cs b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentData.cs index 3ded51cbb..c742b8cb7 100644 --- a/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentData.cs +++ b/BTCPayServer/Payments/Bitcoin/BitcoinLikePaymentData.cs @@ -20,10 +20,10 @@ namespace BTCPayServer.Payments.Bitcoin { } - public BitcoinLikePaymentData(Coin coin, bool rbf) + public BitcoinLikePaymentData(TxOut txout, OutPoint outpoint, bool rbf) { - Outpoint = coin.Outpoint; - Output = coin.TxOut; + Outpoint = outpoint; + Output = txout; ConfirmationCount = 0; RBF = rbf; } diff --git a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs index e0bb76c3e..072fa983f 100644 --- a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs +++ b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs @@ -157,7 +157,7 @@ namespace BTCPayServer.Payments.Bitcoin var invoice = (await _InvoiceRepository.GetInvoicesFromAddresses(new [] {key})).FirstOrDefault(); if (invoice != null) { - var paymentData = new BitcoinLikePaymentData(txCoin, evt.TransactionData.Transaction.RBF); + var paymentData = new BitcoinLikePaymentData(txCoin.TxOut, txCoin.Outpoint, evt.TransactionData.Transaction.RBF); var alreadyExist = GetAllBitcoinPaymentData(invoice).Where(c => c.GetPaymentId() == paymentData.GetPaymentId()).Any(); if (!alreadyExist) { @@ -338,7 +338,7 @@ namespace BTCPayServer.Payments.Bitcoin foreach (var coin in coins.Where(c => !alreadyAccounted.Contains(c.Coin.Outpoint))) { var transaction = await wallet.GetTransactionAsync(coin.Coin.Outpoint.Hash); - var paymentData = new BitcoinLikePaymentData(coin.Coin, transaction.Transaction.RBF); + var paymentData = new BitcoinLikePaymentData(coin.Coin.TxOut, coin.Coin.Outpoint, transaction.Transaction.RBF); var payment = await _InvoiceRepository.AddPayment(invoice.Id, coin.Timestamp, paymentData, network).ConfigureAwait(false); alreadyAccounted.Add(coin.Coin.Outpoint); if (payment != null) diff --git a/BTCPayServer/Services/Wallets/BTCPayWallet.cs b/BTCPayServer/Services/Wallets/BTCPayWallet.cs index c21ed79b8..d11800445 100644 --- a/BTCPayServer/Services/Wallets/BTCPayWallet.cs +++ b/BTCPayServer/Services/Wallets/BTCPayWallet.cs @@ -181,7 +181,7 @@ namespace BTCPayServer.Services.Wallets public async Task GetBalance(DerivationStrategyBase derivationStrategy, CancellationToken cancellation = default(CancellationToken)) { UTXOChanges changes = await GetUTXOChanges(derivationStrategy, cancellation); - return changes.GetUnspentUTXOs().Select(c => c.Value).Sum(); + return changes.GetUnspentUTXOs().Select(c => c.Value.GetValue(_Network)).Sum(); } } } From bc2a039ea27dd7a319aa1800aae53673a0894495 Mon Sep 17 00:00:00 2001 From: Kukks Date: Mon, 2 Dec 2019 09:57:38 +0100 Subject: [PATCH 2/3] bump nbx and make balance return decimal --- BTCPayServer.Common/BTCPayServer.Common.csproj | 2 +- BTCPayServer/Controllers/WalletsController.cs | 4 ++-- BTCPayServer/Extensions/MoneyExtensions.cs | 9 ++++----- BTCPayServer/Services/Wallets/BTCPayWallet.cs | 6 +++--- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/BTCPayServer.Common/BTCPayServer.Common.csproj b/BTCPayServer.Common/BTCPayServer.Common.csproj index ffbddfe94..f0a8011e3 100644 --- a/BTCPayServer.Common/BTCPayServer.Common.csproj +++ b/BTCPayServer.Common/BTCPayServer.Common.csproj @@ -5,6 +5,6 @@ - + diff --git a/BTCPayServer/Controllers/WalletsController.cs b/BTCPayServer/Controllers/WalletsController.cs index 481d67364..5ea4e2d9d 100644 --- a/BTCPayServer/Controllers/WalletsController.cs +++ b/BTCPayServer/Controllers/WalletsController.cs @@ -252,7 +252,7 @@ namespace BTCPayServer.Controllers vm.Id = tx.TransactionId.ToString(); vm.Link = string.Format(CultureInfo.InvariantCulture, paymentMethod.Network.BlockExplorerLink, vm.Id); vm.Timestamp = tx.Timestamp; - vm.Positive = tx.BalanceChange.GetValue(wallet.Network) >= Money.Zero; + vm.Positive = tx.BalanceChange.GetValue(wallet.Network) >= 0; vm.Balance = tx.BalanceChange.ToString(); vm.IsConfirmed = tx.Confirmations != 0; @@ -313,7 +313,7 @@ namespace BTCPayServer.Controllers var feeProvider = _feeRateProvider.CreateFeeProvider(network); var recommendedFees = feeProvider.GetFeeRateAsync(); var balance = _walletProvider.GetWallet(network).GetBalance(paymentMethod.AccountDerivation); - model.CurrentBalance = (await balance).ToDecimal(MoneyUnit.BTC); + model.CurrentBalance = await balance; model.RecommendedSatoshiPerByte = (int)(await recommendedFees).GetFee(1).Satoshi; model.FeeSatoshiPerByte = model.RecommendedSatoshiPerByte; model.SupportRBF = network.SupportRBF; diff --git a/BTCPayServer/Extensions/MoneyExtensions.cs b/BTCPayServer/Extensions/MoneyExtensions.cs index e800b9485..3a4b5c653 100644 --- a/BTCPayServer/Extensions/MoneyExtensions.cs +++ b/BTCPayServer/Extensions/MoneyExtensions.cs @@ -1,19 +1,18 @@ using System; -using System.Linq; using NBitcoin; namespace BTCPayServer { public static class MoneyExtensions { - public static Money GetValue(this IMoney m, BTCPayNetwork network = null) + public static decimal GetValue(this IMoney m, BTCPayNetwork network = null) { switch (m) { case Money money: - return money; - case MoneyBag mb: - return mb.Select(money => money.GetValue(network)).Sum(); + return money.ToDecimal(MoneyUnit.BTC); +// case MoneyBag mb: +// return mb.Select(money => money.GetValue(network)).Sum(); // case AssetMoney assetMoney: // if (network is ElementsBTCPayNetwork elementsBTCPayNetwork) // { diff --git a/BTCPayServer/Services/Wallets/BTCPayWallet.cs b/BTCPayServer/Services/Wallets/BTCPayWallet.cs index d11800445..612a22551 100644 --- a/BTCPayServer/Services/Wallets/BTCPayWallet.cs +++ b/BTCPayServer/Services/Wallets/BTCPayWallet.cs @@ -178,10 +178,10 @@ namespace BTCPayServer.Services.Wallets }).ToArray(); } - public async Task GetBalance(DerivationStrategyBase derivationStrategy, CancellationToken cancellation = default(CancellationToken)) + public async Task GetBalance(DerivationStrategyBase derivationStrategy, CancellationToken cancellation = default(CancellationToken)) { - UTXOChanges changes = await GetUTXOChanges(derivationStrategy, cancellation); - return changes.GetUnspentUTXOs().Select(c => c.Value.GetValue(_Network)).Sum(); + var result = await _Client.GetBalanceAsync(derivationStrategy, cancellation); + return result.Total.GetValue(_Network); } } } From d6e1d34ebf6875d79c6dd77d0db32a23e9705ad6 Mon Sep 17 00:00:00 2001 From: Kukks Date: Mon, 2 Dec 2019 10:10:24 +0100 Subject: [PATCH 3/3] update nbx docker tests --- BTCPayServer.Tests/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BTCPayServer.Tests/docker-compose.yml b/BTCPayServer.Tests/docker-compose.yml index 7294f666b..3de9cc25d 100644 --- a/BTCPayServer.Tests/docker-compose.yml +++ b/BTCPayServer.Tests/docker-compose.yml @@ -76,7 +76,7 @@ services: - customer_lnd - merchant_lnd nbxplorer: - image: nicolasdorier/nbxplorer:2.0.0.66 + image: nicolasdorier/nbxplorer:2.1.1 restart: unless-stopped ports: - "32838:32838"