diff --git a/BTCPayServer.Tests/ChangellyTests.cs b/BTCPayServer.Tests/ChangellyTests.cs index cbbb0c3e7..c445957fc 100644 --- a/BTCPayServer.Tests/ChangellyTests.cs +++ b/BTCPayServer.Tests/ChangellyTests.cs @@ -118,7 +118,7 @@ namespace BTCPayServer.Tests var changellyController = tester.PayTester.GetController(user.UserId, user.StoreId); changellyController.IsTest = true; - + //test non existing payment method Assert.IsType(Assert .IsType(await changellyController.GetCurrencyList(user.StoreId)) @@ -143,7 +143,6 @@ namespace BTCPayServer.Tests Assert.Equal("UpdateStore", Assert.IsType( await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName); - Assert.IsNotType(Assert .IsType(await changellyController.GetCurrencyList(user.StoreId)) @@ -179,18 +178,18 @@ namespace BTCPayServer.Tests //confirm saved Assert.Equal("UpdateStore", Assert.IsType( await storesController.UpdateChangellySettings(user.StoreId, updateModel, "save")).ActionName); - + var factory = UnitTest1.CreateBTCPayRateFactory(); var fetcher = new RateFetcher(factory); var httpClientFactory = new MockHttpClientFactory(); 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; var result = Assert .IsType(await changellyController.GetCurrencyList(user.StoreId)) .Value as IEnumerable; Assert.True(result.Any()); - } } @@ -215,20 +214,40 @@ namespace BTCPayServer.Tests var fetcher = new RateFetcher(factory); var httpClientFactory = new MockHttpClientFactory(); 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; Assert.IsType(Assert - .IsType(await changellyController.CalculateAmount(user.StoreId, "ltc", "btc", 1.0m)).Value); - + .IsType(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 HttpClient CreateClient(string name) { - return new HttpClient(); + return new HttpClient(); } } } diff --git a/BTCPayServer/Controllers/ChangellyController.cs b/BTCPayServer/Controllers/ChangellyController.cs index 7568d96cb..b1f6294d7 100644 --- a/BTCPayServer/Controllers/ChangellyController.cs +++ b/BTCPayServer/Controllers/ChangellyController.cs @@ -61,8 +61,8 @@ namespace BTCPayServer.Controllers } var callCounter = 0; - var response1 = await client.GetExchangeAmount(fromCurrency, toCurrency, 1); - var currentAmount = response1; + var baseRate = await client.GetExchangeAmount(fromCurrency, toCurrency, 1); + var currentAmount = ChangellyCalculationHelper.ComputeBaseAmount(baseRate, toCurrencyAmount); while (true) { if (callCounter > 10) @@ -70,13 +70,13 @@ namespace BTCPayServer.Controllers BadRequest(); } - var response2 = await client.GetExchangeAmount(fromCurrency, toCurrency, currentAmount); + var computedAmount = await client.GetExchangeAmount(fromCurrency, toCurrency, currentAmount); callCounter++; - if (response2 < toCurrencyAmount) + if (computedAmount < toCurrencyAmount) { - var newCurrentAmount = ((toCurrencyAmount / response2) * 1m) * currentAmount; - - currentAmount = newCurrentAmount; + currentAmount = + ChangellyCalculationHelper.ComputeCorrectAmount(currentAmount, computedAmount, + toCurrencyAmount); } else { @@ -114,4 +114,6 @@ namespace BTCPayServer.Controllers public bool IsTest { get; set; } = false; } + + } diff --git a/BTCPayServer/Payments/Changelly/ChangellyCalculationHelper.cs b/BTCPayServer/Payments/Changelly/ChangellyCalculationHelper.cs new file mode 100644 index 000000000..8297b78b8 --- /dev/null +++ b/BTCPayServer/Payments/Changelly/ChangellyCalculationHelper.cs @@ -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; + } + } +}