2024-04-30 11:31:15 +02:00
|
|
|
#nullable enable
|
2020-05-31 12:18:29 +02:00
|
|
|
using System;
|
2024-04-30 11:31:15 +02:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Services.Rates;
|
2020-05-31 12:18:29 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Rating
|
|
|
|
{
|
|
|
|
public static class Extensions
|
|
|
|
{
|
2024-04-30 11:31:15 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 10:34:09 +09:00
|
|
|
public static decimal RoundToSignificant(this decimal value, int divisibility)
|
|
|
|
{
|
|
|
|
return RoundToSignificant(value, ref divisibility);
|
|
|
|
}
|
2020-05-31 12:18:29 +02:00
|
|
|
public static decimal RoundToSignificant(this decimal value, ref int divisibility)
|
|
|
|
{
|
|
|
|
if (value != 0m)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
var rounded = decimal.Round(value, divisibility, MidpointRounding.AwayFromZero);
|
2023-06-13 20:34:21 +02:00
|
|
|
if ((Math.Abs(rounded - value) / value) < 0.01m)
|
2020-05-31 12:18:29 +02:00
|
|
|
{
|
|
|
|
value = rounded;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
divisibility++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|