2017-10-27 11:39:11 +09:00
|
|
|
|
using Newtonsoft.Json;
|
2018-04-14 22:35:52 +09:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-10-27 11:39:11 +09:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-02-10 22:03:33 +09:00
|
|
|
|
using System.Globalization;
|
2017-10-27 11:39:11 +09:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
2018-04-14 22:35:52 +09:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2017-10-27 11:39:11 +09:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-14 22:35:52 +09:00
|
|
|
|
using System.ComponentModel;
|
2017-10-27 11:39:11 +09:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-14 22:35:52 +09:00
|
|
|
|
|
|
|
|
|
public class CoinAverageRateProviderDescription : RateProviderDescription
|
|
|
|
|
{
|
|
|
|
|
public CoinAverageRateProviderDescription(string crypto)
|
|
|
|
|
{
|
|
|
|
|
CryptoCode = crypto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string CryptoCode { get; set; }
|
|
|
|
|
|
2018-04-15 21:18:51 +09:00
|
|
|
|
public CoinAverageRateProvider CreateRateProvider(IServiceProvider serviceProvider)
|
2018-04-14 22:35:52 +09:00
|
|
|
|
{
|
|
|
|
|
return new CoinAverageRateProvider(CryptoCode)
|
|
|
|
|
{
|
|
|
|
|
Authenticator = serviceProvider.GetService<ICoinAverageAuthenticator>()
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-15 21:18:51 +09:00
|
|
|
|
|
|
|
|
|
IRateProvider RateProviderDescription.CreateRateProvider(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
return CreateRateProvider(serviceProvider);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GetExchangeTickersResponse
|
|
|
|
|
{
|
|
|
|
|
public class Exchange
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
[JsonProperty("display_name")]
|
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
public string[] Symbols { get; set; }
|
|
|
|
|
}
|
|
|
|
|
public bool Success { get; set; }
|
|
|
|
|
public Exchange[] Exchanges { get; set; }
|
2018-04-14 22:35:52 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RatesSetting
|
|
|
|
|
{
|
|
|
|
|
public string PublicKey { get; set; }
|
|
|
|
|
public string PrivateKey { get; set; }
|
|
|
|
|
[DefaultValue(15)]
|
|
|
|
|
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
|
|
|
|
|
public int CacheInMinutes { get; set; } = 15;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface ICoinAverageAuthenticator
|
|
|
|
|
{
|
|
|
|
|
Task AddHeader(HttpRequestMessage message);
|
|
|
|
|
}
|
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 16:00:20 +09:00
|
|
|
|
public string Exchange { get; set; }
|
|
|
|
|
|
2018-01-09 02:57:06 +09:00
|
|
|
|
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
|
|
|
|
{
|
2018-03-26 01:57:44 +09:00
|
|
|
|
if (currency == "BTC")
|
|
|
|
|
return 1.0m;
|
2017-12-17 02:28:37 +09:00
|
|
|
|
if (rates.TryGetValue(currency, out decimal result))
|
|
|
|
|
return result;
|
|
|
|
|
throw new RateUnavailableException(currency);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 22:35:52 +09:00
|
|
|
|
public ICoinAverageAuthenticator Authenticator { get; set; }
|
|
|
|
|
|
2017-12-17 02:28:37 +09:00
|
|
|
|
private async Task<Dictionary<string, decimal>> GetRatesCore()
|
|
|
|
|
{
|
2018-04-14 22:35:52 +09:00
|
|
|
|
string url = Exchange == null ? $"https://apiv2.bitcoinaverage.com/indices/{Market}/ticker/short"
|
|
|
|
|
: $"https://apiv2.bitcoinaverage.com/exchanges/{Exchange}";
|
|
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
|
|
|
|
var auth = Authenticator;
|
|
|
|
|
if (auth != null)
|
2018-01-19 16:00:20 +09:00
|
|
|
|
{
|
2018-04-14 22:35:52 +09:00
|
|
|
|
await auth.AddHeader(request);
|
2018-01-19 16:00:20 +09:00
|
|
|
|
}
|
2018-04-14 22:35:52 +09:00
|
|
|
|
var resp = await _Client.SendAsync(request);
|
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());
|
2018-01-19 16:00:20 +09:00
|
|
|
|
if(Exchange != null)
|
|
|
|
|
{
|
|
|
|
|
rates = (JObject)rates["symbols"];
|
|
|
|
|
}
|
2017-12-17 02:28:37 +09:00
|
|
|
|
return rates.Properties()
|
2018-02-10 22:03:33 +09:00
|
|
|
|
.Where(p => p.Name.StartsWith(CryptoCode, StringComparison.OrdinalIgnoreCase) && TryToDecimal(p, out decimal unused))
|
2018-01-19 16:00:20 +09:00
|
|
|
|
.ToDictionary(p => p.Name.Substring(CryptoCode.Length, p.Name.Length - CryptoCode.Length), p =>
|
|
|
|
|
{
|
2018-02-10 22:03:33 +09:00
|
|
|
|
TryToDecimal(p, out decimal v);
|
|
|
|
|
return v;
|
2018-01-19 16:00:20 +09:00
|
|
|
|
});
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-27 11:39:11 +09:00
|
|
|
|
|
2018-02-10 22:03:33 +09:00
|
|
|
|
private bool TryToDecimal(JProperty p, out decimal v)
|
2017-12-17 02:28:37 +09:00
|
|
|
|
{
|
2018-02-10 22:03:33 +09:00
|
|
|
|
JToken token = p.Value[Exchange == null ? "last" : "bid"];
|
|
|
|
|
return decimal.TryParse(token.Value<string>(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out v);
|
2017-12-17 02:28:37 +09:00
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2018-04-14 22:35:52 +09:00
|
|
|
|
|
|
|
|
|
public async Task TestAuthAsync()
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, "https://apiv2.bitcoinaverage.com/blockchain/tx_price/BTCUSD/8a3b4394ba811a9e2b0bbf3cc56888d053ea21909299b2703cdc35e156c860ff");
|
|
|
|
|
var auth = Authenticator;
|
|
|
|
|
if (auth != null)
|
|
|
|
|
{
|
|
|
|
|
await auth.AddHeader(request);
|
|
|
|
|
}
|
|
|
|
|
var resp = await _Client.SendAsync(request);
|
|
|
|
|
resp.EnsureSuccessStatusCode();
|
|
|
|
|
}
|
2018-04-15 21:18:51 +09:00
|
|
|
|
|
2018-04-18 18:23:39 +09:00
|
|
|
|
public async Task<GetRateLimitsResponse> GetRateLimitsAsync()
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, "https://apiv2.bitcoinaverage.com/info/ratelimits");
|
|
|
|
|
var auth = Authenticator;
|
|
|
|
|
if (auth != null)
|
|
|
|
|
{
|
|
|
|
|
await auth.AddHeader(request);
|
|
|
|
|
}
|
|
|
|
|
var resp = await _Client.SendAsync(request);
|
|
|
|
|
resp.EnsureSuccessStatusCode();
|
|
|
|
|
var jobj = JObject.Parse(await resp.Content.ReadAsStringAsync());
|
|
|
|
|
var response = new GetRateLimitsResponse();
|
|
|
|
|
response.CounterReset = TimeSpan.FromSeconds(jobj["counter_reset"].Value<int>());
|
2018-04-20 01:01:39 +09:00
|
|
|
|
var totalPeriod = jobj["total_period"].Value<string>();
|
|
|
|
|
if (totalPeriod == "24h")
|
|
|
|
|
{
|
|
|
|
|
response.TotalPeriod = TimeSpan.FromHours(24);
|
|
|
|
|
}
|
|
|
|
|
else if (totalPeriod == "30d")
|
|
|
|
|
{
|
|
|
|
|
response.TotalPeriod = TimeSpan.FromDays(30);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.TotalPeriod = TimeSpan.FromSeconds(jobj["total_period"].Value<int>());
|
|
|
|
|
}
|
2018-04-18 18:23:39 +09:00
|
|
|
|
response.RequestsLeft = jobj["requests_left"].Value<int>();
|
2018-04-20 01:01:39 +09:00
|
|
|
|
response.RequestsPerPeriod = jobj["requests_per_period"].Value<int>();
|
2018-04-18 18:23:39 +09:00
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 21:18:51 +09:00
|
|
|
|
public async Task<GetExchangeTickersResponse> GetExchangeTickersAsync()
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, "https://apiv2.bitcoinaverage.com/symbols/exchanges/ticker");
|
|
|
|
|
var resp = await _Client.SendAsync(request);
|
|
|
|
|
resp.EnsureSuccessStatusCode();
|
|
|
|
|
var jobj = JObject.Parse(await resp.Content.ReadAsStringAsync());
|
|
|
|
|
var response = new GetExchangeTickersResponse();
|
|
|
|
|
response.Success = jobj["success"].Value<bool>();
|
|
|
|
|
var exchanges = (JObject)jobj["exchanges"];
|
|
|
|
|
response.Exchanges = exchanges
|
|
|
|
|
.Properties()
|
|
|
|
|
.Select(p =>
|
|
|
|
|
{
|
|
|
|
|
var exchange = JsonConvert.DeserializeObject<GetExchangeTickersResponse.Exchange>(p.Value.ToString());
|
|
|
|
|
exchange.Name = p.Name;
|
|
|
|
|
return exchange;
|
|
|
|
|
})
|
|
|
|
|
.ToArray();
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2018-04-18 18:23:39 +09:00
|
|
|
|
|
|
|
|
|
public class GetRateLimitsResponse
|
|
|
|
|
{
|
|
|
|
|
public TimeSpan CounterReset { get; set; }
|
|
|
|
|
public int RequestsLeft { get; set; }
|
2018-04-20 01:01:39 +09:00
|
|
|
|
public int RequestsPerPeriod { get; set; }
|
|
|
|
|
public TimeSpan TotalPeriod { get; set; }
|
2018-04-18 18:23:39 +09:00
|
|
|
|
}
|
2017-10-27 11:39:11 +09:00
|
|
|
|
}
|