btcpayserver/BTCPayServer/Payments/PayJoin/PayjoinReceiverContext.cs
Andrew Camilleri bec888da19
Payjoin label fixes (#3986)
* Payjoin label fixes

* When a payjoin label was applied, coin selection filter would not work
* When a payjoin happened with a receive address wallet, the payjoin label was not applied
* Coin selection shows when a utxo is currently reserved for a payjoin. Applies both to UI and to GF API

* remove reserved label

* Update BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs
2022-07-23 20:26:13 +09:00

68 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BTCPayServer.Logging;
using BTCPayServer.Services.Invoices;
using Microsoft.Extensions.Logging;
using NBitcoin;
using NBitpayClient;
using NBXplorer;
namespace BTCPayServer.Payments.PayJoin
{
public class PayjoinReceiverContext
{
private readonly InvoiceRepository _invoiceRepository;
private readonly ExplorerClient _explorerClient;
private readonly UTXOLocker _utxoLocker;
private readonly BTCPayServer.Logging.Logs BTCPayLogs;
public PayjoinReceiverContext(InvoiceRepository invoiceRepository, ExplorerClient explorerClient, UTXOLocker utxoLocker, BTCPayServer.Logging.Logs logs)
{
this.BTCPayLogs = logs;
_invoiceRepository = invoiceRepository;
_explorerClient = explorerClient;
_utxoLocker = utxoLocker;
}
public Invoice Invoice { get; set; }
public NBitcoin.Transaction OriginalTransaction { get; set; }
public InvoiceLogs Logs { get; } = new InvoiceLogs();
public OutPoint[] LockedUTXOs { get; set; }
public async Task DisposeAsync()
{
List<Task> disposing = new List<Task>();
if (Invoice != null)
{
disposing.Add(_invoiceRepository.AddInvoiceLogs(Invoice.Id, Logs));
}
if (!doNotBroadcast && OriginalTransaction != null)
{
disposing.Add(_explorerClient.BroadcastAsync(OriginalTransaction));
}
if (!success && LockedUTXOs != null)
{
disposing.Add(_utxoLocker.TryUnlock(LockedUTXOs));
}
try
{
await Task.WhenAll(disposing);
}
catch (Exception ex)
{
BTCPayLogs.PayServer.LogWarning(ex, "Error while disposing the PayjoinReceiverContext");
}
}
bool doNotBroadcast = false;
public void DoNotBroadcast()
{
doNotBroadcast = true;
}
bool success = false;
public void Success()
{
doNotBroadcast = true;
success = true;
}
}
}