diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 535be2fa9..cf2ad9c26 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -84,29 +84,6 @@ namespace BTCPayServer.Controllers Current = !h.UnAssigned.HasValue }).ToArray(); - var updateConfirmationCountIfNeeded = invoice - .GetPayments() - .Select(async payment => - { - var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode()); - var paymentData = payment.GetCryptoPaymentData(); - if (paymentData is Payments.Bitcoin.BitcoinLikePaymentData onChainPaymentData) - { - int confirmationCount = 0; - if ((onChainPaymentData.ConfirmationCount < paymentNetwork.MaxTrackedConfirmation && payment.Accounted) - && (onChainPaymentData.Legacy || invoice.MonitoringExpiration < DateTimeOffset.UtcNow)) - // The confirmation count in the paymentData is not up to date - { - confirmationCount = (await ((ExplorerClientProvider)_ServiceProvider.GetService(typeof(ExplorerClientProvider))).GetExplorerClient(payment.GetCryptoCode())?.GetTransactionAsync(onChainPaymentData.Outpoint.Hash))?.Confirmations ?? 0; - onChainPaymentData.ConfirmationCount = confirmationCount; - payment.SetCryptoPaymentData(onChainPaymentData); - await _InvoiceRepository.UpdatePayments(new List { payment }); - } - } - }) - .ToArray(); - await Task.WhenAll(updateConfirmationCountIfNeeded); - var details = InvoicePopulatePayments(invoice); model.CryptoPayments = details.CryptoPayments; model.OnChainPayments = details.OnChainPayments; diff --git a/BTCPayServer/HostedServices/InvoiceWatcher.cs b/BTCPayServer/HostedServices/InvoiceWatcher.cs index 35c6de91a..a9ac1160a 100644 --- a/BTCPayServer/HostedServices/InvoiceWatcher.cs +++ b/BTCPayServer/HostedServices/InvoiceWatcher.cs @@ -43,15 +43,18 @@ namespace BTCPayServer.HostedServices InvoiceRepository _InvoiceRepository; EventAggregator _EventAggregator; BTCPayNetworkProvider _NetworkProvider; + ExplorerClientProvider _ExplorerClientProvider; public InvoiceWatcher( BTCPayNetworkProvider networkProvider, InvoiceRepository invoiceRepository, - EventAggregator eventAggregator) + EventAggregator eventAggregator, + ExplorerClientProvider explorerClientProvider) { _InvoiceRepository = invoiceRepository ?? throw new ArgumentNullException(nameof(invoiceRepository)); _EventAggregator = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator)); _NetworkProvider = networkProvider; + _ExplorerClientProvider = explorerClientProvider; } CompositeDisposable leases = new CompositeDisposable(); @@ -285,6 +288,29 @@ namespace BTCPayServer.HostedServices if (invoice.Status == InvoiceStatus.Complete || ((invoice.Status == InvoiceStatus.Invalid || invoice.Status == InvoiceStatus.Expired) && invoice.MonitoringExpiration < DateTimeOffset.UtcNow)) { + var updateConfirmationCountIfNeeded = invoice + .GetPayments() + .Select(async payment => + { + var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode()); + var paymentData = payment.GetCryptoPaymentData(); + if (paymentData is Payments.Bitcoin.BitcoinLikePaymentData onChainPaymentData) + { + // Do update if confirmation count in the paymentData is not up to date + if ((onChainPaymentData.ConfirmationCount < paymentNetwork.MaxTrackedConfirmation && payment.Accounted) + && (onChainPaymentData.Legacy || invoice.MonitoringExpiration < DateTimeOffset.UtcNow)) + { + var transactionResult = await _ExplorerClientProvider.GetExplorerClient(payment.GetCryptoCode())?.GetTransactionAsync(onChainPaymentData.Outpoint.Hash); + var confirmationCount = transactionResult?.Confirmations ?? 0; + onChainPaymentData.ConfirmationCount = confirmationCount; + payment.SetCryptoPaymentData(onChainPaymentData); + await _InvoiceRepository.UpdatePayments(new List { payment }); + } + } + }) + .ToArray(); + await Task.WhenAll(updateConfirmationCountIfNeeded); + if (await _InvoiceRepository.RemovePendingInvoice(invoice.Id)) _EventAggregator.Publish(new InvoiceStopWatchedEvent(invoice.Id)); break;