From 15d9adfbf1d898b4672c0c8d53e2e9088093063e Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Mon, 5 Nov 2018 12:14:39 +0900 Subject: [PATCH] Fix rate fetching for kraken doge and dash --- BTCPayServer.Tests/UnitTest1.cs | 17 +++++++++++++ .../Rates/KrakenExchangeRateProvider.cs | 24 +++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/BTCPayServer.Tests/UnitTest1.cs b/BTCPayServer.Tests/UnitTest1.cs index 6eadcfadb..a73c07135 100644 --- a/BTCPayServer.Tests/UnitTest1.cs +++ b/BTCPayServer.Tests/UnitTest1.cs @@ -584,6 +584,23 @@ namespace BTCPayServer.Tests } } + [Fact] + [Trait("Integration", "Integration")] + public void CanSolveTheDogesRatesOnKraken() + { + var provider = new BTCPayNetworkProvider(NetworkType.Mainnet); + var factory = CreateBTCPayRateFactory(); + var fetcher = new RateFetcher(factory); + + Assert.True(RateRules.TryParse("X_X=kraken(X_BTC) * kraken(BTC_X)", out var rule)); + foreach(var pair in new[] { "DOGE_USD", "DOGE_CAD", "DASH_CAD", "DASH_USD", "DASH_EUR" }) + { + var result = fetcher.FetchRate(CurrencyPair.Parse(pair), rule).GetAwaiter().GetResult(); + Assert.NotNull(result.BidAsk); + Assert.Empty(result.Errors); + } + } + [Fact] [Trait("Integration", "Integration")] public void CanRescanWallet() diff --git a/BTCPayServer/Services/Rates/KrakenExchangeRateProvider.cs b/BTCPayServer/Services/Rates/KrakenExchangeRateProvider.cs index 6f9759682..24f9a12b6 100644 --- a/BTCPayServer/Services/Rates/KrakenExchangeRateProvider.cs +++ b/BTCPayServer/Services/Rates/KrakenExchangeRateProvider.cs @@ -42,6 +42,19 @@ namespace BTCPayServer.Services.Rates string[] _Symbols = Array.Empty(); DateTimeOffset? _LastSymbolUpdate = null; + + Dictionary _TickerMapping = new Dictionary() + { + { "XXDG", "DOGE" }, + { "XXBT", "BTC" }, + { "XBT", "BTC" }, + { "DASH", "DASH" }, + { "ZUSD", "USD" }, + { "ZEUR", "EUR" }, + { "ZJPY", "JPY" }, + { "ZCAD", "CAD" }, + }; + public async Task GetRatesAsync() { var result = new ExchangeRates(); @@ -58,11 +71,14 @@ namespace BTCPayServer.Services.Rates try { string global = null; - if(symbol.StartsWith("DASH", StringComparison.OrdinalIgnoreCase)) + var mapped1 = _TickerMapping.Where(t => symbol.StartsWith(t.Key, StringComparison.OrdinalIgnoreCase)) + .Select(t => new { KrakenTicker = t.Key, PayTicker = t.Value }).SingleOrDefault(); + if (mapped1 != null) { - var p2 = symbol.Substring(4); - p2 = p2 == "XBT" ? "BTC" : p2; - global = $"{p2}_{symbol.Substring(0, 4)}"; + var p2 = symbol.Substring(mapped1.KrakenTicker.Length); + if (_TickerMapping.TryGetValue(p2, out var mapped2)) + p2 = mapped2; + global = $"{p2}_{mapped1.PayTicker}"; } else {