From 01e12329e9558a204ec5c4c25a8b964494eda4cb Mon Sep 17 00:00:00 2001 From: Nicolas Dorier Date: Mon, 7 Oct 2024 09:37:56 +0900 Subject: [PATCH] Remove additional cryptoCode from events (#6277) --- BTCPayServer.Tests/GreenfieldAPITests.cs | 2 +- .../BitcoinLike/BitcoinLikePayoutHandler.cs | 23 +++++++++---------- BTCPayServer/Events/NewBlockEvent.cs | 6 +++-- .../Events/NewOnChainTransactionEvent.cs | 5 ++-- BTCPayServer/Extensions.cs | 13 ++++------- .../TransactionLabelMarkerHostedService.cs | 2 +- .../Payments/Bitcoin/NBXplorerListener.cs | 4 ++-- .../Monero/Services/MoneroListener.cs | 2 +- .../Altcoins/Zcash/Services/ZcashListener.cs | 2 +- .../Services/Wallets/WalletReceiveService.cs | 7 +++--- BTCPayServer/WalletId.cs | 7 ++---- 11 files changed, 35 insertions(+), 38 deletions(-) diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index 14c361983..7199e5859 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -3776,7 +3776,7 @@ namespace BTCPayServer.Tests { await tester.ExplorerNode.GenerateAsync(1); - }, bevent => bevent.CryptoCode.Equals("BTC", StringComparison.Ordinal)); + }, bevent => bevent.PaymentMethodId == PaymentTypes.CHAIN.GetPaymentMethodId("BTC")); Assert.Contains( await client.ShowOnChainWalletTransactions(walletId.StoreId, walletId.CryptoCode, diff --git a/BTCPayServer/Data/Payouts/BitcoinLike/BitcoinLikePayoutHandler.cs b/BTCPayServer/Data/Payouts/BitcoinLike/BitcoinLikePayoutHandler.cs index 6f9aa8968..7378775f2 100644 --- a/BTCPayServer/Data/Payouts/BitcoinLike/BitcoinLikePayoutHandler.cs +++ b/BTCPayServer/Data/Payouts/BitcoinLike/BitcoinLikePayoutHandler.cs @@ -35,7 +35,6 @@ using StoreData = BTCPayServer.Data.StoreData; public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork { public string Currency { get; } - private readonly BTCPayNetworkProvider _btcPayNetworkProvider; private readonly PaymentMethodHandlerDictionary _paymentHandlers; private readonly ExplorerClientProvider _explorerClientProvider; private readonly BTCPayNetworkJsonSerializerSettings _jsonSerializerSettings; @@ -65,7 +64,6 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork EventAggregator eventAggregator, TransactionLinkProviders transactionLinkProviders) { - _btcPayNetworkProvider = btcPayNetworkProvider; PayoutMethodId = payoutMethodId; PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(network.CryptoCode); Network = network; @@ -174,12 +172,14 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork public async Task BackgroundCheck(object o) { - if (o is NewOnChainTransactionEvent newTransaction && newTransaction.NewTransactionEvent.TrackedSource is AddressTrackedSource addressTrackedSource) + if (o is NewOnChainTransactionEvent newTransaction && newTransaction.NewTransactionEvent.TrackedSource is AddressTrackedSource addressTrackedSource + && newTransaction.PaymentMethodId == PaymentMethodId) { await UpdatePayoutsAwaitingForPayment(newTransaction, addressTrackedSource); } - if (o is NewBlockEvent || o is NewOnChainTransactionEvent) + if ((o is NewBlockEvent nbe && nbe.PaymentMethodId == PaymentMethodId) || + (o is NewOnChainTransactionEvent nct && nct.PaymentMethodId == PaymentMethodId)) { await UpdatePayoutsInProgress(); } @@ -416,18 +416,17 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork { try { - var network = _btcPayNetworkProvider.GetNetwork(newTransaction.CryptoCode); var destinationSum = - newTransaction.NewTransactionEvent.Outputs.Sum(output => output.Value.GetValue(network)); + newTransaction.NewTransactionEvent.Outputs.Sum(output => output.Value.GetValue(Network)); var destination = addressTrackedSource.Address.ToString(); - var paymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(newTransaction.CryptoCode); + await using var ctx = _dbContextFactory.CreateContext(); var payout = await ctx.Payouts .Include(o => o.StoreData) .Include(o => o.PullPaymentData) .Where(p => p.State == PayoutState.AwaitingPayment) - .Where(p => p.PayoutMethodId == paymentMethodId.ToString()) + .Where(p => p.PayoutMethodId == PaymentMethodId.ToString()) #pragma warning disable CA1307 // Specify StringComparison .Where(p => destination.Equals(p.DedupId)) #pragma warning restore CA1307 // Specify StringComparison @@ -440,15 +439,15 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork // The round up here is not strictly necessary, this is temporary to fix existing payout before we // were properly roundup the crypto amount destinationSum != - BTCPayServer.Extensions.RoundUp(payout.Amount.Value, network.Divisibility)) + BTCPayServer.Extensions.RoundUp(payout.Amount.Value, Network.Divisibility)) return; var derivationSchemeSettings = payout.StoreData - .GetDerivationSchemeSettings(_paymentHandlers, newTransaction.CryptoCode)?.AccountDerivation; + .GetDerivationSchemeSettings(_paymentHandlers, Network.CryptoCode)?.AccountDerivation; if (derivationSchemeSettings is null) return; - var storeWalletMatched = (await _explorerClientProvider.GetExplorerClient(newTransaction.CryptoCode) + var storeWalletMatched = (await _explorerClientProvider.GetExplorerClient(Network.CryptoCode) .GetTransactionAsync(derivationSchemeSettings, newTransaction.NewTransactionEvent.TransactionData.TransactionHash)); //if the wallet related to the store of the payout does not have the tx: it has been paid externally @@ -463,7 +462,7 @@ public class BitcoinLikePayoutHandler : IPayoutHandler, IHasNetwork { payout.State = PayoutState.InProgress; await WalletRepository.AddWalletTransactionAttachment( - new WalletId(payout.StoreDataId, newTransaction.CryptoCode), + new WalletId(payout.StoreDataId, Network.CryptoCode), newTransaction.NewTransactionEvent.TransactionData.TransactionHash, Attachment.Payout(payout.PullPaymentDataId, payout.Id)); } diff --git a/BTCPayServer/Events/NewBlockEvent.cs b/BTCPayServer/Events/NewBlockEvent.cs index 5faef5d6c..5edbe9e26 100644 --- a/BTCPayServer/Events/NewBlockEvent.cs +++ b/BTCPayServer/Events/NewBlockEvent.cs @@ -1,11 +1,13 @@ +using BTCPayServer.Payments; + namespace BTCPayServer.Events { public class NewBlockEvent { - public string CryptoCode { get; set; } + public PaymentMethodId PaymentMethodId { get; set; } public override string ToString() { - return $"{CryptoCode}: New block"; + return $"{PaymentMethodId}: New block"; } } } diff --git a/BTCPayServer/Events/NewOnChainTransactionEvent.cs b/BTCPayServer/Events/NewOnChainTransactionEvent.cs index d695fac15..86de21969 100644 --- a/BTCPayServer/Events/NewOnChainTransactionEvent.cs +++ b/BTCPayServer/Events/NewOnChainTransactionEvent.cs @@ -1,3 +1,4 @@ +using BTCPayServer.Payments; using NBXplorer.Models; namespace BTCPayServer.Events @@ -5,12 +6,12 @@ namespace BTCPayServer.Events public class NewOnChainTransactionEvent { public NewTransactionEvent NewTransactionEvent { get; set; } - public string CryptoCode { get; set; } + public PaymentMethodId PaymentMethodId { get; set; } public override string ToString() { var state = NewTransactionEvent.BlockId == null ? "Unconfirmed" : "Confirmed"; - return $"{CryptoCode}: New transaction {NewTransactionEvent.TransactionData.TransactionHash} ({state})"; + return $"{PaymentMethodId}: New transaction {NewTransactionEvent.TransactionData.TransactionHash} ({state})"; } } } diff --git a/BTCPayServer/Extensions.cs b/BTCPayServer/Extensions.cs index 589df9b12..3e8721b80 100644 --- a/BTCPayServer/Extensions.cs +++ b/BTCPayServer/Extensions.cs @@ -410,20 +410,17 @@ namespace BTCPayServer } public static BitcoinLikePaymentHandler? TryGetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, BTCPayNetwork network) - { - return handlers.TryGetBitcoinHandler(network.CryptoCode); - } + => handlers.TryGetBitcoinHandler(network.CryptoCode); public static BitcoinLikePaymentHandler? TryGetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, string cryptoCode) + => handlers.TryGetBitcoinHandler(PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode)); + public static BitcoinLikePaymentHandler? TryGetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, PaymentMethodId paymentMethodId) { - var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode); - if (handlers.TryGetValue(pmi, out var h) && h is BitcoinLikePaymentHandler b) + if (handlers.TryGetValue(paymentMethodId, out var h) && h is BitcoinLikePaymentHandler b) return b; return null; } public static BitcoinLikePaymentHandler GetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, BTCPayNetwork network) - { - return handlers.GetBitcoinHandler(network.CryptoCode); - } + => handlers.GetBitcoinHandler(network.CryptoCode); public static BitcoinLikePaymentHandler GetBitcoinHandler(this PaymentMethodHandlerDictionary handlers, string cryptoCode) { var pmi = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode); diff --git a/BTCPayServer/HostedServices/TransactionLabelMarkerHostedService.cs b/BTCPayServer/HostedServices/TransactionLabelMarkerHostedService.cs index d9b72c800..305ebcfbf 100644 --- a/BTCPayServer/HostedServices/TransactionLabelMarkerHostedService.cs +++ b/BTCPayServer/HostedServices/TransactionLabelMarkerHostedService.cs @@ -43,7 +43,7 @@ namespace BTCPayServer.HostedServices // If we find, then we create a link between them and the tx object. case NewOnChainTransactionEvent transactionEvent: { - var handler = _handlers.TryGetBitcoinHandler(transactionEvent.CryptoCode); + var handler = _handlers.TryGetBitcoinHandler(transactionEvent.PaymentMethodId); var derivation = transactionEvent.NewTransactionEvent.DerivationStrategy; if (handler is null || derivation is null) break; diff --git a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs index 9a39b8518..c698adc52 100644 --- a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs +++ b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs @@ -152,7 +152,7 @@ namespace BTCPayServer.Payments.Bitcoin { case NBXplorer.Models.NewBlockEvent evt: await UpdatePaymentStates(wallet); - _Aggregator.Publish(new Events.NewBlockEvent() { CryptoCode = evt.CryptoCode }); + _Aggregator.Publish(new Events.NewBlockEvent() { PaymentMethodId = pmi }); break; case NBXplorer.Models.NewTransactionEvent evt: if (evt.DerivationStrategy != null) @@ -202,7 +202,7 @@ namespace BTCPayServer.Payments.Bitcoin _Aggregator.Publish(new NewOnChainTransactionEvent() { - CryptoCode = wallet.Network.CryptoCode, + PaymentMethodId = pmi, NewTransactionEvent = evt }); diff --git a/BTCPayServer/Services/Altcoins/Monero/Services/MoneroListener.cs b/BTCPayServer/Services/Altcoins/Monero/Services/MoneroListener.cs index bc4342d05..901a77953 100644 --- a/BTCPayServer/Services/Altcoins/Monero/Services/MoneroListener.cs +++ b/BTCPayServer/Services/Altcoins/Monero/Services/MoneroListener.cs @@ -267,7 +267,7 @@ namespace BTCPayServer.Services.Altcoins.Monero.Services private async Task OnNewBlock(string cryptoCode) { await UpdateAnyPendingMoneroLikePayment(cryptoCode); - _eventAggregator.Publish(new NewBlockEvent() { CryptoCode = cryptoCode }); + _eventAggregator.Publish(new NewBlockEvent() { PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode) }); } private async Task OnTransactionUpdated(string cryptoCode, string transactionHash) diff --git a/BTCPayServer/Services/Altcoins/Zcash/Services/ZcashListener.cs b/BTCPayServer/Services/Altcoins/Zcash/Services/ZcashListener.cs index 7ade159c4..b0202ff49 100644 --- a/BTCPayServer/Services/Altcoins/Zcash/Services/ZcashListener.cs +++ b/BTCPayServer/Services/Altcoins/Zcash/Services/ZcashListener.cs @@ -265,7 +265,7 @@ namespace BTCPayServer.Services.Altcoins.Zcash.Services private async Task OnNewBlock(string cryptoCode) { await UpdateAnyPendingZcashLikePayment(cryptoCode); - _eventAggregator.Publish(new NewBlockEvent() { CryptoCode = cryptoCode }); + _eventAggregator.Publish(new NewBlockEvent() { PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(cryptoCode) }); } private async Task OnTransactionUpdated(string cryptoCode, string transactionHash) diff --git a/BTCPayServer/Services/Wallets/WalletReceiveService.cs b/BTCPayServer/Services/Wallets/WalletReceiveService.cs index a0a4549be..ec0c0d252 100644 --- a/BTCPayServer/Services/Wallets/WalletReceiveService.cs +++ b/BTCPayServer/Services/Wallets/WalletReceiveService.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Data; using BTCPayServer.Events; +using BTCPayServer.Payments; using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Stores; using Microsoft.Extensions.Hosting; @@ -102,11 +103,11 @@ namespace BTCPayServer.Services.Wallets _walletReceiveState.AddOrReplace(walletId, information); } - public IEnumerable> GetByDerivation(string cryptoCode, + public IEnumerable> GetByDerivation(PaymentMethodId paymentMethodId, DerivationStrategyBase derivationStrategyBase) { return _walletReceiveState.Where(pair => - pair.Key.CryptoCode.Equals(cryptoCode, StringComparison.InvariantCulture) && + pair.Key.PaymentMethodId == paymentMethodId && pair.Value.DerivationStrategy == derivationStrategyBase); } @@ -117,7 +118,7 @@ namespace BTCPayServer.Services.Wallets _leases.Add(_eventAggregator.Subscribe(evt => { - var matching = GetByDerivation(evt.CryptoCode, evt.NewTransactionEvent.DerivationStrategy).Where(pair => + var matching = GetByDerivation(evt.PaymentMethodId, evt.NewTransactionEvent.DerivationStrategy).Where(pair => evt.NewTransactionEvent.Outputs.Any(output => output.ScriptPubKey == pair.Value.ScriptPubKey)); foreach (var keyValuePair in matching) diff --git a/BTCPayServer/WalletId.cs b/BTCPayServer/WalletId.cs index 16d532e56..d907fb61e 100644 --- a/BTCPayServer/WalletId.cs +++ b/BTCPayServer/WalletId.cs @@ -27,14 +27,11 @@ namespace BTCPayServer ArgumentNullException.ThrowIfNull(cryptoCode); StoreId = storeId; CryptoCode = cryptoCode; + PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(CryptoCode); } public string StoreId { get; } public string CryptoCode { get; } - - public PaymentMethodId GetPaymentMethodId() - { - return PaymentTypes.CHAIN.GetPaymentMethodId(CryptoCode); - } + public PaymentMethodId PaymentMethodId { get; } public static WalletId Parse(string id) {