diff --git a/BTCPayServer.Rating/Providers/CoinmateRateProvider.cs b/BTCPayServer.Rating/Providers/CoinmateRateProvider.cs new file mode 100644 index 000000000..896e98dfa --- /dev/null +++ b/BTCPayServer.Rating/Providers/CoinmateRateProvider.cs @@ -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 GetRatesAsync(CancellationToken cancellationToken) + { + var response = await _httpClient.GetAsync("https://coinmate.io/api/tickerAll", cancellationToken); + response.EnsureSuccessStatusCode(); + var jobj = await response.Content.ReadAsAsync(cancellationToken); + + var data = jobj["data"]; + if (data == null) + { + return []; + } + + var list = new List(); + + foreach (var pairProperty in data.Children()) + { + 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("bid"); + var ask = details.Value("ask"); + + list.Add(new PairRate(new CurrencyPair(baseCurrency, quoteCurrency), new BidAsk(bid, ask))); + } + + return list.ToArray(); + } +} diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 8fd674da0..332172b9c 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -160,7 +160,7 @@ namespace BTCPayServer.Hosting // AddOnchainWalletParsers(services); - + services.AddStartupTask(); services.AddStartupTask(); @@ -597,6 +597,7 @@ o.GetRequiredService>().ToDictionary(o => o.P services.AddRateProvider(); services.AddRateProvider(); services.AddRateProvider(); + services.AddRateProvider(); services.AddSingleton(); services.AddSingleton(o => o.GetRequiredService());