mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-19 15:04:19 +01:00
fix issue with changelly rates and cover with UTs (#368)
This commit is contained in:
committed by
Nicolas Dorier
parent
254db22063
commit
f51af6c61c
@@ -118,7 +118,7 @@ namespace BTCPayServer.Tests
|
|||||||
var changellyController =
|
var changellyController =
|
||||||
tester.PayTester.GetController<ChangellyController>(user.UserId, user.StoreId);
|
tester.PayTester.GetController<ChangellyController>(user.UserId, user.StoreId);
|
||||||
changellyController.IsTest = true;
|
changellyController.IsTest = true;
|
||||||
|
|
||||||
//test non existing payment method
|
//test non existing payment method
|
||||||
Assert.IsType<BitpayErrorModel>(Assert
|
Assert.IsType<BitpayErrorModel>(Assert
|
||||||
.IsType<BadRequestObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
.IsType<BadRequestObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
||||||
@@ -143,7 +143,6 @@ namespace BTCPayServer.Tests
|
|||||||
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
||||||
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Assert.IsNotType<BitpayErrorModel>(Assert
|
Assert.IsNotType<BitpayErrorModel>(Assert
|
||||||
.IsType<OkObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
.IsType<OkObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
||||||
@@ -179,18 +178,18 @@ namespace BTCPayServer.Tests
|
|||||||
//confirm saved
|
//confirm saved
|
||||||
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
Assert.Equal("UpdateStore", Assert.IsType<RedirectToActionResult>(
|
||||||
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName);
|
||||||
|
|
||||||
var factory = UnitTest1.CreateBTCPayRateFactory();
|
var factory = UnitTest1.CreateBTCPayRateFactory();
|
||||||
var fetcher = new RateFetcher(factory);
|
var fetcher = new RateFetcher(factory);
|
||||||
var httpClientFactory = new MockHttpClientFactory();
|
var httpClientFactory = new MockHttpClientFactory();
|
||||||
var changellyController = new ChangellyController(
|
var changellyController = new ChangellyController(
|
||||||
new ChangellyClientProvider(tester.PayTester.StoreRepository,httpClientFactory), tester.NetworkProvider, fetcher);
|
new ChangellyClientProvider(tester.PayTester.StoreRepository, httpClientFactory),
|
||||||
|
tester.NetworkProvider, fetcher);
|
||||||
changellyController.IsTest = true;
|
changellyController.IsTest = true;
|
||||||
var result = Assert
|
var result = Assert
|
||||||
.IsType<OkObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
.IsType<OkObjectResult>(await changellyController.GetCurrencyList(user.StoreId))
|
||||||
.Value as IEnumerable<CurrencyFull>;
|
.Value as IEnumerable<CurrencyFull>;
|
||||||
Assert.True(result.Any());
|
Assert.True(result.Any());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,20 +214,40 @@ namespace BTCPayServer.Tests
|
|||||||
var fetcher = new RateFetcher(factory);
|
var fetcher = new RateFetcher(factory);
|
||||||
var httpClientFactory = new MockHttpClientFactory();
|
var httpClientFactory = new MockHttpClientFactory();
|
||||||
var changellyController = new ChangellyController(
|
var changellyController = new ChangellyController(
|
||||||
new ChangellyClientProvider(tester.PayTester.StoreRepository,httpClientFactory), tester.NetworkProvider, fetcher);
|
new ChangellyClientProvider(tester.PayTester.StoreRepository, httpClientFactory),
|
||||||
|
tester.NetworkProvider, fetcher);
|
||||||
changellyController.IsTest = true;
|
changellyController.IsTest = true;
|
||||||
Assert.IsType<decimal>(Assert
|
Assert.IsType<decimal>(Assert
|
||||||
.IsType<OkObjectResult>(await changellyController.CalculateAmount(user.StoreId, "ltc", "btc", 1.0m)).Value);
|
.IsType<OkObjectResult>(await changellyController.CalculateAmount(user.StoreId, "ltc", "btc", 1.0m))
|
||||||
|
.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Trait("Integration", "Integration")]
|
||||||
|
public void CanComputeBaseAmount()
|
||||||
|
{
|
||||||
|
Assert.Equal(1, ChangellyCalculationHelper.ComputeBaseAmount(1, 1));
|
||||||
|
Assert.Equal(0.5m, ChangellyCalculationHelper.ComputeBaseAmount(1, 0.5m));
|
||||||
|
Assert.Equal(2, ChangellyCalculationHelper.ComputeBaseAmount(0.5m, 1));
|
||||||
|
Assert.Equal(4m, ChangellyCalculationHelper.ComputeBaseAmount(1, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Trait("Integration", "Integration")]
|
||||||
|
public void CanComputeCorrectAmount()
|
||||||
|
{
|
||||||
|
Assert.Equal(1, ChangellyCalculationHelper.ComputeCorrectAmount(0.5m, 1, 2));
|
||||||
|
Assert.Equal(0.25m, ChangellyCalculationHelper.ComputeCorrectAmount(0.5m, 1, 0.5m));
|
||||||
|
Assert.Equal(20, ChangellyCalculationHelper.ComputeCorrectAmount(10, 1, 2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MockHttpClientFactory : IHttpClientFactory
|
public class MockHttpClientFactory : IHttpClientFactory
|
||||||
{
|
{
|
||||||
public HttpClient CreateClient(string name)
|
public HttpClient CreateClient(string name)
|
||||||
{
|
{
|
||||||
return new HttpClient();
|
return new HttpClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ namespace BTCPayServer.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
var callCounter = 0;
|
var callCounter = 0;
|
||||||
var response1 = await client.GetExchangeAmount(fromCurrency, toCurrency, 1);
|
var baseRate = await client.GetExchangeAmount(fromCurrency, toCurrency, 1);
|
||||||
var currentAmount = response1;
|
var currentAmount = ChangellyCalculationHelper.ComputeBaseAmount(baseRate, toCurrencyAmount);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (callCounter > 10)
|
if (callCounter > 10)
|
||||||
@@ -70,13 +70,13 @@ namespace BTCPayServer.Controllers
|
|||||||
BadRequest();
|
BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
var response2 = await client.GetExchangeAmount(fromCurrency, toCurrency, currentAmount);
|
var computedAmount = await client.GetExchangeAmount(fromCurrency, toCurrency, currentAmount);
|
||||||
callCounter++;
|
callCounter++;
|
||||||
if (response2 < toCurrencyAmount)
|
if (computedAmount < toCurrencyAmount)
|
||||||
{
|
{
|
||||||
var newCurrentAmount = ((toCurrencyAmount / response2) * 1m) * currentAmount;
|
currentAmount =
|
||||||
|
ChangellyCalculationHelper.ComputeCorrectAmount(currentAmount, computedAmount,
|
||||||
currentAmount = newCurrentAmount;
|
toCurrencyAmount);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -114,4 +114,6 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
public bool IsTest { get; set; } = false;
|
public bool IsTest { get; set; } = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
namespace BTCPayServer.Payments.Changelly
|
||||||
|
{
|
||||||
|
public static class ChangellyCalculationHelper
|
||||||
|
{
|
||||||
|
public static decimal ComputeBaseAmount(decimal baseRate, decimal toAmount)
|
||||||
|
{
|
||||||
|
return (1m / baseRate) * toAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static decimal ComputeCorrectAmount(decimal currentFromAmount, decimal currentAmount,
|
||||||
|
decimal expectedAmount)
|
||||||
|
{
|
||||||
|
return (currentFromAmount / currentAmount) * expectedAmount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user