2017-09-13 15:47:34 +09:00
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-04-14 22:35:52 +09:00
|
|
|
|
using NBitcoin;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2017-09-15 16:06:57 +09:00
|
|
|
|
namespace BTCPayServer.Services.Rates
|
2017-09-13 15:47:34 +09:00
|
|
|
|
{
|
2018-04-14 22:35:52 +09:00
|
|
|
|
public class BitpayRateProviderDescription : RateProviderDescription
|
|
|
|
|
{
|
|
|
|
|
public IRateProvider CreateRateProvider(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
return new BitpayRateProvider(new Bitpay(new Key(), new Uri("https://bitpay.com/")));
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public class BitpayRateProvider : IRateProvider
|
|
|
|
|
{
|
|
|
|
|
Bitpay _Bitpay;
|
|
|
|
|
public BitpayRateProvider(Bitpay bitpay)
|
|
|
|
|
{
|
|
|
|
|
if (bitpay == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(bitpay));
|
|
|
|
|
_Bitpay = bitpay;
|
|
|
|
|
}
|
|
|
|
|
public async Task<decimal> GetRateAsync(string currency)
|
|
|
|
|
{
|
|
|
|
|
var rates = await _Bitpay.GetRatesAsync().ConfigureAwait(false);
|
|
|
|
|
var rate = rates.GetRate(currency);
|
|
|
|
|
if (rate == 0m)
|
|
|
|
|
throw new RateUnavailableException(currency);
|
|
|
|
|
return (decimal)rate;
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public async Task<ICollection<Rate>> GetRatesAsync()
|
|
|
|
|
{
|
|
|
|
|
return (await _Bitpay.GetRatesAsync().ConfigureAwait(false))
|
|
|
|
|
.AllRates
|
|
|
|
|
.Select(r => new Rate() { Currency = r.Code, Value = r.Value })
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
}
|