diff --git a/BTCPayServer.Rating/Providers/HitBTCRateProvider.cs b/BTCPayServer.Rating/Providers/HitBTCRateProvider.cs new file mode 100644 index 000000000..deaa90a0c --- /dev/null +++ b/BTCPayServer.Rating/Providers/HitBTCRateProvider.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Services.Rates; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Rating +{ + public class HitBTCRateProvider : IRateProvider + { + private readonly HttpClient _httpClient; + public HitBTCRateProvider(HttpClient httpClient) + { + _httpClient = httpClient ?? new HttpClient(); + } + + public async Task GetRatesAsync(CancellationToken cancellationToken) + { + var response = await _httpClient.GetAsync("https://api.hitbtc.com/api/2/public/ticker", cancellationToken); + var jarray = await response.Content.ReadAsAsync(cancellationToken); + return jarray + .Children() + .Where(p => CurrencyPair.TryParse(p["symbol"].Value(), out _)) + .Select(p => new PairRate(CurrencyPair.Parse(p["symbol"].Value()), CreateBidAsk(p))) + .ToArray(); + } + + private BidAsk CreateBidAsk(JObject p) + { + var bid = p["bid"].Value(); + var ask = p["ask"].Value(); + return new BidAsk(bid, ask); + } + } +} diff --git a/BTCPayServer.Rating/Services/RateProviderFactory.cs b/BTCPayServer.Rating/Services/RateProviderFactory.cs index a2671f1ba..60ed0af09 100644 --- a/BTCPayServer.Rating/Services/RateProviderFactory.cs +++ b/BTCPayServer.Rating/Services/RateProviderFactory.cs @@ -90,10 +90,10 @@ namespace BTCPayServer.Services.Rates AddExchangeSharpProviders("binance"); AddExchangeSharpProviders("bittrex"); AddExchangeSharpProviders("poloniex"); - AddExchangeSharpProviders("hitbtc"); AddExchangeSharpProviders("ndax"); // Handmade providers + Providers.Add("hitbtc", new HitBTCRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_HITBTC"))); Providers.Add("coingecko", new CoinGeckoRateProvider(_httpClientFactory)); Providers.Add("kraken", new KrakenExchangeRateProvider() { HttpClient = _httpClientFactory?.CreateClient("EXCHANGE_KRAKEN") }); Providers.Add("bylls", new ByllsRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BYLLS"))); diff --git a/Changelog.md b/Changelog.md index 97917a0bd..70e56f3c1 100644 --- a/Changelog.md +++ b/Changelog.md @@ -11,8 +11,9 @@ ### Bug fixes * Fix some crashes when switching from Altcoins edition to Bitcoin-Only. (@Kukks @NicolasDorier) -* Fix invoices CSV Export formatting introduced in 1.0.5.4 +* Fix invoices CSV Export formatting introduced in 1.0.5.4 (@NicolasDorier) * UI: Fix custom-select glitch (#1822 @DennisReimann) +* Fix crash with hitbtc rate provider (@NicolasDorier) ## 1.0.5.4: