mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 22:25:28 +01:00
Bump and fix rate providers (#3813)
* Bump and fix rate providers * fix
This commit is contained in:
parent
a443426d83
commit
ae10d0c7fd
5 changed files with 27 additions and 27 deletions
|
@ -8,7 +8,7 @@
|
||||||
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
|
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
|
||||||
<PackageReference Include="NBitcoin" Version="7.0.1" />
|
<PackageReference Include="NBitcoin" Version="7.0.1" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.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>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -10,26 +10,20 @@ using ExchangeSharp;
|
||||||
|
|
||||||
namespace BTCPayServer.Services.Rates
|
namespace BTCPayServer.Services.Rates
|
||||||
{
|
{
|
||||||
public class ExchangeSharpRateProvider<T> : IRateProvider where T : ExchangeAPI, new()
|
public class ExchangeSharpRateProvider<T> : IRateProvider where T : ExchangeAPI
|
||||||
{
|
{
|
||||||
readonly HttpClient _httpClient;
|
readonly HttpClient _httpClient;
|
||||||
public ExchangeSharpRateProvider(HttpClient httpClient, bool reverseCurrencyPair = false)
|
public ExchangeSharpRateProvider(HttpClient httpClient)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(httpClient);
|
ArgumentNullException.ThrowIfNull(httpClient);
|
||||||
ReverseCurrencyPair = reverseCurrencyPair;
|
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ReverseCurrencyPair
|
|
||||||
{
|
|
||||||
get; set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
|
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await new SynchronizationContextRemover();
|
await new SynchronizationContextRemover();
|
||||||
|
|
||||||
var exchangeAPI = new T();
|
var exchangeAPI = (T) await ExchangeAPI.GetExchangeAPIAsync<T>();
|
||||||
exchangeAPI.RequestMaker = new HttpClientRequestMaker(exchangeAPI, _httpClient, cancellationToken);
|
exchangeAPI.RequestMaker = new HttpClientRequestMaker(exchangeAPI, _httpClient, cancellationToken);
|
||||||
var rates = await exchangeAPI.GetTickersAsync();
|
var rates = await exchangeAPI.GetTickersAsync();
|
||||||
|
|
||||||
|
@ -51,15 +45,21 @@ namespace BTCPayServer.Services.Rates
|
||||||
if (notFoundSymbols.TryGetValue(ticker.Key, out _))
|
if (notFoundSymbols.TryGetValue(ticker.Key, out _))
|
||||||
return null;
|
return null;
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
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
|
||||||
{
|
{
|
||||||
var tickerName = await exchangeAPI.ExchangeMarketSymbolToGlobalMarketSymbolAsync(ticker.Key);
|
var tickerName = await exchangeAPI.ExchangeMarketSymbolToGlobalMarketSymbolAsync(ticker.Key);
|
||||||
if (!CurrencyPair.TryParse(tickerName, out var pair))
|
if (!CurrencyPair.TryParse(tickerName, out pair))
|
||||||
{
|
{
|
||||||
notFoundSymbols.TryAdd(ticker.Key, ticker.Key);
|
notFoundSymbols.TryAdd(ticker.Key, ticker.Key);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (ReverseCurrencyPair)
|
}
|
||||||
pair = new CurrencyPair(pair.Right, pair.Left);
|
|
||||||
return new PairRate(pair, new BidAsk(ticker.Value.Bid, ticker.Value.Ask));
|
return new PairRate(pair, new BidAsk(ticker.Value.Bid, ticker.Value.Ask));
|
||||||
}
|
}
|
||||||
catch (ArgumentException)
|
catch (ArgumentException)
|
||||||
|
|
|
@ -108,7 +108,7 @@ namespace BTCPayServer.Services.Rates
|
||||||
set;
|
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 default(SynchronizationContextRemover);
|
||||||
await api.RateLimit.WaitToProceedAsync();
|
await api.RateLimit.WaitToProceedAsync();
|
||||||
|
@ -170,7 +170,11 @@ namespace BTCPayServer.Services.Rates
|
||||||
{
|
{
|
||||||
response?.Dispose();
|
response?.Dispose();
|
||||||
}
|
}
|
||||||
return responseString;
|
return new IAPIRequestMaker.RequestResult<string>()
|
||||||
|
{
|
||||||
|
Response = responseString,
|
||||||
|
HTTPHeaderDate = response.Headers.Date
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,12 +16,7 @@ namespace BTCPayServer.Services.Rates
|
||||||
// Make sure that only one request is sent to kraken in general
|
// Make sure that only one request is sent to kraken in general
|
||||||
public class KrakenExchangeRateProvider : IRateProvider
|
public class KrakenExchangeRateProvider : IRateProvider
|
||||||
{
|
{
|
||||||
public KrakenExchangeRateProvider()
|
|
||||||
{
|
|
||||||
_Helper = new ExchangeKrakenAPI();
|
|
||||||
}
|
|
||||||
|
|
||||||
readonly ExchangeKrakenAPI _Helper;
|
|
||||||
public HttpClient HttpClient
|
public HttpClient HttpClient
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -88,7 +83,8 @@ namespace BTCPayServer.Services.Rates
|
||||||
{
|
{
|
||||||
var result = new List<PairRate>();
|
var result = new List<PairRate>();
|
||||||
var symbols = await GetSymbolsAsync(cancellationToken);
|
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);
|
var csvPairsList = string.Join(",", normalizedPairsList);
|
||||||
JToken apiTickers = await MakeJsonRequestAsync<JToken>("/0/public/Ticker", null, new Dictionary<string, object> { { "pair", csvPairsList } }, cancellationToken: cancellationToken);
|
JToken apiTickers = await MakeJsonRequestAsync<JToken>("/0/public/Ticker", null, new Dictionary<string, object> { { "pair", csvPairsList } }, cancellationToken: cancellationToken);
|
||||||
var tickers = new List<KeyValuePair<string, ExchangeTicker>>();
|
var tickers = new List<KeyValuePair<string, ExchangeTicker>>();
|
||||||
|
@ -111,7 +107,7 @@ namespace BTCPayServer.Services.Rates
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
global = await _Helper.ExchangeMarketSymbolToGlobalMarketSymbolAsync(symbol);
|
global = await helper.ExchangeMarketSymbolToGlobalMarketSymbolAsync(symbol);
|
||||||
}
|
}
|
||||||
if (CurrencyPair.TryParse(global, out var pair))
|
if (CurrencyPair.TryParse(global, out var pair))
|
||||||
result.Add(new PairRate(pair.Inverse(), new BidAsk(ticker.Bid, ticker.Ask)));
|
result.Add(new PairRate(pair.Inverse(), new BidAsk(ticker.Bid, ticker.Ask)));
|
||||||
|
|
|
@ -141,9 +141,9 @@ namespace BTCPayServer.Services.Rates
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
Providers.Add(providerName, provider);
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue