btcpayserver/BTCPayServer/Services/Rates/QuadrigacxRateProvider.cs

49 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
2018-05-03 03:32:42 +09:00
using BTCPayServer.Rating;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Services.Rates
{
public class QuadrigacxRateProvider : IRateProvider
{
2018-05-03 03:32:42 +09:00
public const string QuadrigacxName = "quadrigacx";
static HttpClient _Client = new HttpClient();
private bool TryToDecimal(JObject p, out decimal v)
{
v = 0.0m;
JToken token = p.Property("bid")?.Value;
if (token == null)
return false;
return decimal.TryParse(token.Value<string>(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out v);
}
2018-05-03 03:32:42 +09:00
public async Task<ExchangeRates> GetRatesAsync()
{
var response = await _Client.GetAsync($"https://api.quadrigacx.com/v2/ticker?book=all");
response.EnsureSuccessStatusCode();
var rates = JObject.Parse(await response.Content.ReadAsStringAsync());
2018-05-03 03:32:42 +09:00
var exchangeRates = new ExchangeRates();
foreach (var prop in rates.Properties())
{
2018-05-03 03:32:42 +09:00
var rate = new ExchangeRate();
if (!Rating.CurrencyPair.TryParse(prop.Name, out var pair))
continue;
rate.CurrencyPair = pair;
rate.Exchange = QuadrigacxName;
if (!TryToDecimal((JObject)prop.Value, out var v))
continue;
rate.Value = v;
2018-05-03 03:32:42 +09:00
exchangeRates.Add(rate);
}
2018-05-03 03:32:42 +09:00
return exchangeRates;
}
}
}