From 465fbdd47f1eaf54f35599224bcb69bc7fde9217 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sat, 10 Feb 2018 22:03:33 +0900 Subject: [PATCH] Fix bug which can happen if parsing of CoinAverage decimal is on another culture --- .../Services/Rates/CoinAverageRateProvider.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs b/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs index 8d063f42d..ff6b087fd 100644 --- a/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs +++ b/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs @@ -2,6 +2,7 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -72,24 +73,19 @@ namespace BTCPayServer.Services.Rates rates = (JObject)rates["symbols"]; } return rates.Properties() - .Where(p => p.Name.StartsWith(CryptoCode, StringComparison.OrdinalIgnoreCase)) + .Where(p => p.Name.StartsWith(CryptoCode, StringComparison.OrdinalIgnoreCase) && TryToDecimal(p, out decimal unused)) .ToDictionary(p => p.Name.Substring(CryptoCode.Length, p.Name.Length - CryptoCode.Length), p => { - if (Exchange == null) - { - return ToDecimal(p.Value["last"]); - } - else - { - return ToDecimal(p.Value["bid"]); - } + TryToDecimal(p, out decimal v); + return v; }); } } - private decimal ToDecimal(JToken token) + private bool TryToDecimal(JProperty p, out decimal v) { - return decimal.Parse(token.Value(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint); + JToken token = p.Value[Exchange == null ? "last" : "bid"]; + return decimal.TryParse(token.Value(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out v); } public async Task> GetRatesAsync()