2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2018-08-21 15:59:57 +09:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text;
|
2019-03-05 17:09:17 +09:00
|
|
|
using System.Threading;
|
2018-08-21 15:59:57 +09:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Rating;
|
|
|
|
using ExchangeSharp;
|
2024-02-01 10:09:32 +09:00
|
|
|
using Microsoft.CodeAnalysis;
|
2018-08-21 15:59:57 +09:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Services.Rates
|
|
|
|
{
|
|
|
|
// Make sure that only one request is sent to kraken in general
|
2020-01-17 18:11:05 +09:00
|
|
|
public class KrakenExchangeRateProvider : IRateProvider
|
2018-08-21 15:59:57 +09:00
|
|
|
{
|
2024-02-01 10:09:32 +09:00
|
|
|
public RateSourceInfo RateSourceInfo => new("kraken", "Kraken", "https://api.kraken.com/0/public/Ticker");
|
2018-08-21 15:59:57 +09:00
|
|
|
public HttpClient HttpClient
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _LocalClient ?? _Client;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2019-03-07 19:39:43 +09:00
|
|
|
_LocalClient = value;
|
2018-08-21 15:59:57 +09:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 15:09:42 +09:00
|
|
|
|
2018-08-21 15:59:57 +09:00
|
|
|
HttpClient _LocalClient;
|
2020-06-28 22:07:48 -05:00
|
|
|
static readonly HttpClient _Client = new HttpClient();
|
2018-08-21 15:59:57 +09:00
|
|
|
string[] _Symbols = Array.Empty<string>();
|
|
|
|
DateTimeOffset? _LastSymbolUpdate = null;
|
2020-06-28 22:07:48 -05:00
|
|
|
readonly Dictionary<string, string> _TickerMapping = new Dictionary<string, string>()
|
2018-11-05 12:14:39 +09:00
|
|
|
{
|
|
|
|
{ "XXDG", "DOGE" },
|
|
|
|
{ "XXBT", "BTC" },
|
|
|
|
{ "XBT", "BTC" },
|
|
|
|
{ "DASH", "DASH" },
|
|
|
|
{ "ZUSD", "USD" },
|
|
|
|
{ "ZEUR", "EUR" },
|
|
|
|
{ "ZJPY", "JPY" },
|
|
|
|
{ "ZCAD", "CAD" },
|
2024-02-01 10:09:32 +09:00
|
|
|
{ "ZGBP", "GBP" },
|
|
|
|
{ "XXMR", "XMR" },
|
|
|
|
{ "XETH", "ETH" },
|
|
|
|
{ "USDC", "USDC" }, // On A=A purpose
|
2018-11-05 12:14:39 +09:00
|
|
|
};
|
|
|
|
|
2024-02-01 10:09:32 +09:00
|
|
|
string Normalize(string ticker)
|
|
|
|
{
|
|
|
|
_TickerMapping.TryGetValue(ticker, out var normalized);
|
|
|
|
return normalized ?? ticker;
|
|
|
|
}
|
|
|
|
|
|
|
|
readonly ConcurrentDictionary<string, CurrencyPair> CachedCurrencyPairs = new ConcurrentDictionary<string, CurrencyPair>();
|
|
|
|
private CurrencyPair GetCurrencyPair(string symbol)
|
|
|
|
{
|
|
|
|
if (CachedCurrencyPairs.TryGetValue(symbol, out var pair))
|
|
|
|
return pair;
|
|
|
|
var found = _TickerMapping.Where(t => symbol.StartsWith(t.Key, StringComparison.OrdinalIgnoreCase))
|
|
|
|
.Select(t => new { KrakenTicker = t.Key, PayTicker = t.Value }).FirstOrDefault();
|
|
|
|
if (found is not null)
|
|
|
|
{
|
|
|
|
pair = new CurrencyPair(found.PayTicker, Normalize(symbol.Substring(found.KrakenTicker.Length)));
|
|
|
|
}
|
|
|
|
if (pair is null)
|
|
|
|
{
|
|
|
|
found = _TickerMapping.Where(t => symbol.EndsWith(t.Key, StringComparison.OrdinalIgnoreCase))
|
|
|
|
.Select(t => new { KrakenTicker = t.Key, PayTicker = t.Value }).FirstOrDefault();
|
|
|
|
if (found is not null)
|
|
|
|
pair = new CurrencyPair(Normalize(symbol.Substring(0, symbol.Length - found.KrakenTicker.Length)), found.PayTicker);
|
|
|
|
}
|
|
|
|
if (pair is null)
|
|
|
|
CurrencyPair.TryParse(symbol, out pair);
|
|
|
|
CachedCurrencyPairs.TryAdd(symbol, pair);
|
|
|
|
return pair;
|
|
|
|
}
|
2020-01-17 18:11:05 +09:00
|
|
|
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
|
2018-08-21 15:59:57 +09:00
|
|
|
{
|
2020-01-17 18:11:05 +09:00
|
|
|
var result = new List<PairRate>();
|
2019-10-20 15:24:07 +09:00
|
|
|
var symbols = await GetSymbolsAsync(cancellationToken);
|
2024-02-01 10:09:32 +09:00
|
|
|
JToken apiTickers = await MakeJsonRequestAsync<JToken>("/0/public/Ticker", null, null, cancellationToken: cancellationToken);
|
2018-08-21 15:59:57 +09:00
|
|
|
foreach (string symbol in symbols)
|
|
|
|
{
|
|
|
|
var ticker = ConvertToExchangeTicker(symbol, apiTickers[symbol]);
|
|
|
|
if (ticker != null)
|
|
|
|
{
|
2024-02-01 10:09:32 +09:00
|
|
|
var pair = GetCurrencyPair(symbol);
|
|
|
|
if (pair is not null)
|
|
|
|
result.Add(new PairRate(pair, new BidAsk(ticker.Bid, ticker.Ask)));
|
2018-08-21 15:59:57 +09:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 18:11:05 +09:00
|
|
|
return result.ToArray();
|
2018-08-21 15:59:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
private static ExchangeTicker ConvertToExchangeTicker(string symbol, JToken ticker)
|
|
|
|
{
|
|
|
|
if (ticker == null)
|
|
|
|
return null;
|
|
|
|
decimal last = ticker["c"][0].ConvertInvariant<decimal>();
|
|
|
|
return new ExchangeTicker
|
|
|
|
{
|
|
|
|
Ask = ticker["a"][0].ConvertInvariant<decimal>(),
|
|
|
|
Bid = ticker["b"][0].ConvertInvariant<decimal>(),
|
|
|
|
Last = last,
|
|
|
|
Volume = new ExchangeVolume
|
|
|
|
{
|
2019-11-11 07:14:29 +01:00
|
|
|
BaseCurrencyVolume = ticker["v"][1].ConvertInvariant<decimal>(),
|
|
|
|
BaseCurrency = symbol,
|
|
|
|
QuoteCurrencyVolume = ticker["v"][1].ConvertInvariant<decimal>() * last,
|
|
|
|
QuoteCurrency = symbol,
|
2018-08-21 15:59:57 +09:00
|
|
|
Timestamp = DateTime.UtcNow
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-20 15:24:07 +09:00
|
|
|
private async Task<string[]> GetSymbolsAsync(CancellationToken cancellationToken)
|
2018-08-21 15:59:57 +09:00
|
|
|
{
|
|
|
|
if (_LastSymbolUpdate != null && DateTimeOffset.UtcNow - _LastSymbolUpdate.Value < TimeSpan.FromDays(0.5))
|
|
|
|
{
|
|
|
|
return _Symbols;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-20 15:24:07 +09:00
|
|
|
JToken json = await MakeJsonRequestAsync<JToken>("/0/public/AssetPairs", cancellationToken: cancellationToken);
|
2018-08-21 15:59:57 +09:00
|
|
|
var symbols = (from prop in json.Children<JProperty>() where !prop.Name.Contains(".d", StringComparison.OrdinalIgnoreCase) select prop.Name).ToArray();
|
|
|
|
_Symbols = symbols;
|
|
|
|
_LastSymbolUpdate = DateTimeOffset.UtcNow;
|
|
|
|
return symbols;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 16:24:11 +09:00
|
|
|
private async Task<T> MakeJsonRequestAsync<T>(string url, string baseUrl = null, Dictionary<string, object> payload = null, string requestMethod = null, CancellationToken cancellationToken = default)
|
2018-08-21 15:59:57 +09:00
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append("https://api.kraken.com");
|
|
|
|
;
|
|
|
|
sb.Append(url);
|
|
|
|
if (payload != null)
|
|
|
|
{
|
2022-11-20 09:42:36 +01:00
|
|
|
sb.Append('?');
|
2018-08-21 15:59:57 +09:00
|
|
|
sb.Append(String.Join('&', payload.Select(kv => $"{kv.Key}={kv.Value}").OfType<object>().ToArray()));
|
|
|
|
}
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, sb.ToString());
|
2023-12-19 11:44:10 +09:00
|
|
|
using var response = await HttpClient.SendAsync(request, cancellationToken);
|
2018-08-21 15:59:57 +09:00
|
|
|
string stringResult = await response.Content.ReadAsStringAsync();
|
|
|
|
var result = JsonConvert.DeserializeObject<T>(stringResult);
|
|
|
|
if (result is JToken json)
|
|
|
|
{
|
2023-01-06 14:18:07 +01:00
|
|
|
if (!(json is JArray) && json["result"] is JObject { Count: > 0 } pairResult)
|
2022-08-10 08:20:41 +02:00
|
|
|
{
|
|
|
|
return (T)(object)(pairResult);
|
2023-01-06 14:18:07 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 15:59:57 +09:00
|
|
|
if (!(json is JArray) && json["error"] is JArray error && error.Count != 0)
|
|
|
|
{
|
2022-08-10 08:20:41 +02:00
|
|
|
throw new APIException(string.Join("\n",
|
|
|
|
error.Select(token => token.ToStringInvariant()).Distinct()));
|
2018-08-21 15:59:57 +09:00
|
|
|
}
|
|
|
|
result = (T)(object)(json["result"] ?? json);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|