btcpayserver/BTCPayServer/Controllers/BitpayRateController.cs

170 lines
6.9 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05:00
using System;
2017-09-13 15:47:34 +09:00
using System.Collections.Generic;
2021-12-27 13:46:31 +09:00
using System.Globalization;
2017-09-13 15:47:34 +09:00
using System.Linq;
2020-06-28 17:55:27 +09:00
using System.Text;
using System.Threading;
2017-09-13 15:47:34 +09:00
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Data;
2017-09-13 15:47:34 +09:00
using BTCPayServer.Filters;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Models;
using BTCPayServer.Rating;
using BTCPayServer.Security;
using BTCPayServer.Security.Bitpay;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
2020-06-28 17:55:27 +09:00
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
2017-09-13 15:47:34 +09:00
namespace BTCPayServer.Controllers
{
[EnableCors(CorsPolicies.All)]
[Authorize(Policy = ServerPolicies.CanGetRates.Key, AuthenticationSchemes = AuthenticationSchemes.Bitpay)]
2022-01-07 12:08:28 +09:00
public class BitpayRateController : Controller
{
readonly RateFetcher _rateProviderFactory;
readonly BTCPayNetworkProvider _networkProvider;
readonly CurrencyNameTable _currencyNameTable;
readonly StoreRepository _storeRepo;
private StoreData CurrentStore => HttpContext.GetStoreData();
2022-01-07 12:08:28 +09:00
public BitpayRateController(
RateFetcher rateProviderFactory,
BTCPayNetworkProvider networkProvider,
StoreRepository storeRepo,
CurrencyNameTable currencyNameTable)
{
_rateProviderFactory = rateProviderFactory ?? throw new ArgumentNullException(nameof(rateProviderFactory));
_networkProvider = networkProvider;
_storeRepo = storeRepo;
_currencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
}
2017-09-13 15:47:34 +09:00
2018-05-21 16:49:37 +02:00
[Route("rates/{baseCurrency}")]
[HttpGet]
[BitpayAPIConstraint]
2019-10-12 20:35:30 +09:00
public async Task<IActionResult> GetBaseCurrencyRates(string baseCurrency, CancellationToken cancellationToken)
2018-05-21 16:49:37 +02:00
{
var supportedMethods = CurrentStore.GetSupportedPaymentMethods(_networkProvider);
2018-05-21 16:49:37 +02:00
var currencyCodes = supportedMethods.Where(method => !string.IsNullOrEmpty(method.PaymentId.CryptoCode))
.Select(method => method.PaymentId.CryptoCode).Distinct();
2018-05-28 14:55:49 +02:00
2018-07-30 22:51:39 +09:00
var currencypairs = BuildCurrencyPairs(currencyCodes, baseCurrency);
2019-10-12 20:35:30 +09:00
var result = await GetRates2(currencypairs, null, cancellationToken);
2018-05-21 16:49:37 +02:00
var rates = (result as JsonResult)?.Value as Rate[];
return rates == null ? result : Json(new DataWrapper<Rate[]>(rates));
2018-05-21 16:49:37 +02:00
}
[HttpGet("rates/{baseCurrency}/{currency}")]
[BitpayAPIConstraint]
2019-10-12 20:35:30 +09:00
public async Task<IActionResult> GetCurrencyPairRate(string baseCurrency, string currency, CancellationToken cancellationToken)
2018-05-28 10:20:18 +02:00
{
2019-10-12 20:35:30 +09:00
var result = await GetRates2($"{baseCurrency}_{currency}", null, cancellationToken);
return (result as JsonResult)?.Value is not Rate[] rates
? result
: Json(new DataWrapper<Rate>(rates.First()));
2018-05-28 10:20:18 +02:00
}
[HttpGet("rates")]
[BitpayAPIConstraint]
2019-11-15 18:20:10 +09:00
public async Task<IActionResult> GetRates(string currencyPairs, string storeId = null, CancellationToken cancellationToken = default)
2018-01-20 12:11:24 +09:00
{
2019-11-15 18:10:14 +09:00
var result = await GetRates2(currencyPairs, storeId, cancellationToken);
return (result as JsonResult)?.Value is not Rate[] rates
? result
: Json(new DataWrapper<Rate[]>(rates));
2018-01-20 12:11:24 +09:00
}
2019-10-12 20:35:30 +09:00
[AllowAnonymous]
[HttpGet("api/rates")]
public async Task<IActionResult> GetRates2(string currencyPairs, string storeId, CancellationToken cancellationToken)
{
var store = CurrentStore ?? await _storeRepo.FindStore(storeId);
2018-05-03 03:32:42 +09:00
if (store == null)
{
var err = Json(new BitpayErrorsModel { Error = "Store not found" });
2019-10-12 20:35:30 +09:00
err.StatusCode = 404;
return err;
}
2018-05-28 14:55:49 +02:00
if (currencyPairs == null)
{
currencyPairs = store.GetStoreBlob().GetDefaultCurrencyPairString();
2018-05-28 14:55:49 +02:00
if (string.IsNullOrEmpty(currencyPairs))
{
var result = Json(new BitpayErrorsModel() { Error = "You need to setup the default currency pairs in 'Store Settings / Rates' or specify 'currencyPairs' query parameter (eg. BTC_USD,LTC_CAD)." });
2018-05-28 14:55:49 +02:00
result.StatusCode = 400;
return result;
}
}
var rules = store.GetStoreBlob().GetRateRules(_networkProvider);
var pairs = new HashSet<CurrencyPair>();
2018-05-28 14:55:49 +02:00
foreach (var currency in currencyPairs.Split(','))
2018-05-03 03:32:42 +09:00
{
2018-05-28 14:55:49 +02:00
if (!CurrencyPair.TryParse(currency, out var pair))
2018-05-03 03:32:42 +09:00
{
var result = Json(new BitpayErrorsModel() { Error = $"Currency pair {currency} uncorrectly formatted" });
result.StatusCode = 400;
return result;
}
pairs.Add(pair);
}
2018-04-15 21:18:51 +09:00
var fetching = _rateProviderFactory.FetchRates(pairs, rules, cancellationToken);
2018-05-03 03:32:42 +09:00
await Task.WhenAll(fetching.Select(f => f.Value).ToArray());
return Json(pairs
.Select(r => (Pair: r, Value: fetching[r].GetAwaiter().GetResult().BidAsk?.Bid))
2018-05-03 03:32:42 +09:00
.Where(r => r.Value.HasValue)
.Select(r =>
new Rate
{
2018-05-03 03:32:42 +09:00
CryptoCode = r.Pair.Left,
Code = r.Pair.Right,
2018-05-06 22:41:38 +09:00
CurrencyPair = r.Pair.ToString(),
Name = _currencyNameTable.GetCurrencyData(r.Pair.Right, true).Name,
2018-05-03 03:32:42 +09:00
Value = r.Value.Value
2018-01-20 12:11:24 +09:00
}).Where(n => n.Name != null).ToArray());
}
2018-05-03 03:32:42 +09:00
2018-07-30 22:51:39 +09:00
private static string BuildCurrencyPairs(IEnumerable<string> currencyCodes, string baseCrypto)
{
var currencyPairsBuilder = new StringBuilder();
2018-07-30 22:51:39 +09:00
bool first = true;
foreach (var currencyCode in currencyCodes)
{
2019-10-12 20:35:30 +09:00
if (!first)
2021-10-06 12:53:41 +09:00
currencyPairsBuilder.Append(',');
2018-07-30 23:07:29 +09:00
first = false;
2021-12-27 13:46:31 +09:00
currencyPairsBuilder.Append(CultureInfo.InvariantCulture, $"{baseCrypto}_{currencyCode}");
2018-07-30 22:51:39 +09:00
}
return currencyPairsBuilder.ToString();
}
2018-05-03 03:32:42 +09:00
public class Rate
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
2018-05-03 03:32:42 +09:00
[JsonProperty(PropertyName = "cryptoCode")]
public string CryptoCode { get; set; }
2018-05-06 22:41:38 +09:00
[JsonProperty(PropertyName = "currencyPair")]
public string CurrencyPair { get; set; }
2018-05-06 22:41:38 +09:00
2018-05-03 03:32:42 +09:00
[JsonProperty(PropertyName = "code")]
public string Code { get; set; }
2018-05-03 03:32:42 +09:00
[JsonProperty(PropertyName = "rate")]
public decimal Value { get; set; }
2018-05-03 03:32:42 +09:00
}
}
2017-09-13 15:47:34 +09:00
}