2017-09-13 08:47:34 +02:00
|
|
|
|
using NBitcoin;
|
|
|
|
|
using NBXplorer;
|
|
|
|
|
using NBXplorer.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-09-15 09:06:57 +02:00
|
|
|
|
namespace BTCPayServer.Services.Fees
|
2017-09-13 08:47:34 +02:00
|
|
|
|
{
|
2017-12-21 07:52:04 +01:00
|
|
|
|
public class NBXplorerFeeProviderFactory : IFeeProviderFactory
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-07 18:36:41 +01:00
|
|
|
|
public NBXplorerFeeProviderFactory(ExplorerClientProvider explorerClients)
|
2017-12-21 07:52:04 +01:00
|
|
|
|
{
|
2018-01-07 18:36:41 +01:00
|
|
|
|
if (explorerClients == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(explorerClients));
|
|
|
|
|
_ExplorerClients = explorerClients;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-07 18:36:41 +01:00
|
|
|
|
private readonly ExplorerClientProvider _ExplorerClients;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
|
|
|
|
|
public FeeRate Fallback { get; set; }
|
2019-05-29 11:43:50 +02:00
|
|
|
|
public IFeeProvider CreateFeeProvider(BTCPayNetworkBase network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-07 18:36:41 +01:00
|
|
|
|
return new NBXplorerFeeProvider(this, _ExplorerClients.GetExplorerClient(network));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-12-21 07:52:04 +01:00
|
|
|
|
}
|
|
|
|
|
public class NBXplorerFeeProvider : IFeeProvider
|
|
|
|
|
{
|
2018-01-07 18:36:41 +01:00
|
|
|
|
public NBXplorerFeeProvider(NBXplorerFeeProviderFactory parent, ExplorerClient explorerClient)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-07 18:36:41 +01:00
|
|
|
|
if (explorerClient == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(explorerClient));
|
|
|
|
|
_Factory = parent;
|
|
|
|
|
_ExplorerClient = explorerClient;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2018-01-07 18:36:41 +01:00
|
|
|
|
NBXplorerFeeProviderFactory _Factory;
|
|
|
|
|
ExplorerClient _ExplorerClient;
|
2019-11-07 01:21:33 +01:00
|
|
|
|
public async Task<FeeRate> GetFeeRateAsync(int blockTarget = 20)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-11-07 01:21:33 +01:00
|
|
|
|
return (await _ExplorerClient.GetFeeRateAsync(blockTarget).ConfigureAwait(false)).FeeRate;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
catch (NBXplorerException ex) when (ex.Error.HttpCode == 400 && ex.Error.Code == "fee-estimation-unavailable")
|
|
|
|
|
{
|
2017-12-21 07:52:04 +01:00
|
|
|
|
return _Factory.Fallback;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|