From ae10d0c7fd0eeb134b0978ed922c01d12c828751 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Mon, 6 Jun 2022 11:56:50 +0200 Subject: [PATCH] Bump and fix rate providers (#3813) * Bump and fix rate providers * fix --- .../BTCPayServer.Rating.csproj | 2 +- .../Providers/ExchangeSharpRateProvider.cs | 30 +++++++++---------- .../Providers/HttpClientRequestMaker.cs | 8 +++-- .../Providers/KrakenExchangeRateProvider.cs | 10 ++----- .../Services/RateProviderFactory.cs | 4 +-- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/BTCPayServer.Rating/BTCPayServer.Rating.csproj b/BTCPayServer.Rating/BTCPayServer.Rating.csproj index 767fbdb78..d4f73e90b 100644 --- a/BTCPayServer.Rating/BTCPayServer.Rating.csproj +++ b/BTCPayServer.Rating/BTCPayServer.Rating.csproj @@ -8,7 +8,7 @@ - + diff --git a/BTCPayServer.Rating/Providers/ExchangeSharpRateProvider.cs b/BTCPayServer.Rating/Providers/ExchangeSharpRateProvider.cs index 7e9155c3f..de0c91c67 100644 --- a/BTCPayServer.Rating/Providers/ExchangeSharpRateProvider.cs +++ b/BTCPayServer.Rating/Providers/ExchangeSharpRateProvider.cs @@ -10,26 +10,20 @@ using ExchangeSharp; namespace BTCPayServer.Services.Rates { - public class ExchangeSharpRateProvider : IRateProvider where T : ExchangeAPI, new() + public class ExchangeSharpRateProvider : IRateProvider where T : ExchangeAPI { readonly HttpClient _httpClient; - public ExchangeSharpRateProvider(HttpClient httpClient, bool reverseCurrencyPair = false) + public ExchangeSharpRateProvider(HttpClient httpClient) { ArgumentNullException.ThrowIfNull(httpClient); - ReverseCurrencyPair = reverseCurrencyPair; _httpClient = httpClient; } - public bool ReverseCurrencyPair - { - get; set; - } - public async Task GetRatesAsync(CancellationToken cancellationToken) { await new SynchronizationContextRemover(); - var exchangeAPI = new T(); + var exchangeAPI = (T) await ExchangeAPI.GetExchangeAPIAsync(); exchangeAPI.RequestMaker = new HttpClientRequestMaker(exchangeAPI, _httpClient, cancellationToken); var rates = await exchangeAPI.GetTickersAsync(); @@ -52,14 +46,20 @@ namespace BTCPayServer.Services.Rates return null; try { - var tickerName = await exchangeAPI.ExchangeMarketSymbolToGlobalMarketSymbolAsync(ticker.Key); - if (!CurrencyPair.TryParse(tickerName, out var pair)) + CurrencyPair pair; + if (ticker.Value.Volume.BaseCurrency is not null && ticker.Value.Volume.QuoteCurrency is not null) { - notFoundSymbols.TryAdd(ticker.Key, ticker.Key); - return null; + pair = new CurrencyPair(ticker.Value.Volume.BaseCurrency, ticker.Value.Volume.QuoteCurrency); + } + else + { + var tickerName = await exchangeAPI.ExchangeMarketSymbolToGlobalMarketSymbolAsync(ticker.Key); + if (!CurrencyPair.TryParse(tickerName, out pair)) + { + notFoundSymbols.TryAdd(ticker.Key, ticker.Key); + return null; + } } - if (ReverseCurrencyPair) - pair = new CurrencyPair(pair.Right, pair.Left); return new PairRate(pair, new BidAsk(ticker.Value.Bid, ticker.Value.Ask)); } catch (ArgumentException) diff --git a/BTCPayServer.Rating/Providers/HttpClientRequestMaker.cs b/BTCPayServer.Rating/Providers/HttpClientRequestMaker.cs index 95f038984..569f36ed7 100644 --- a/BTCPayServer.Rating/Providers/HttpClientRequestMaker.cs +++ b/BTCPayServer.Rating/Providers/HttpClientRequestMaker.cs @@ -108,7 +108,7 @@ namespace BTCPayServer.Services.Rates set; } - public async Task MakeRequestAsync(string url, string baseUrl = null, Dictionary payload = null, string method = null) + public async Task> MakeRequestAsync(string url, string baseUrl = null, Dictionary payload = null, string method = null) { await default(SynchronizationContextRemover); await api.RateLimit.WaitToProceedAsync(); @@ -170,7 +170,11 @@ namespace BTCPayServer.Services.Rates { response?.Dispose(); } - return responseString; + return new IAPIRequestMaker.RequestResult() + { + Response = responseString, + HTTPHeaderDate = response.Headers.Date + }; } } } diff --git a/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs b/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs index 1e033d3bd..db72fa7a3 100644 --- a/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs +++ b/BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs @@ -16,12 +16,7 @@ namespace BTCPayServer.Services.Rates // Make sure that only one request is sent to kraken in general public class KrakenExchangeRateProvider : IRateProvider { - public KrakenExchangeRateProvider() - { - _Helper = new ExchangeKrakenAPI(); - } - readonly ExchangeKrakenAPI _Helper; public HttpClient HttpClient { get @@ -88,7 +83,8 @@ namespace BTCPayServer.Services.Rates { var result = new List(); var symbols = await GetSymbolsAsync(cancellationToken); - var normalizedPairsList = symbols.Where(s => !notFoundSymbols.ContainsKey(s)).Select(s => _Helper.NormalizeMarketSymbol(s)).ToList(); + var helper = (ExchangeKrakenAPI)await ExchangeAPI.GetExchangeAPIAsync(); + var normalizedPairsList = symbols.Where(s => !notFoundSymbols.ContainsKey(s)).Select(s => helper.NormalizeMarketSymbol(s)).ToList(); var csvPairsList = string.Join(",", normalizedPairsList); JToken apiTickers = await MakeJsonRequestAsync("/0/public/Ticker", null, new Dictionary { { "pair", csvPairsList } }, cancellationToken: cancellationToken); var tickers = new List>(); @@ -111,7 +107,7 @@ namespace BTCPayServer.Services.Rates } else { - global = await _Helper.ExchangeMarketSymbolToGlobalMarketSymbolAsync(symbol); + global = await helper.ExchangeMarketSymbolToGlobalMarketSymbolAsync(symbol); } if (CurrencyPair.TryParse(global, out var pair)) result.Add(new PairRate(pair.Inverse(), new BidAsk(ticker.Bid, ticker.Ask))); diff --git a/BTCPayServer.Rating/Services/RateProviderFactory.cs b/BTCPayServer.Rating/Services/RateProviderFactory.cs index 39e06910f..42debe873 100644 --- a/BTCPayServer.Rating/Services/RateProviderFactory.cs +++ b/BTCPayServer.Rating/Services/RateProviderFactory.cs @@ -141,9 +141,9 @@ namespace BTCPayServer.Services.Rates } } - private IRateProvider AddExchangeSharpProviders(string providerName) where T : ExchangeAPI, new() + private IRateProvider AddExchangeSharpProviders(string providerName) where T : ExchangeAPI { - var provider = new ExchangeSharpRateProvider(_httpClientFactory.CreateClient($"EXCHANGE_{providerName}".ToUpperInvariant()), true); + var provider = new ExchangeSharpRateProvider(_httpClientFactory.CreateClient($"EXCHANGE_{providerName}".ToUpperInvariant())); Providers.Add(providerName, provider); return provider; }