mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 09:54:30 +01:00
6049fa23a7
* Support pluginable rate providers This PR allows plugins to provide custom rate providers, that can be contextual to a store. For example, if you use the upcoming fiat offramp plugin, or the Blink plugin, you'll probably want to configure the fetch the rates from them since they are determining the actual fiat rrate to you. However, they require API keys. This PR enables these scenarios, even much more advanced ones, but for example: * Install fiat offramp plugin * Configure it * You can now use the fiat offramp rate provider (no additional config steps beyond selecting the rate source from the select, or maybe the plugin would automatically set it for you once configured) * Apply suggestions from code review * Simplify * Do not use BackgroundFetcherRateProvider for contextual rate prov --------- Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
47 lines
1.5 KiB
C#
47 lines
1.5 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
namespace BTCPayServer.Rating
|
|
{
|
|
public static class Extensions
|
|
{
|
|
public static Task<PairRate[]> GetRatesAsyncWithMaybeContext(this IRateProvider rateProvider, IRateContext? context, CancellationToken cancellationToken)
|
|
{
|
|
if (rateProvider is IContextualRateProvider contextualRateProvider && context is { })
|
|
{
|
|
return contextualRateProvider.GetRatesAsync(context, cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
return rateProvider.GetRatesAsync(cancellationToken);
|
|
}
|
|
}
|
|
public static decimal RoundToSignificant(this decimal value, int divisibility)
|
|
{
|
|
return RoundToSignificant(value, ref divisibility);
|
|
}
|
|
public static decimal RoundToSignificant(this decimal value, ref int divisibility)
|
|
{
|
|
if (value != 0m)
|
|
{
|
|
while (true)
|
|
{
|
|
var rounded = decimal.Round(value, divisibility, MidpointRounding.AwayFromZero);
|
|
if ((Math.Abs(rounded - value) / value) < 0.01m)
|
|
{
|
|
value = rounded;
|
|
break;
|
|
}
|
|
divisibility++;
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
}
|
|
}
|