mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-20 02:28:31 +01:00
0a9290a980
Address #1110
52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using NBitcoin;
|
|
using NBXplorer;
|
|
using NBXplorer.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BTCPayServer.Services.Fees
|
|
{
|
|
public class NBXplorerFeeProviderFactory : IFeeProviderFactory
|
|
{
|
|
public NBXplorerFeeProviderFactory(ExplorerClientProvider explorerClients)
|
|
{
|
|
if (explorerClients == null)
|
|
throw new ArgumentNullException(nameof(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)
|
|
{
|
|
if (explorerClient == null)
|
|
throw new ArgumentNullException(nameof(explorerClient));
|
|
_Factory = parent;
|
|
_ExplorerClient = explorerClient;
|
|
}
|
|
NBXplorerFeeProviderFactory _Factory;
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|