btcpayserver/BTCPayServer/Services/Fees/NBxplorerFeeProvider.cs

49 lines
1.6 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05:00
using System;
2017-09-13 15:47:34 +09:00
using System.Threading.Tasks;
2020-06-28 17:55:27 +09:00
using NBitcoin;
using NBXplorer;
using NBXplorer.Models;
2017-09-13 15:47:34 +09:00
namespace BTCPayServer.Services.Fees
2017-09-13 15:47:34 +09:00
{
public class NBXplorerFeeProviderFactory : IFeeProviderFactory
{
public NBXplorerFeeProviderFactory(ExplorerClientProvider explorerClients)
{
ArgumentNullException.ThrowIfNull(explorerClients);
_ExplorerClients = explorerClients;
}
private readonly ExplorerClientProvider _ExplorerClients;
public FeeRate Fallback { get; set; }
public IFeeProvider CreateFeeProvider(BTCPayNetworkBase network)
{
return new NBXplorerFeeProvider(this, _ExplorerClients.GetExplorerClient(network));
}
}
public class NBXplorerFeeProvider : IFeeProvider
{
public NBXplorerFeeProvider(NBXplorerFeeProviderFactory parent, ExplorerClient explorerClient)
{
ArgumentNullException.ThrowIfNull(explorerClient);
_Factory = parent;
_ExplorerClient = explorerClient;
}
readonly NBXplorerFeeProviderFactory _Factory;
readonly ExplorerClient _ExplorerClient;
public async Task<FeeRate> GetFeeRateAsync(int blockTarget = 20)
{
try
{
return (await _ExplorerClient.GetFeeRateAsync(blockTarget).ConfigureAwait(false)).FeeRate;
}
catch (NBXplorerException ex) when (ex.Error.HttpCode == 400 && ex.Error.Code == "fee-estimation-unavailable")
{
return _Factory.Fallback;
}
}
}
2017-09-13 15:47:34 +09:00
}