add CoinmateRateProvider

This commit is contained in:
patrykdarmofalski
2025-05-13 19:33:29 +02:00
parent 95bef5a7c4
commit 1e6a021b8d
2 changed files with 55 additions and 1 deletions

View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Services.Rates;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Rating.Providers;
public class CoinmateRateProvider : IRateProvider
{
private readonly HttpClient _httpClient;
public CoinmateRateProvider(HttpClient httpClient)
{
_httpClient = httpClient ?? new HttpClient();
}
public RateSourceInfo RateSourceInfo => new("coinmate", "Coinmate", "https://coinmate.io/api/tickerAll");
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
{
var response = await _httpClient.GetAsync("https://coinmate.io/api/tickerAll", cancellationToken);
response.EnsureSuccessStatusCode();
var jobj = await response.Content.ReadAsAsync<JObject>(cancellationToken);
var data = jobj["data"];
if (data == null)
{
return [];
}
var list = new List<PairRate>();
foreach (var pairProperty in data.Children<JProperty>())
{
var pairName = pairProperty.Name;
var pairParts = pairName.Split('_');
if (pairParts.Length != 2)
continue;
var baseCurrency = pairParts[0];
var quoteCurrency = pairParts[1];
var details = pairProperty.Value;
var bid = details.Value<decimal>("bid");
var ask = details.Value<decimal>("ask");
list.Add(new PairRate(new CurrencyPair(baseCurrency, quoteCurrency), new BidAsk(bid, ask)));
}
return list.ToArray();
}
}

View File

@@ -597,6 +597,7 @@ o.GetRequiredService<IEnumerable<IPaymentLinkExtension>>().ToDictionary(o => o.P
services.AddRateProvider<FreeCurrencyRatesRateProvider>();
services.AddRateProvider<BitmyntRateProvider>();
services.AddRateProvider<BareBitcoinRateProvider>();
services.AddRateProvider<CoinmateRateProvider>();
services.AddSingleton<InvoiceBlobMigratorHostedService>();
services.AddSingleton<IHostedService, InvoiceBlobMigratorHostedService>(o => o.GetRequiredService<InvoiceBlobMigratorHostedService>());