2017-10-27 11:39:11 +09:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Services.Rates
|
|
|
|
|
{
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public class CoinAverageException : Exception
|
|
|
|
|
{
|
|
|
|
|
public CoinAverageException(string message) : base(message)
|
|
|
|
|
{
|
2017-10-27 11:39:11 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class CoinAverageRateProvider : IRateProvider
|
|
|
|
|
{
|
|
|
|
|
static HttpClient _Client = new HttpClient();
|
2017-10-27 11:39:11 +09:00
|
|
|
|
|
2018-01-09 02:57:06 +09:00
|
|
|
|
public CoinAverageRateProvider(string cryptoCode)
|
|
|
|
|
{
|
|
|
|
|
CryptoCode = cryptoCode ?? "BTC";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string CryptoCode { get; set; }
|
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public string Market
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
} = "global";
|
|
|
|
|
public async Task<decimal> GetRateAsync(string currency)
|
|
|
|
|
{
|
2017-12-17 02:28:37 +09:00
|
|
|
|
var rates = await GetRatesCore();
|
|
|
|
|
return GetRate(rates, currency);
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2017-10-27 11:39:11 +09:00
|
|
|
|
|
2017-12-17 02:28:37 +09:00
|
|
|
|
private decimal GetRate(Dictionary<string, decimal> rates, string currency)
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2017-12-17 02:28:37 +09:00
|
|
|
|
if (rates.TryGetValue(currency, out decimal result))
|
|
|
|
|
return result;
|
|
|
|
|
throw new RateUnavailableException(currency);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Dictionary<string, decimal>> GetRatesCore()
|
|
|
|
|
{
|
|
|
|
|
var resp = await _Client.GetAsync($"https://apiv2.bitcoinaverage.com/indices/{Market}/ticker/short");
|
2017-10-27 17:53:04 +09:00
|
|
|
|
using (resp)
|
|
|
|
|
{
|
2017-10-27 11:39:11 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
if ((int)resp.StatusCode == 401)
|
|
|
|
|
throw new CoinAverageException("Unauthorized access to the API");
|
|
|
|
|
if ((int)resp.StatusCode == 429)
|
|
|
|
|
throw new CoinAverageException("Exceed API limits");
|
|
|
|
|
if ((int)resp.StatusCode == 403)
|
|
|
|
|
throw new CoinAverageException("Unauthorized access to the API, premium plan needed");
|
|
|
|
|
resp.EnsureSuccessStatusCode();
|
2017-12-17 02:28:37 +09:00
|
|
|
|
var rates = JObject.Parse(await resp.Content.ReadAsStringAsync());
|
|
|
|
|
return rates.Properties()
|
2018-01-09 02:57:06 +09:00
|
|
|
|
.Where(p => p.Name.StartsWith(CryptoCode, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
.ToDictionary(p => p.Name.Substring(CryptoCode.Length, p.Name.Length - CryptoCode.Length), p => ToDecimal(p.Value["last"]));
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-27 11:39:11 +09:00
|
|
|
|
|
2017-12-17 02:28:37 +09:00
|
|
|
|
private decimal ToDecimal(JToken token)
|
|
|
|
|
{
|
|
|
|
|
return decimal.Parse(token.Value<string>(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public async Task<ICollection<Rate>> GetRatesAsync()
|
|
|
|
|
{
|
2017-12-17 02:28:37 +09:00
|
|
|
|
var rates = await GetRatesCore();
|
|
|
|
|
return rates.Select(o => new Rate()
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2017-12-17 02:28:37 +09:00
|
|
|
|
Currency = o.Key,
|
|
|
|
|
Value = o.Value
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-27 11:39:11 +09:00
|
|
|
|
}
|