Files
btcpayserver/BTCPayServer/Controllers/ChangellyController.cs
Andrew Camilleri a5fca7a1c4 Changelly Support (#267)
* Disable shapeshift and use changelly

* UI to manage changelly payment method

* wip on changelly api

* Add in Vue component for changelly and remove target currency from payment method

* add changelly merhcant id

* Small fixes to get Conversion to load

* wip fixing the component

* fix merge conflict

* fixes to UI

* remove debug, fix fee calc and move changelly to own partials

* Update ChangellyController.cs

* move original vue setup back to checkout

* Update core.js

* Extracting Changelly component to js file

* Proposal for loading spinner

* remove zone

* imrpove changelly ui

* add in changelly config checks

* try new method to calculate amount + remove to currency from list

* abstract changelly lofgic to provider and reduce dependency on js component

* Add UTs for Changelly

* refactor changelly backend

* fix failing UT

* add shitcoin tax

* pr changes

* pr changes
2018-10-18 12:13:39 +09:00

104 lines
3.3 KiB
C#

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Models;
using BTCPayServer.Payments.Changelly;
using Changelly.ResponseModel;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
[Route("[controller]/{storeId}")]
public class ChangellyController : Controller
{
private readonly ChangellyClientProvider _changellyClientProvider;
public ChangellyController(ChangellyClientProvider changellyClientProvider)
{
_changellyClientProvider = changellyClientProvider;
}
[HttpGet]
[Route("currencies")]
public async Task<IActionResult> GetCurrencyList(string storeId)
{
if (!TryGetChangellyClient(storeId, out var actionResult, out var client))
{
return actionResult;
}
var result = _changellyClientProvider.GetCurrenciesFull(client);
if (result.Success)
{
return Ok(result);
}
return BadRequest(result);
}
[HttpGet]
[Route("calculate")]
public IActionResult CalculateAmount(string storeId, string fromCurrency, string toCurrency,
double toCurrencyAmount)
{
if (!TryGetChangellyClient(storeId, out var actionResult, out var client))
{
return actionResult;
}
double? currentAmount = null;
var callCounter = 0;
var response1 = _changellyClientProvider.GetExchangeAmount(client,fromCurrency, toCurrency, 1);
if (!response1.Success) return BadRequest(response1);
currentAmount = response1.amount;
while (true)
{
if (callCounter > 10)
{
BadRequest();
}
//Client needs to be reset between same calls for some reason
if (!TryGetChangellyClient(storeId, out actionResult, out client))
{
return actionResult;
}
var response2 = _changellyClientProvider.GetExchangeAmount(client,fromCurrency, toCurrency, currentAmount.Value);
callCounter++;
if (!response2.Success) return BadRequest(response2);
if (response2.amount < toCurrencyAmount)
{
var newCurrentAmount = ((toCurrencyAmount / response2.amount) * 1) * currentAmount.Value;
currentAmount = newCurrentAmount;
}
else
{
return Ok(currentAmount.Value);
}
}
}
private bool TryGetChangellyClient(string storeId, out IActionResult actionResult,
out Changelly.Changelly changelly)
{
changelly = null;
actionResult = null;
storeId = storeId ?? HttpContext.GetStoreData()?.Id;
if (!_changellyClientProvider.TryGetChangellyClient(storeId, out var error, out changelly))
{
actionResult = BadRequest(new BitpayErrorModel()
{
Error = error
});
return false;
}
return true;
}
}
}