btcpayserver/BTCPayServer/Services/Wallets/BTCPayWalletProvider.cs
d11n d5d0be5824
Code formatting updates (#4502)
* Editorconfig: Add space_before_self_closing setting

This was a difference between the way dotnet-format and Rider format code. See https://www.jetbrains.com/help/rider/EditorConfig_Index.html

* Editorconfig: Keep 4 spaces indentation for Swagger JSON files

They are all formatted that way, let's keep it like that.

* Apply dotnet-format, mostly white-space related changes
2023-01-06 22:18:07 +09:00

67 lines
2.6 KiB
C#

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 WalletRepository WalletRepository { get; }
public Logs Logs { get; }
private readonly ExplorerClientProvider _Client;
readonly BTCPayNetworkProvider _NetworkProvider;
readonly IOptions<MemoryCacheOptions> _Options;
public BTCPayWalletProvider(ExplorerClientProvider client,
IOptions<MemoryCacheOptions> memoryCacheOption,
Data.ApplicationDbContextFactory dbContextFactory,
BTCPayNetworkProvider networkProvider,
NBXplorerConnectionFactory nbxplorerConnectionFactory,
WalletRepository walletRepository,
Logs logs)
{
ArgumentNullException.ThrowIfNull(client);
this.Logs = logs;
_Client = client;
_NetworkProvider = networkProvider;
WalletRepository = walletRepository;
_Options = memoryCacheOption;
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);
_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;
}
}
}