mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 22:14:26 +01:00
Refactoring: Bitpay Rate Controller
Some tweaks I did when working on #3752.
This commit is contained in:
committed by
Andrew Camilleri
parent
84fcd1c1b5
commit
8ef8c97072
@@ -25,33 +25,24 @@ namespace BTCPayServer.Controllers
|
|||||||
[Authorize(Policy = ServerPolicies.CanGetRates.Key, AuthenticationSchemes = AuthenticationSchemes.Bitpay)]
|
[Authorize(Policy = ServerPolicies.CanGetRates.Key, AuthenticationSchemes = AuthenticationSchemes.Bitpay)]
|
||||||
public class BitpayRateController : Controller
|
public class BitpayRateController : Controller
|
||||||
{
|
{
|
||||||
public StoreData CurrentStore
|
|
||||||
{
|
readonly RateFetcher _rateProviderFactory;
|
||||||
get
|
readonly BTCPayNetworkProvider _networkProvider;
|
||||||
{
|
readonly CurrencyNameTable _currencyNameTable;
|
||||||
return HttpContext.GetStoreData();
|
readonly StoreRepository _storeRepo;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
readonly RateFetcher _RateProviderFactory;
|
private StoreData CurrentStore => HttpContext.GetStoreData();
|
||||||
readonly BTCPayNetworkProvider _NetworkProvider;
|
|
||||||
readonly CurrencyNameTable _CurrencyNameTable;
|
|
||||||
readonly StoreRepository _StoreRepo;
|
|
||||||
|
|
||||||
public TokenRepository TokenRepository { get; }
|
|
||||||
|
|
||||||
public BitpayRateController(
|
public BitpayRateController(
|
||||||
RateFetcher rateProviderFactory,
|
RateFetcher rateProviderFactory,
|
||||||
BTCPayNetworkProvider networkProvider,
|
BTCPayNetworkProvider networkProvider,
|
||||||
TokenRepository tokenRepository,
|
|
||||||
StoreRepository storeRepo,
|
StoreRepository storeRepo,
|
||||||
CurrencyNameTable currencyNameTable)
|
CurrencyNameTable currencyNameTable)
|
||||||
{
|
{
|
||||||
_RateProviderFactory = rateProviderFactory ?? throw new ArgumentNullException(nameof(rateProviderFactory));
|
_rateProviderFactory = rateProviderFactory ?? throw new ArgumentNullException(nameof(rateProviderFactory));
|
||||||
_NetworkProvider = networkProvider;
|
_networkProvider = networkProvider;
|
||||||
TokenRepository = tokenRepository;
|
_storeRepo = storeRepo;
|
||||||
_StoreRepo = storeRepo;
|
_currencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
|
||||||
_CurrencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("rates/{baseCurrency}")]
|
[Route("rates/{baseCurrency}")]
|
||||||
@@ -59,7 +50,7 @@ namespace BTCPayServer.Controllers
|
|||||||
[BitpayAPIConstraint]
|
[BitpayAPIConstraint]
|
||||||
public async Task<IActionResult> GetBaseCurrencyRates(string baseCurrency, CancellationToken cancellationToken)
|
public async Task<IActionResult> GetBaseCurrencyRates(string baseCurrency, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var supportedMethods = CurrentStore.GetSupportedPaymentMethods(_NetworkProvider);
|
var supportedMethods = CurrentStore.GetSupportedPaymentMethods(_networkProvider);
|
||||||
|
|
||||||
var currencyCodes = supportedMethods.Where(method => !string.IsNullOrEmpty(method.PaymentId.CryptoCode))
|
var currencyCodes = supportedMethods.Where(method => !string.IsNullOrEmpty(method.PaymentId.CryptoCode))
|
||||||
.Select(method => method.PaymentId.CryptoCode).Distinct();
|
.Select(method => method.PaymentId.CryptoCode).Distinct();
|
||||||
@@ -68,44 +59,37 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
var result = await GetRates2(currencypairs, null, cancellationToken);
|
var result = await GetRates2(currencypairs, null, cancellationToken);
|
||||||
var rates = (result as JsonResult)?.Value as Rate[];
|
var rates = (result as JsonResult)?.Value as Rate[];
|
||||||
if (rates == null)
|
return rates == null ? result : Json(new DataWrapper<Rate[]>(rates));
|
||||||
return result;
|
|
||||||
return Json(new DataWrapper<Rate[]>(rates));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("rates/{baseCurrency}/{currency}")]
|
[HttpGet("rates/{baseCurrency}/{currency}")]
|
||||||
[HttpGet]
|
|
||||||
[BitpayAPIConstraint]
|
[BitpayAPIConstraint]
|
||||||
public async Task<IActionResult> GetCurrencyPairRate(string baseCurrency, string currency, CancellationToken cancellationToken)
|
public async Task<IActionResult> GetCurrencyPairRate(string baseCurrency, string currency, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var result = await GetRates2($"{baseCurrency}_{currency}", null, cancellationToken);
|
var result = await GetRates2($"{baseCurrency}_{currency}", null, cancellationToken);
|
||||||
var rates = (result as JsonResult)?.Value as Rate[];
|
return (result as JsonResult)?.Value is not Rate[] rates
|
||||||
if (rates == null)
|
? result
|
||||||
return result;
|
: Json(new DataWrapper<Rate>(rates.First()));
|
||||||
return Json(new DataWrapper<Rate>(rates.First()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("rates")]
|
[HttpGet("rates")]
|
||||||
[HttpGet]
|
|
||||||
[BitpayAPIConstraint]
|
[BitpayAPIConstraint]
|
||||||
public async Task<IActionResult> GetRates(string currencyPairs, string storeId = null, CancellationToken cancellationToken = default)
|
public async Task<IActionResult> GetRates(string currencyPairs, string storeId = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var result = await GetRates2(currencyPairs, storeId, cancellationToken);
|
var result = await GetRates2(currencyPairs, storeId, cancellationToken);
|
||||||
var rates = (result as JsonResult)?.Value as Rate[];
|
return (result as JsonResult)?.Value is not Rate[] rates
|
||||||
if (rates == null)
|
? result
|
||||||
return result;
|
: Json(new DataWrapper<Rate[]>(rates));
|
||||||
return Json(new DataWrapper<Rate[]>(rates));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("api/rates")]
|
|
||||||
[HttpGet]
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
|
[HttpGet("api/rates")]
|
||||||
public async Task<IActionResult> GetRates2(string currencyPairs, string storeId, CancellationToken cancellationToken)
|
public async Task<IActionResult> GetRates2(string currencyPairs, string storeId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var store = this.CurrentStore ?? await this._StoreRepo.FindStore(storeId);
|
var store = CurrentStore ?? await _storeRepo.FindStore(storeId);
|
||||||
if (store == null)
|
if (store == null)
|
||||||
{
|
{
|
||||||
var err = Json(new BitpayErrorsModel() { Error = "Store not found" });
|
var err = Json(new BitpayErrorsModel { Error = "Store not found" });
|
||||||
err.StatusCode = 404;
|
err.StatusCode = 404;
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -120,10 +104,8 @@ namespace BTCPayServer.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rules = store.GetStoreBlob().GetRateRules(_networkProvider);
|
||||||
var rules = store.GetStoreBlob().GetRateRules(_NetworkProvider);
|
var pairs = new HashSet<CurrencyPair>();
|
||||||
|
|
||||||
HashSet<CurrencyPair> pairs = new HashSet<CurrencyPair>();
|
|
||||||
foreach (var currency in currencyPairs.Split(','))
|
foreach (var currency in currencyPairs.Split(','))
|
||||||
{
|
{
|
||||||
if (!CurrencyPair.TryParse(currency, out var pair))
|
if (!CurrencyPair.TryParse(currency, out var pair))
|
||||||
@@ -135,25 +117,25 @@ namespace BTCPayServer.Controllers
|
|||||||
pairs.Add(pair);
|
pairs.Add(pair);
|
||||||
}
|
}
|
||||||
|
|
||||||
var fetching = _RateProviderFactory.FetchRates(pairs, rules, cancellationToken);
|
var fetching = _rateProviderFactory.FetchRates(pairs, rules, cancellationToken);
|
||||||
await Task.WhenAll(fetching.Select(f => f.Value).ToArray());
|
await Task.WhenAll(fetching.Select(f => f.Value).ToArray());
|
||||||
return Json(pairs
|
return Json(pairs
|
||||||
.Select(r => (Pair: r, Value: fetching[r].GetAwaiter().GetResult().BidAsk?.Bid))
|
.Select(r => (Pair: r, Value: fetching[r].GetAwaiter().GetResult().BidAsk?.Bid))
|
||||||
.Where(r => r.Value.HasValue)
|
.Where(r => r.Value.HasValue)
|
||||||
.Select(r =>
|
.Select(r =>
|
||||||
new Rate()
|
new Rate
|
||||||
{
|
{
|
||||||
CryptoCode = r.Pair.Left,
|
CryptoCode = r.Pair.Left,
|
||||||
Code = r.Pair.Right,
|
Code = r.Pair.Right,
|
||||||
CurrencyPair = r.Pair.ToString(),
|
CurrencyPair = r.Pair.ToString(),
|
||||||
Name = _CurrencyNameTable.GetCurrencyData(r.Pair.Right, true).Name,
|
Name = _currencyNameTable.GetCurrencyData(r.Pair.Right, true).Name,
|
||||||
Value = r.Value.Value
|
Value = r.Value.Value
|
||||||
}).Where(n => n.Name != null).ToArray());
|
}).Where(n => n.Name != null).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string BuildCurrencyPairs(IEnumerable<string> currencyCodes, string baseCrypto)
|
private static string BuildCurrencyPairs(IEnumerable<string> currencyCodes, string baseCrypto)
|
||||||
{
|
{
|
||||||
StringBuilder currencyPairsBuilder = new StringBuilder();
|
var currencyPairsBuilder = new StringBuilder();
|
||||||
bool first = true;
|
bool first = true;
|
||||||
foreach (var currencyCode in currencyCodes)
|
foreach (var currencyCode in currencyCodes)
|
||||||
{
|
{
|
||||||
@@ -169,37 +151,19 @@ namespace BTCPayServer.Controllers
|
|||||||
{
|
{
|
||||||
|
|
||||||
[JsonProperty(PropertyName = "name")]
|
[JsonProperty(PropertyName = "name")]
|
||||||
public string Name
|
public string Name { get; set; }
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
[JsonProperty(PropertyName = "cryptoCode")]
|
[JsonProperty(PropertyName = "cryptoCode")]
|
||||||
public string CryptoCode
|
public string CryptoCode { get; set; }
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonProperty(PropertyName = "currencyPair")]
|
[JsonProperty(PropertyName = "currencyPair")]
|
||||||
public string CurrencyPair
|
public string CurrencyPair { get; set; }
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonProperty(PropertyName = "code")]
|
[JsonProperty(PropertyName = "code")]
|
||||||
public string Code
|
public string Code { get; set; }
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
[JsonProperty(PropertyName = "rate")]
|
[JsonProperty(PropertyName = "rate")]
|
||||||
public decimal Value
|
public decimal Value { get; set; }
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user