Skip to content

Commit

Permalink
Bump and fix rate providers (btcpayserver#3813)
Browse files Browse the repository at this point in the history
* Bump and fix rate providers

* fix
  • Loading branch information
Kukks authored Jun 6, 2022
1 parent a443426 commit ae10d0c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion BTCPayServer.Rating/BTCPayServer.Rating.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="NBitcoin" Version="7.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="DigitalRuby.ExchangeSharp" Version="0.6.3" />
<PackageReference Include="DigitalRuby.ExchangeSharp" Version="1.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 15 additions & 15 deletions BTCPayServer.Rating/Providers/ExchangeSharpRateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,20 @@

namespace BTCPayServer.Services.Rates
{
public class ExchangeSharpRateProvider<T> : IRateProvider where T : ExchangeAPI, new()
public class ExchangeSharpRateProvider<T> : 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<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
{
await new SynchronizationContextRemover();

var exchangeAPI = new T();
var exchangeAPI = (T) await ExchangeAPI.GetExchangeAPIAsync<T>();
exchangeAPI.RequestMaker = new HttpClientRequestMaker(exchangeAPI, _httpClient, cancellationToken);
var rates = await exchangeAPI.GetTickersAsync();

Expand All @@ -52,14 +46,20 @@ private async Task<PairRate> CreateExchangeRate(T exchangeAPI, KeyValuePair<stri
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)
{
pair = new CurrencyPair(ticker.Value.Volume.BaseCurrency, ticker.Value.Volume.QuoteCurrency);
}
else
{
notFoundSymbols.TryAdd(ticker.Key, ticker.Key);
return null;
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)
Expand Down
8 changes: 6 additions & 2 deletions BTCPayServer.Rating/Providers/HttpClientRequestMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Action<IAPIRequestMaker, RequestMakerState, object> RequestStateChanged
set;
}

public async Task<string> MakeRequestAsync(string url, string baseUrl = null, Dictionary<string, object> payload = null, string method = null)
public async Task<IAPIRequestMaker.RequestResult<string>> MakeRequestAsync(string url, string baseUrl = null, Dictionary<string, object> payload = null, string method = null)
{
await default(SynchronizationContextRemover);
await api.RateLimit.WaitToProceedAsync();
Expand Down Expand Up @@ -170,7 +170,11 @@ public async Task<string> MakeRequestAsync(string url, string baseUrl = null, Di
{
response?.Dispose();
}
return responseString;
return new IAPIRequestMaker.RequestResult<string>()
{
Response = responseString,
HTTPHeaderDate = response.Headers.Date
};
}
}
}
10 changes: 3 additions & 7 deletions BTCPayServer.Rating/Providers/KrakenExchangeRateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,7 +83,8 @@ public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
{
var result = new List<PairRate>();
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<ExchangeKrakenAPI>();
var normalizedPairsList = symbols.Where(s => !notFoundSymbols.ContainsKey(s)).Select(s => helper.NormalizeMarketSymbol(s)).ToList();
var csvPairsList = string.Join(",", normalizedPairsList);
JToken apiTickers = await MakeJsonRequestAsync<JToken>("/0/public/Ticker", null, new Dictionary<string, object> { { "pair", csvPairsList } }, cancellationToken: cancellationToken);
var tickers = new List<KeyValuePair<string, ExchangeTicker>>();
Expand All @@ -111,7 +107,7 @@ public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
}
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)));
Expand Down
4 changes: 2 additions & 2 deletions BTCPayServer.Rating/Services/RateProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ void InitExchanges()
}
}

private IRateProvider AddExchangeSharpProviders<T>(string providerName) where T : ExchangeAPI, new()
private IRateProvider AddExchangeSharpProviders<T>(string providerName) where T : ExchangeAPI
{
var provider = new ExchangeSharpRateProvider<T>(_httpClientFactory.CreateClient($"EXCHANGE_{providerName}".ToUpperInvariant()), true);
var provider = new ExchangeSharpRateProvider<T>(_httpClientFactory.CreateClient($"EXCHANGE_{providerName}".ToUpperInvariant()));
Providers.Add(providerName, provider);
return provider;
}
Expand Down

0 comments on commit ae10d0c

Please sign in to comment.