btcpayserver/BTCPayServer/Services/Wallets/BTCPayWalletProvider.cs

68 lines
2.6 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
2021-11-22 17:16:08 +09:00
using BTCPayServer.Logging;
using Microsoft.Extensions.Caching.Memory;
2018-02-15 13:02:12 +09:00
using Microsoft.Extensions.Options;
namespace BTCPayServer.Services.Wallets
{
public class BTCPayWalletProvider
{
public WalletRepository WalletRepository { get; }
2021-11-22 17:16:08 +09:00
public Logs Logs { get; }
private readonly ExplorerClientProvider _Client;
readonly BTCPayNetworkProvider _NetworkProvider;
readonly IOptions<MemoryCacheOptions> _Options;
2018-01-11 21:01:00 +09:00
public BTCPayWalletProvider(ExplorerClientProvider client,
2018-02-15 13:02:12 +09:00
IOptions<MemoryCacheOptions> memoryCacheOption,
2020-04-16 14:25:52 +09:00
Data.ApplicationDbContextFactory dbContextFactory,
2021-11-22 17:16:08 +09:00
BTCPayNetworkProvider networkProvider,
NBXplorerConnectionFactory nbxplorerConnectionFactory,
WalletRepository walletRepository,
2021-11-22 17:16:08 +09:00
Logs logs)
{
ArgumentNullException.ThrowIfNull(client);
2021-11-22 17:16:08 +09:00
this.Logs = logs;
_Client = client;
_NetworkProvider = networkProvider;
WalletRepository = walletRepository;
2018-02-15 13:02:12 +09:00
_Options = memoryCacheOption;
2020-06-28 17:55:27 +09:00
foreach (var network in networkProvider.GetAll().OfType<BTCPayNetwork>())
{
var explorerClient = _Client.GetExplorerClient(network.CryptoCode);
if (explorerClient == null)
continue;
_Wallets.Add(network.CryptoCode.ToUpperInvariant(), new BTCPayWallet(explorerClient, new MemoryCache(_Options), network, WalletRepository, dbContextFactory, nbxplorerConnectionFactory, Logs));
}
}
readonly Dictionary<string, BTCPayWallet> _Wallets = new Dictionary<string, BTCPayWallet>();
public BTCPayWallet GetWallet(BTCPayNetworkBase network)
{
ArgumentNullException.ThrowIfNull(network);
return GetWallet(network.CryptoCode);
}
public BTCPayWallet GetWallet(string cryptoCode)
{
ArgumentNullException.ThrowIfNull(cryptoCode);
2019-12-24 08:20:44 +01:00
_Wallets.TryGetValue(cryptoCode.ToUpperInvariant(), out var result);
return result;
}
public bool IsAvailable(BTCPayNetworkBase network)
{
return _Client.IsAvailable(network);
}
public IEnumerable<BTCPayWallet> GetWallets()
{
foreach (var w in _Wallets)
yield return w.Value;
}
}
}