using System; using System.Collections.Generic; using System.Linq; using BTCPayServer.Logging; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; namespace BTCPayServer.Services.Wallets { public class BTCPayWalletProvider { public Logs Logs { get; } private readonly ExplorerClientProvider _Client; readonly BTCPayNetworkProvider _NetworkProvider; readonly IOptions _Options; public BTCPayWalletProvider(ExplorerClientProvider client, IOptions memoryCacheOption, Data.ApplicationDbContextFactory dbContextFactory, BTCPayNetworkProvider networkProvider, Logs logs) { if (client == null) throw new ArgumentNullException(nameof(client)); this.Logs = logs; _Client = client; _NetworkProvider = networkProvider; _Options = memoryCacheOption; foreach (var network in networkProvider.GetAll().OfType()) { var explorerClient = _Client.GetExplorerClient(network.CryptoCode); if (explorerClient == null) continue; _Wallets.Add(network.CryptoCode.ToUpperInvariant(), new BTCPayWallet(explorerClient, new MemoryCache(_Options), network, dbContextFactory, Logs)); } } readonly Dictionary _Wallets = new Dictionary(); public BTCPayWallet GetWallet(BTCPayNetworkBase network) { if (network == null) throw new ArgumentNullException(nameof(network)); return GetWallet(network.CryptoCode); } public BTCPayWallet GetWallet(string cryptoCode) { if (cryptoCode == null) throw new ArgumentNullException(nameof(cryptoCode)); _Wallets.TryGetValue(cryptoCode.ToUpperInvariant(), out var result); return result; } public bool IsAvailable(BTCPayNetworkBase network) { return _Client.IsAvailable(network); } public IEnumerable GetWallets() { foreach (var w in _Wallets) yield return w.Value; } } }