2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2018-08-21 08:59:57 +02:00
|
|
|
using System.Collections.Concurrent;
|
2018-05-04 08:35:39 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-01-18 13:48:04 +01:00
|
|
|
using System.Net.Http;
|
2019-03-05 09:09:17 +01:00
|
|
|
using System.Threading;
|
2018-05-04 08:35:39 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Rating;
|
|
|
|
using ExchangeSharp;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Services.Rates
|
|
|
|
{
|
2020-01-18 13:48:04 +01:00
|
|
|
public class ExchangeSharpRateProvider<T> : IRateProvider where T : ExchangeAPI, new()
|
2018-05-04 08:35:39 +02:00
|
|
|
{
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly HttpClient _httpClient;
|
2020-01-18 13:48:04 +01:00
|
|
|
public ExchangeSharpRateProvider(HttpClient httpClient, bool reverseCurrencyPair = false)
|
2018-05-04 08:35:39 +02:00
|
|
|
{
|
2021-12-28 09:39:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(httpClient);
|
2018-05-04 08:35:39 +02:00
|
|
|
ReverseCurrencyPair = reverseCurrencyPair;
|
2020-01-18 13:48:04 +01:00
|
|
|
_httpClient = httpClient;
|
2018-05-04 08:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool ReverseCurrencyPair
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:11:05 +01:00
|
|
|
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
|
2018-05-04 08:35:39 +02:00
|
|
|
{
|
|
|
|
await new SynchronizationContextRemover();
|
2019-11-11 07:14:29 +01:00
|
|
|
|
2020-01-18 13:48:04 +01:00
|
|
|
var exchangeAPI = new T();
|
|
|
|
exchangeAPI.RequestMaker = new HttpClientRequestMaker(exchangeAPI, _httpClient, cancellationToken);
|
|
|
|
var rates = await exchangeAPI.GetTickersAsync();
|
|
|
|
|
|
|
|
var exchangeRateTasks = rates
|
|
|
|
.Where(t => t.Value.Ask != 0m && t.Value.Bid != 0m)
|
|
|
|
.Select(t => CreateExchangeRate(exchangeAPI, t));
|
|
|
|
|
|
|
|
var exchangeRates = await Task.WhenAll(exchangeRateTasks);
|
2019-11-11 07:14:29 +01:00
|
|
|
|
2020-01-17 10:11:05 +01:00
|
|
|
return exchangeRates
|
2019-11-11 07:14:29 +01:00
|
|
|
.Where(t => t != null)
|
2020-01-17 10:11:05 +01:00
|
|
|
.ToArray();
|
2018-05-04 08:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExchangeSymbolToGlobalSymbol throws exception which would kill perf
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly ConcurrentDictionary<string, string> notFoundSymbols = new ConcurrentDictionary<string, string>();
|
2020-01-18 13:48:04 +01:00
|
|
|
private async Task<PairRate> CreateExchangeRate(T exchangeAPI, KeyValuePair<string, ExchangeTicker> ticker)
|
2018-05-04 08:35:39 +02:00
|
|
|
{
|
2019-11-11 07:14:29 +01:00
|
|
|
if (notFoundSymbols.TryGetValue(ticker.Key, out _))
|
2018-05-04 08:35:39 +02:00
|
|
|
return null;
|
|
|
|
try
|
|
|
|
{
|
2020-01-18 13:48:04 +01:00
|
|
|
var tickerName = await exchangeAPI.ExchangeMarketSymbolToGlobalMarketSymbolAsync(ticker.Key);
|
2018-05-04 08:35:39 +02:00
|
|
|
if (!CurrencyPair.TryParse(tickerName, out var pair))
|
|
|
|
{
|
2018-08-21 08:59:57 +02:00
|
|
|
notFoundSymbols.TryAdd(ticker.Key, ticker.Key);
|
2018-05-04 08:35:39 +02:00
|
|
|
return null;
|
|
|
|
}
|
2020-01-18 13:48:04 +01:00
|
|
|
if (ReverseCurrencyPair)
|
2018-05-04 08:35:39 +02:00
|
|
|
pair = new CurrencyPair(pair.Right, pair.Left);
|
2020-01-17 10:11:05 +01:00
|
|
|
return new PairRate(pair, new BidAsk(ticker.Value.Bid, ticker.Value.Ask));
|
2018-05-04 08:35:39 +02:00
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
2018-08-21 08:59:57 +02:00
|
|
|
notFoundSymbols.TryAdd(ticker.Key, ticker.Key);
|
2018-05-04 08:35:39 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|