Exemplo n.º 1
0
 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));
 }
Exemplo n.º 2
0
        public async Task <IActionResult> CalculateAmount(string storeId, string fromCurrency, string toCurrency,
                                                          decimal toCurrencyAmount, CancellationToken cancellationToken)
        {
            try
            {
                var client = await TryGetChangellyClient(storeId);

                if (fromCurrency.Equals("usd", StringComparison.InvariantCultureIgnoreCase) ||
                    fromCurrency.Equals("eur", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(await HandleCalculateFiatAmount(fromCurrency, toCurrency, toCurrencyAmount, cancellationToken));
                }

                var callCounter = 0;
                var baseRate    = await client.GetExchangeAmount(fromCurrency, toCurrency, 1);

                var currentAmount = ChangellyCalculationHelper.ComputeBaseAmount(baseRate, toCurrencyAmount);
                while (true)
                {
                    if (callCounter > 10)
                    {
                        BadRequest();
                    }

                    var computedAmount = await client.GetExchangeAmount(fromCurrency, toCurrency, currentAmount);

                    callCounter++;
                    if (computedAmount < toCurrencyAmount)
                    {
                        currentAmount =
                            ChangellyCalculationHelper.ComputeCorrectAmount(currentAmount, computedAmount,
                                                                            toCurrencyAmount);
                    }
                    else
                    {
                        return(Ok(currentAmount));
                    }
                }
            }
            catch (Exception e)
            {
                return(BadRequest(new BitpayErrorModel()
                {
                    Error = e.Message
                }));
            }
        }
Exemplo n.º 3
0
 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));
 }