2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Filters;
|
2017-09-15 09:06:57 +02:00
|
|
|
|
using BTCPayServer.Services.Rates;
|
2018-01-19 10:11:43 +01:00
|
|
|
|
using BTCPayServer.Services.Stores;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public class RateController : Controller
|
|
|
|
|
{
|
2018-01-19 10:11:43 +01:00
|
|
|
|
IRateProviderFactory _RateProviderFactory;
|
|
|
|
|
BTCPayNetworkProvider _NetworkProvider;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
CurrencyNameTable _CurrencyNameTable;
|
2018-01-19 10:11:43 +01:00
|
|
|
|
StoreRepository _StoreRepo;
|
|
|
|
|
public RateController(
|
|
|
|
|
IRateProviderFactory rateProviderFactory,
|
|
|
|
|
BTCPayNetworkProvider networkProvider,
|
|
|
|
|
StoreRepository storeRepo,
|
|
|
|
|
CurrencyNameTable currencyNameTable)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-19 10:11:43 +01:00
|
|
|
|
_RateProviderFactory = rateProviderFactory ?? throw new ArgumentNullException(nameof(rateProviderFactory));
|
|
|
|
|
_NetworkProvider = networkProvider;
|
|
|
|
|
_StoreRepo = storeRepo;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
_CurrencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[Route("rates")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[BitpayAPIConstraint]
|
2018-01-19 10:11:43 +01:00
|
|
|
|
public async Task<IActionResult> GetRates(string cryptoCode = null, string storeId = null)
|
2018-01-20 04:11:24 +01:00
|
|
|
|
{
|
|
|
|
|
var result = await GetRates2(cryptoCode, storeId);
|
|
|
|
|
var rates = (result as JsonResult)?.Value as NBitpayClient.Rate[];
|
|
|
|
|
if(rates == null)
|
|
|
|
|
return result;
|
|
|
|
|
return Json(new DataWrapper<NBitpayClient.Rate[]>(rates));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("api/rates")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> GetRates2(string cryptoCode = null, string storeId = null)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-19 10:11:43 +01:00
|
|
|
|
cryptoCode = cryptoCode ?? "BTC";
|
2018-01-20 04:11:24 +01:00
|
|
|
|
var network = _NetworkProvider.GetNetwork(cryptoCode);
|
2018-01-19 10:11:43 +01:00
|
|
|
|
if (network == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2018-04-15 14:18:51 +02:00
|
|
|
|
RateRules rules = null;
|
2018-01-20 04:11:24 +01:00
|
|
|
|
if (storeId != null)
|
2018-01-19 10:11:43 +01:00
|
|
|
|
{
|
|
|
|
|
var store = await _StoreRepo.FindStore(storeId);
|
|
|
|
|
if (store == null)
|
|
|
|
|
return NotFound();
|
2018-04-15 14:18:51 +02:00
|
|
|
|
rules = store.GetStoreBlob().GetRateRules();
|
2018-01-19 10:11:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 14:18:51 +02:00
|
|
|
|
var rateProvider = _RateProviderFactory.GetRateProvider(network, rules);
|
|
|
|
|
if (rateProvider == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2018-01-19 10:11:43 +01:00
|
|
|
|
var allRates = (await rateProvider.GetRatesAsync());
|
2018-01-20 04:11:24 +01:00
|
|
|
|
return Json(allRates.Select(r =>
|
2017-10-27 10:53:04 +02:00
|
|
|
|
new NBitpayClient.Rate()
|
|
|
|
|
{
|
|
|
|
|
Code = r.Currency,
|
|
|
|
|
Name = _CurrencyNameTable.GetCurrencyData(r.Currency)?.Name,
|
|
|
|
|
Value = r.Value
|
2018-01-20 04:11:24 +01:00
|
|
|
|
}).Where(n => n.Name != null).ToArray());
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|