mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 06:21:44 +01:00
Moving update of confirmation count to InvoiceWatcher
This commit is contained in:
parent
de73fedd1b
commit
e6c794d68f
2 changed files with 27 additions and 24 deletions
|
@ -84,29 +84,6 @@ namespace BTCPayServer.Controllers
|
||||||
Current = !h.UnAssigned.HasValue
|
Current = !h.UnAssigned.HasValue
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
|
|
||||||
var updateConfirmationCountIfNeeded = invoice
|
|
||||||
.GetPayments()
|
|
||||||
.Select<PaymentEntity, Task>(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<PaymentEntity> { payment });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.ToArray();
|
|
||||||
await Task.WhenAll(updateConfirmationCountIfNeeded);
|
|
||||||
|
|
||||||
var details = InvoicePopulatePayments(invoice);
|
var details = InvoicePopulatePayments(invoice);
|
||||||
model.CryptoPayments = details.CryptoPayments;
|
model.CryptoPayments = details.CryptoPayments;
|
||||||
model.OnChainPayments = details.OnChainPayments;
|
model.OnChainPayments = details.OnChainPayments;
|
||||||
|
|
|
@ -43,15 +43,18 @@ namespace BTCPayServer.HostedServices
|
||||||
InvoiceRepository _InvoiceRepository;
|
InvoiceRepository _InvoiceRepository;
|
||||||
EventAggregator _EventAggregator;
|
EventAggregator _EventAggregator;
|
||||||
BTCPayNetworkProvider _NetworkProvider;
|
BTCPayNetworkProvider _NetworkProvider;
|
||||||
|
ExplorerClientProvider _ExplorerClientProvider;
|
||||||
|
|
||||||
public InvoiceWatcher(
|
public InvoiceWatcher(
|
||||||
BTCPayNetworkProvider networkProvider,
|
BTCPayNetworkProvider networkProvider,
|
||||||
InvoiceRepository invoiceRepository,
|
InvoiceRepository invoiceRepository,
|
||||||
EventAggregator eventAggregator)
|
EventAggregator eventAggregator,
|
||||||
|
ExplorerClientProvider explorerClientProvider)
|
||||||
{
|
{
|
||||||
_InvoiceRepository = invoiceRepository ?? throw new ArgumentNullException(nameof(invoiceRepository));
|
_InvoiceRepository = invoiceRepository ?? throw new ArgumentNullException(nameof(invoiceRepository));
|
||||||
_EventAggregator = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator));
|
_EventAggregator = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator));
|
||||||
_NetworkProvider = networkProvider;
|
_NetworkProvider = networkProvider;
|
||||||
|
_ExplorerClientProvider = explorerClientProvider;
|
||||||
}
|
}
|
||||||
CompositeDisposable leases = new CompositeDisposable();
|
CompositeDisposable leases = new CompositeDisposable();
|
||||||
|
|
||||||
|
@ -285,6 +288,29 @@ namespace BTCPayServer.HostedServices
|
||||||
if (invoice.Status == InvoiceStatus.Complete ||
|
if (invoice.Status == InvoiceStatus.Complete ||
|
||||||
((invoice.Status == InvoiceStatus.Invalid || invoice.Status == InvoiceStatus.Expired) && invoice.MonitoringExpiration < DateTimeOffset.UtcNow))
|
((invoice.Status == InvoiceStatus.Invalid || invoice.Status == InvoiceStatus.Expired) && invoice.MonitoringExpiration < DateTimeOffset.UtcNow))
|
||||||
{
|
{
|
||||||
|
var updateConfirmationCountIfNeeded = invoice
|
||||||
|
.GetPayments()
|
||||||
|
.Select<PaymentEntity, Task>(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<PaymentEntity> { payment });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
await Task.WhenAll(updateConfirmationCountIfNeeded);
|
||||||
|
|
||||||
if (await _InvoiceRepository.RemovePendingInvoice(invoice.Id))
|
if (await _InvoiceRepository.RemovePendingInvoice(invoice.Id))
|
||||||
_EventAggregator.Publish(new InvoiceStopWatchedEvent(invoice.Id));
|
_EventAggregator.Publish(new InvoiceStopWatchedEvent(invoice.Id));
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Reference in a new issue