diff --git a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs index c192683e0..b4df6d389 100644 --- a/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs +++ b/BTCPayServer/Payments/Bitcoin/NBXplorerListener.cs @@ -138,9 +138,7 @@ namespace BTCPayServer.Payments.Bitcoin switch (newEvent) { case NBXplorer.Models.NewBlockEvent evt: - await Task.WhenAll((await _InvoiceRepository.GetPendingInvoices()) - .Select(invoiceId => UpdatePaymentStates(wallet, invoiceId)) - .ToArray()); + await UpdatePaymentStates(wallet, await _InvoiceRepository.GetPendingInvoices()); _Aggregator.Publish(new Events.NewBlockEvent() { CryptoCode = evt.CryptoCode }); break; case NBXplorer.Models.NewTransactionEvent evt: @@ -206,11 +204,21 @@ namespace BTCPayServer.Payments.Bitcoin } } + async Task UpdatePaymentStates(BTCPayWallet wallet, string[] invoiceIds) + { + var invoices = await _InvoiceRepository.GetInvoices(invoiceIds); + await Task.WhenAll(invoices.Select(i => UpdatePaymentStates(wallet, i)).ToArray()); + } async Task UpdatePaymentStates(BTCPayWallet wallet, string invoiceId) { var invoice = await _InvoiceRepository.GetInvoice(invoiceId, false); if (invoice == null) return null; + return await UpdatePaymentStates(wallet, invoice); + } + async Task UpdatePaymentStates(BTCPayWallet wallet, InvoiceEntity invoice) + { + List updatedPaymentEntities = new List(); var transactions = await wallet.GetTransactions(invoice.GetAllBitcoinPaymentData() .Select(p => p.Outpoint.Hash) diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 42d23b7d8..d9e704d79 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -467,6 +467,20 @@ retry: var res = await GetInvoiceRaw(id, inludeAddressData); return res == null ? null : ToEntity(res); } + public async Task GetInvoices(string[] invoiceIds) + { + var invoiceIdSet = invoiceIds.ToHashSet(); + using (var context = _ContextFactory.CreateContext()) + { + IQueryable query = + context + .Invoices + .Include(o => o.Payments) + .Where(o => invoiceIdSet.Contains(o.Id)); + + return (await query.ToListAsync()).Select(o => ToEntity(o)).ToArray(); + } + } private async Task GetInvoiceRaw(string id, bool inludeAddressData = false) {