From 4230ba513fb08bc881fa2b74ae44bcaf45768fb1 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 15 Oct 2021 12:17:28 +0900 Subject: [PATCH] Add support for rpio exchange rate (close #2960) --- .../Providers/RipioExchangeProvider.cs | 42 +++++++++++++++++++ .../Services/RateProviderFactory.cs | 2 + 2 files changed, 44 insertions(+) create mode 100644 BTCPayServer.Rating/Providers/RipioExchangeProvider.cs diff --git a/BTCPayServer.Rating/Providers/RipioExchangeProvider.cs b/BTCPayServer.Rating/Providers/RipioExchangeProvider.cs new file mode 100644 index 000000000..3294f40d6 --- /dev/null +++ b/BTCPayServer.Rating/Providers/RipioExchangeProvider.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Rating; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Services.Rates +{ + public class RipioExchangeProvider : IRateProvider + { + private readonly HttpClient _httpClient; + public RipioExchangeProvider(HttpClient httpClient) + { + _httpClient = httpClient ?? new HttpClient(); + } + public async Task GetRatesAsync(CancellationToken cancellationToken) + { + var response = await _httpClient.GetAsync("https://api.exchange.ripio.com/api/v1/rate/all/", cancellationToken); + var jarray = (JArray)(await response.Content.ReadAsAsync(cancellationToken)); + return jarray + .Children() + .Select(jobj => ParsePair(jobj)) + .Where(p => p != null) + .ToArray(); + } + + private PairRate ParsePair(JObject jobj) + { + var pair = CurrencyPair.Parse(jobj["pair"].Value()); + var bid = decimal.Parse(jobj["bid"].Value(), System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture); + var ask = decimal.Parse(jobj["ask"].Value(), System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture); + if (bid > ask) + return null; + return new PairRate(pair, new BidAsk(bid, ask)); + } + } +} diff --git a/BTCPayServer.Rating/Services/RateProviderFactory.cs b/BTCPayServer.Rating/Services/RateProviderFactory.cs index 40de8081c..f7b4d7d38 100644 --- a/BTCPayServer.Rating/Services/RateProviderFactory.cs +++ b/BTCPayServer.Rating/Services/RateProviderFactory.cs @@ -75,6 +75,7 @@ namespace BTCPayServer.Services.Rates yield return new AvailableRateProvider("bitbank", "Bitbank", "https://public.bitbank.cc/prices"); yield return new AvailableRateProvider("bitflyer", "Bitflyer", "https://api.bitflyer.com/v1/ticker"); yield return new AvailableRateProvider("bitpay", "Bitpay", "https://bitpay.com/rates"); + yield return new AvailableRateProvider("ripio", "Ripio", "https://api.exchange.ripio.com/api/v1/rate/all/"); yield return new AvailableRateProvider("polispay", "PolisPay", "https://obol.polispay.com/complex/btc/polis"); @@ -99,6 +100,7 @@ namespace BTCPayServer.Services.Rates Providers.Add("bylls", new ByllsRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BYLLS"))); Providers.Add("bitbank", new BitbankRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BITBANK"))); Providers.Add("bitpay", new BitpayRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BITPAY"))); + Providers.Add("ripio", new RipioExchangeProvider(_httpClientFactory?.CreateClient("EXCHANGE_RIPIO"))); Providers.Add("bitflyer", new BitflyerRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BITFLYER"))); Providers.Add("polispay", new PolisRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_POLIS"))); // Providers.Add("argoneum", new ArgoneumRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_ARGONEUM")));