mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-20 10:40:29 +01:00
simplify code
This commit is contained in:
parent
10be8aec82
commit
5e452a679e
@ -33,10 +33,12 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
private TaskCompletionSource<bool> _RunningTask;
|
private TaskCompletionSource<bool> _RunningTask;
|
||||||
private CancellationTokenSource _Cts;
|
private CancellationTokenSource _Cts;
|
||||||
BTCPayWalletProvider _Wallets;
|
BTCPayWalletProvider _Wallets;
|
||||||
|
BTCPayNetworkProvider _NetworkProvider;
|
||||||
|
|
||||||
public NBXplorerListener(ExplorerClientProvider explorerClients,
|
public NBXplorerListener(ExplorerClientProvider explorerClients,
|
||||||
BTCPayWalletProvider wallets,
|
BTCPayWalletProvider wallets,
|
||||||
InvoiceRepository invoiceRepository,
|
InvoiceRepository invoiceRepository,
|
||||||
|
BTCPayNetworkProvider networkProvider,
|
||||||
EventAggregator aggregator, IApplicationLifetime lifetime)
|
EventAggregator aggregator, IApplicationLifetime lifetime)
|
||||||
{
|
{
|
||||||
PollInterval = TimeSpan.FromMinutes(1.0);
|
PollInterval = TimeSpan.FromMinutes(1.0);
|
||||||
@ -45,10 +47,11 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
_ExplorerClients = explorerClients;
|
_ExplorerClients = explorerClients;
|
||||||
_Aggregator = aggregator;
|
_Aggregator = aggregator;
|
||||||
_Lifetime = lifetime;
|
_Lifetime = lifetime;
|
||||||
|
_NetworkProvider = networkProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
CompositeDisposable leases = new CompositeDisposable();
|
CompositeDisposable leases = new CompositeDisposable();
|
||||||
ConcurrentDictionary<string, NotificationSession> _Sessions = new ConcurrentDictionary<string, NotificationSession>();
|
ConcurrentDictionary<string, NotificationSession> _SessionsByCryptoCode = new ConcurrentDictionary<string, NotificationSession>();
|
||||||
private Timer _ListenPoller;
|
private Timer _ListenPoller;
|
||||||
|
|
||||||
TimeSpan _PollInterval;
|
TimeSpan _PollInterval;
|
||||||
@ -101,16 +104,12 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
if (inv.Name == "invoice_created")
|
if (inv.Name == "invoice_created")
|
||||||
{
|
{
|
||||||
var invoice = await _InvoiceRepository.GetInvoice(null, inv.InvoiceId);
|
var invoice = await _InvoiceRepository.GetInvoice(null, inv.InvoiceId);
|
||||||
List<Task> listeningDerivations = new List<Task>();
|
await Task.WhenAll(invoice.GetSupportedPaymentMethod<DerivationStrategy>(_NetworkProvider)
|
||||||
foreach (var notificationSessions in _Sessions)
|
.Select(s => (Session: _SessionsByCryptoCode.TryGet(s.PaymentId.CryptoCode),
|
||||||
{
|
DerivationStrategy: s.DerivationStrategyBase))
|
||||||
var derivationStrategy = GetStrategy(notificationSessions.Key, invoice);
|
.Where(s => s.Session != null)
|
||||||
if (derivationStrategy != null)
|
.Select(s => s.Session.ListenDerivationSchemesAsync(new[] { s.DerivationStrategy }))
|
||||||
{
|
.ToArray()).ConfigureAwait(false);
|
||||||
listeningDerivations.Add(notificationSessions.Value.ListenDerivationSchemesAsync(new[] { derivationStrategy }, _Cts.Token));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await Task.WhenAll(listeningDerivations.ToArray()).ConfigureAwait(false);
|
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@ -122,7 +121,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
bool cleanup = false;
|
bool cleanup = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (_Sessions.ContainsKey(network.CryptoCode))
|
if (_SessionsByCryptoCode.ContainsKey(network.CryptoCode))
|
||||||
return;
|
return;
|
||||||
var client = _ExplorerClients.GetExplorerClient(network);
|
var client = _ExplorerClients.GetExplorerClient(network);
|
||||||
if (client == null)
|
if (client == null)
|
||||||
@ -130,7 +129,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
if (_Cts.IsCancellationRequested)
|
if (_Cts.IsCancellationRequested)
|
||||||
return;
|
return;
|
||||||
var session = await client.CreateNotificationSessionAsync(_Cts.Token).ConfigureAwait(false);
|
var session = await client.CreateNotificationSessionAsync(_Cts.Token).ConfigureAwait(false);
|
||||||
if (!_Sessions.TryAdd(network.CryptoCode, session))
|
if (!_SessionsByCryptoCode.TryAdd(network.CryptoCode, session))
|
||||||
{
|
{
|
||||||
await session.DisposeAsync();
|
await session.DisposeAsync();
|
||||||
return;
|
return;
|
||||||
@ -202,8 +201,8 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
if (cleanup)
|
if (cleanup)
|
||||||
{
|
{
|
||||||
Logs.PayServer.LogInformation($"Disconnected from WebSocket of NBXplorer ({network.CryptoCode})");
|
Logs.PayServer.LogInformation($"Disconnected from WebSocket of NBXplorer ({network.CryptoCode})");
|
||||||
_Sessions.TryRemove(network.CryptoCode, out NotificationSession unused);
|
_SessionsByCryptoCode.TryRemove(network.CryptoCode, out NotificationSession unused);
|
||||||
if (_Sessions.Count == 0 && _Cts.IsCancellationRequested)
|
if (_SessionsByCryptoCode.Count == 0 && _Cts.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
_RunningTask.TrySetResult(true);
|
_RunningTask.TrySetResult(true);
|
||||||
}
|
}
|
||||||
@ -228,7 +227,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
var conflicts = GetConflicts(transactions.Select(t => t.Value));
|
var conflicts = GetConflicts(transactions.Select(t => t.Value));
|
||||||
foreach (var payment in invoice.GetPayments(wallet.Network))
|
foreach (var payment in invoice.GetPayments(wallet.Network))
|
||||||
{
|
{
|
||||||
if (payment.GetpaymentMethodId().PaymentType != PaymentTypes.BTCLike)
|
if (payment.GetpaymentMethodId().PaymentType != PaymentTypes.BTCLike)
|
||||||
continue;
|
continue;
|
||||||
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
|
var paymentData = (BitcoinLikePaymentData)payment.GetCryptoPaymentData();
|
||||||
if (!transactions.TryGetValue(paymentData.Outpoint.Hash, out TransactionResult tx))
|
if (!transactions.TryGetValue(paymentData.Outpoint.Hash, out TransactionResult tx))
|
||||||
@ -246,7 +245,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
|
|
||||||
if (paymentData.ConfirmationCount != tx.Confirmations)
|
if (paymentData.ConfirmationCount != tx.Confirmations)
|
||||||
{
|
{
|
||||||
if(wallet.Network.MaxTrackedConfirmation >= paymentData.ConfirmationCount)
|
if (wallet.Network.MaxTrackedConfirmation >= paymentData.ConfirmationCount)
|
||||||
{
|
{
|
||||||
paymentData.ConfirmationCount = tx.Confirmations;
|
paymentData.ConfirmationCount = tx.Confirmations;
|
||||||
payment.SetCryptoPaymentData(paymentData);
|
payment.SetCryptoPaymentData(paymentData);
|
||||||
@ -354,9 +353,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
|
|
||||||
private DerivationStrategyBase GetDerivationStrategy(InvoiceEntity invoice, BTCPayNetwork network)
|
private DerivationStrategyBase GetDerivationStrategy(InvoiceEntity invoice, BTCPayNetwork network)
|
||||||
{
|
{
|
||||||
return invoice.GetSupportedPaymentMethod(_ExplorerClients.NetworkProviders)
|
return invoice.GetSupportedPaymentMethod<DerivationStrategy>(new PaymentMethodId(network.CryptoCode, PaymentTypes.BTCLike), _ExplorerClients.NetworkProviders)
|
||||||
.OfType<DerivationStrategy>()
|
|
||||||
.Where(d => d.Network.CryptoCode == network.CryptoCode)
|
|
||||||
.Select(d => d.DerivationStrategyBase)
|
.Select(d => d.DerivationStrategyBase)
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
@ -389,7 +386,7 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
foreach (var invoiceId in await _InvoiceRepository.GetPendingInvoices())
|
foreach (var invoiceId in await _InvoiceRepository.GetPendingInvoices())
|
||||||
{
|
{
|
||||||
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
|
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
|
||||||
var strategy = GetStrategy(network.CryptoCode, invoice);
|
var strategy = GetDerivationStrategy(invoice, network);
|
||||||
if (strategy != null)
|
if (strategy != null)
|
||||||
strategies.Add(strategy);
|
strategies.Add(strategy);
|
||||||
}
|
}
|
||||||
@ -397,19 +394,6 @@ namespace BTCPayServer.Payments.Bitcoin
|
|||||||
return strategies;
|
return strategies;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DerivationStrategyBase GetStrategy(string cryptoCode, InvoiceEntity invoice)
|
|
||||||
{
|
|
||||||
foreach (var derivationStrategy in invoice.GetSupportedPaymentMethod(_ExplorerClients.NetworkProviders)
|
|
||||||
.OfType<DerivationStrategy>())
|
|
||||||
{
|
|
||||||
if (derivationStrategy.Network.CryptoCode == cryptoCode)
|
|
||||||
{
|
|
||||||
return derivationStrategy.DerivationStrategyBase;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
leases.Dispose();
|
leases.Dispose();
|
||||||
|
@ -168,7 +168,17 @@ namespace BTCPayServer.Services.Invoices
|
|||||||
get;
|
get;
|
||||||
set;
|
set;
|
||||||
}
|
}
|
||||||
|
public IEnumerable<T> GetSupportedPaymentMethod<T>(PaymentMethodId paymentMethodId, BTCPayNetworkProvider networks) where T : ISupportedPaymentMethod
|
||||||
|
{
|
||||||
|
return
|
||||||
|
GetSupportedPaymentMethod(networks)
|
||||||
|
.Where(p => paymentMethodId == null || p.PaymentId == paymentMethodId)
|
||||||
|
.OfType<T>();
|
||||||
|
}
|
||||||
|
public IEnumerable<T> GetSupportedPaymentMethod<T>(BTCPayNetworkProvider networks) where T : ISupportedPaymentMethod
|
||||||
|
{
|
||||||
|
return GetSupportedPaymentMethod<T>(null, networks);
|
||||||
|
}
|
||||||
public IEnumerable<ISupportedPaymentMethod> GetSupportedPaymentMethod(BTCPayNetworkProvider networks)
|
public IEnumerable<ISupportedPaymentMethod> GetSupportedPaymentMethod(BTCPayNetworkProvider networks)
|
||||||
{
|
{
|
||||||
#pragma warning disable CS0618
|
#pragma warning disable CS0618
|
||||||
|
Loading…
Reference in New Issue
Block a user