From dc3abc76c3263fcbc78872d68039b8260fa993a5 Mon Sep 17 00:00:00 2001 From: Kukks Date: Fri, 6 Sep 2019 08:02:26 +0200 Subject: [PATCH] add Ndax rate provider --- .../Providers/ByllsRateProvider.cs | 2 -- .../Providers/NdaxRateProvider.cs | 36 +++++++++++++++++++ .../Services/RateProviderFactory.cs | 2 ++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 BTCPayServer.Rating/Providers/NdaxRateProvider.cs diff --git a/BTCPayServer.Rating/Providers/ByllsRateProvider.cs b/BTCPayServer.Rating/Providers/ByllsRateProvider.cs index e47aac21f..23a822ed2 100644 --- a/BTCPayServer.Rating/Providers/ByllsRateProvider.cs +++ b/BTCPayServer.Rating/Providers/ByllsRateProvider.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; diff --git a/BTCPayServer.Rating/Providers/NdaxRateProvider.cs b/BTCPayServer.Rating/Providers/NdaxRateProvider.cs new file mode 100644 index 000000000..1b8e27a62 --- /dev/null +++ b/BTCPayServer.Rating/Providers/NdaxRateProvider.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Rating; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Services.Rates +{ + public class NdaxRateProvider : IRateProvider, IHasExchangeName + { + private readonly HttpClient _httpClient; + + public NdaxRateProvider(HttpClient httpClient) + { + _httpClient = httpClient ?? new HttpClient(); + } + + public string ExchangeName => "ndax"; + + public async Task GetRatesAsync(CancellationToken cancellationToken) + { + var response = await _httpClient.GetAsync("https://ndax.io/api/returnTicker", cancellationToken); + var jobj = await response.Content.ReadAsAsync>(cancellationToken); + return new ExchangeRates(jobj.Select(pair => new ExchangeRate(ExchangeName, CurrencyPair.Parse(pair.Key), + new BidAsk(GetValue(pair.Value["highestBid"]), GetValue(pair.Value["lowestAsk"]))))); + } + + private static decimal GetValue(JToken jobj) + { + return string.IsNullOrEmpty(jobj.ToString()) ? 0 : jobj.Value(); + } + + } +} diff --git a/BTCPayServer.Rating/Services/RateProviderFactory.cs b/BTCPayServer.Rating/Services/RateProviderFactory.cs index b58a1df88..a693d7046 100644 --- a/BTCPayServer.Rating/Services/RateProviderFactory.cs +++ b/BTCPayServer.Rating/Services/RateProviderFactory.cs @@ -113,6 +113,7 @@ namespace BTCPayServer.Services.Rates Providers.Add(CoinAverageRateProvider.CoinAverageName, new CoinAverageRateProvider() { Exchange = CoinAverageRateProvider.CoinAverageName, HttpClient = _httpClientFactory?.CreateClient("EXCHANGE_COINAVERAGE"), Authenticator = _CoinAverageSettings }); Providers.Add("kraken", new KrakenExchangeRateProvider() { HttpClient = _httpClientFactory?.CreateClient("EXCHANGE_KRAKEN") }); Providers.Add("bylls", new ByllsRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BYLLS"))); + Providers.Add("ndax", new NdaxRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_NDAX"))); Providers.Add("bitbank", new BitbankRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BITBANK"))); Providers.Add("bitpay", new BitpayRateProvider(_httpClientFactory?.CreateClient("EXCHANGE_BITPAY"))); @@ -171,6 +172,7 @@ namespace BTCPayServer.Services.Rates // Add other exchanges supported here exchanges.Add(new CoinAverageExchange(CoinAverageRateProvider.CoinAverageName, "Coin Average", $"https://apiv2.bitcoinaverage.com/indices/global/ticker/short")); exchanges.Add(new CoinAverageExchange("bylls", "Bylls", "https://bylls.com/api/price?from_currency=BTC&to_currency=CAD")); + exchanges.Add(new CoinAverageExchange("ndax", "NDAX", "https://ndax.io/api/returnTicker")); exchanges.Add(new CoinAverageExchange("bitbank", "Bitbank", "https://public.bitbank.cc/prices")); return exchanges;