From 3bc232e1dae31488a2017d54a99a322ca97e52a4 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sun, 18 Feb 2018 02:35:02 +0900 Subject: [PATCH] Further isolate bitcoin related stuff inside BitcoinLikePaymentData --- .../Controllers/InvoiceController.UI.cs | 4 +- .../HostedServices/NBXplorerListener.cs | 13 +++--- .../Services/Invoices/InvoiceEntity.cs | 40 ++++++++++--------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 3cfbf9421..3a93917e6 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -80,14 +80,14 @@ namespace BTCPayServer.Controllers var payments = invoice .GetPayments() - .Where(p => p.GetCryptoPaymentData() is BitcoinLikePaymentData) + .Where(p => p.GetCryptoPaymentDataType() == BitcoinLikePaymentData.OnchainBitcoinType) .Select(async payment => { var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData(); var m = new InvoiceDetailsModel.Payment(); var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode()); m.CryptoCode = payment.GetCryptoCode(); - m.DepositAddress = payment.GetScriptPubKey().GetDestinationAddress(paymentNetwork.NBitcoinNetwork); + m.DepositAddress = paymentData.Output.ScriptPubKey.GetDestinationAddress(paymentNetwork.NBitcoinNetwork); int confirmationCount = 0; if(paymentData.Legacy) // The confirmation count in the paymentData is not up to date diff --git a/BTCPayServer/HostedServices/NBXplorerListener.cs b/BTCPayServer/HostedServices/NBXplorerListener.cs index 6888333f2..3b6d36ada 100644 --- a/BTCPayServer/HostedServices/NBXplorerListener.cs +++ b/BTCPayServer/HostedServices/NBXplorerListener.cs @@ -137,7 +137,7 @@ namespace BTCPayServer.HostedServices await session.ListenNewBlockAsync(_Cts.Token).ConfigureAwait(false); await session.ListenDerivationSchemesAsync((await GetStrategies(network)).ToArray(), _Cts.Token).ConfigureAwait(false); - Logs.PayServer.LogInformation($"{network.CryptoCode}: if any pending invoice got paid while offline..."); + Logs.PayServer.LogInformation($"{network.CryptoCode}: checking if any pending invoice got paid while offline..."); int paymentCount = await FindPaymentViaPolling(wallet, network); Logs.PayServer.LogInformation($"{network.CryptoCode}: {paymentCount} payments happened while offline"); @@ -209,8 +209,8 @@ namespace BTCPayServer.HostedServices IEnumerable GetAllBitcoinPaymentData(InvoiceEntity invoice) { return invoice.GetPayments() - .Select(p => p.GetCryptoPaymentData() as BitcoinLikePaymentData) - .Where(p => p != null); + .Where(p => p.GetCryptoPaymentDataType() == BitcoinLikePaymentData.OnchainBitcoinType) + .Select(p => (BitcoinLikePaymentData)p.GetCryptoPaymentData()); } async Task UpdatePaymentStates(BTCPayWallet wallet, string invoiceId) @@ -223,9 +223,9 @@ namespace BTCPayServer.HostedServices var conflicts = GetConflicts(transactions.Select(t => t.Value)); foreach (var payment in invoice.GetPayments(wallet.Network)) { - var paymentData = payment.GetCryptoPaymentData() as BitcoinLikePaymentData; - if (paymentData == null) + if (payment.GetCryptoPaymentDataType() != BitcoinLikePaymentData.OnchainBitcoinType) continue; + var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData(); if (!transactions.TryGetValue(paymentData.Outpoint.Hash, out TransactionResult tx)) continue; var txId = tx.Transaction.GetHash(); @@ -346,9 +346,10 @@ namespace BTCPayServer.HostedServices private async Task ReceivedPayment(BTCPayWallet wallet, string invoiceId, PaymentEntity payment, DerivationStrategyBase strategy) { + var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData(); var invoice = (await UpdatePaymentStates(wallet, invoiceId)); var cryptoData = invoice.GetCryptoData(wallet.Network, _ExplorerClients.NetworkProviders); - if (cryptoData.GetDepositAddress().ScriptPubKey == payment.GetScriptPubKey() && cryptoData.Calculate().Due > Money.Zero) + if (cryptoData.GetDepositAddress().ScriptPubKey == paymentData.Output.ScriptPubKey && cryptoData.Calculate().Due > Money.Zero) { var address = await wallet.ReserveAddressAsync(strategy); await _InvoiceRepository.NewAddress(invoiceId, address, wallet.Network); diff --git a/BTCPayServer/Services/Invoices/InvoiceEntity.cs b/BTCPayServer/Services/Invoices/InvoiceEntity.cs index f1670ee94..35f993570 100644 --- a/BTCPayServer/Services/Invoices/InvoiceEntity.cs +++ b/BTCPayServer/Services/Invoices/InvoiceEntity.cs @@ -573,7 +573,7 @@ namespace BTCPayServer.Services.Invoices paidEnough |= totalDue <= paid; if (CryptoCode == _.GetCryptoCode()) { - cryptoPaid += _.GetValue(); + cryptoPaid += _.GetCryptoPaymentData().GetValue(); txCount++; } return _; @@ -617,13 +617,6 @@ namespace BTCPayServer.Services.Invoices get; set; } - public Script GetScriptPubKey() - { -#pragma warning disable CS0618 - return Output.ScriptPubKey; -#pragma warning restore CS0618 - } - public bool Accounted { get; set; @@ -639,9 +632,16 @@ namespace BTCPayServer.Services.Invoices [Obsolete("Use GetCryptoPaymentData() instead")] public string CryptoPaymentData { get; set; } - [Obsolete("Use GetCryptoPaymentData() instead")] + [Obsolete("Use GetCryptoPaymentDataType() instead")] public string CryptoPaymentDataType { get; set; } + public string GetCryptoPaymentDataType() + { +#pragma warning disable CS0618 // Type or member is obsolete + return String.IsNullOrEmpty(CryptoPaymentDataType) ? BitcoinLikePaymentData.OnchainBitcoinType : CryptoPaymentDataType; +#pragma warning restore CS0618 // Type or member is obsolete + } + public CryptoPaymentData GetCryptoPaymentData() { #pragma warning disable CS0618 @@ -656,7 +656,7 @@ namespace BTCPayServer.Services.Invoices paymentData.Legacy = true; return paymentData; } - if (CryptoPaymentDataType == "BTCLike") + if (CryptoPaymentDataType == BitcoinLikePaymentData.OnchainBitcoinType) { var paymentData = JsonConvert.DeserializeObject(CryptoPaymentData); // legacy @@ -674,7 +674,7 @@ namespace BTCPayServer.Services.Invoices #pragma warning disable CS0618 if (cryptoPaymentData is BitcoinLikePaymentData paymentData) { - CryptoPaymentDataType = "BTCLike"; + CryptoPaymentDataType = BitcoinLikePaymentData.OnchainBitcoinType; // Legacy Outpoint = paymentData.Outpoint; Output = paymentData.Output; @@ -685,13 +685,6 @@ namespace BTCPayServer.Services.Invoices CryptoPaymentData = JsonConvert.SerializeObject(cryptoPaymentData); #pragma warning restore CS0618 } - - public Money GetValue() - { -#pragma warning disable CS0618 - return Output.Value; -#pragma warning restore CS0618 - } public Money GetValue(Dictionary cryptoData, string cryptoCode, Money value = null) { #pragma warning disable CS0618 @@ -730,6 +723,11 @@ namespace BTCPayServer.Services.Invoices /// /// The search terms string[] GetSearchTerms(); + /// + /// Get value of what as been paid + /// + /// The amount paid + Money GetValue(); bool PaymentCompleted(PaymentEntity entity, BTCPayNetwork network); bool PaymentConfirmed(PaymentEntity entity, SpeedPolicy speedPolicy, BTCPayNetwork network); @@ -737,6 +735,7 @@ namespace BTCPayServer.Services.Invoices public class BitcoinLikePaymentData : CryptoPaymentData { + public readonly static string OnchainBitcoinType = "BTCLike"; public BitcoinLikePaymentData() { @@ -770,6 +769,11 @@ namespace BTCPayServer.Services.Invoices return new[] { Outpoint.Hash.ToString() }; } + public Money GetValue() + { + return Output.Value; + } + public bool PaymentCompleted(PaymentEntity entity, BTCPayNetwork network) { return ConfirmationCount >= network.MaxTrackedConfirmation;