public async Task <IActionResult> ConversionRate([FromQuery] string currencyPair)
        {
            if (string.IsNullOrEmpty(currencyPair) || currencyPair.Trim().Length != 6)
            {
                return(Json(ResponseModel.GetResponse((int)HttpStatusCode.BadRequest, "Currency pair text is invalid.")));
            }

            var currency1 = currencyPair.Substring(0, 3);
            var currency2 = currencyPair.Substring(3, 3);

            var rate = await _rateCalculatorService.CalculateConversionRate(currency1, currency2);

            if (rate == -1)
            {
                return(Json(ResponseModel.GetResponse((int)HttpStatusCode.BadRequest, "Currency pair text is invalid.")));
            }

            var message = $"1 {currency1} = {rate:0.0000} {currency2}";

            return(Json(ResponseModel.GetResponse((int)HttpStatusCode.OK, message, new { rate = $"{rate:0.0000}" })));
        }
Exemple #2
0
        public async Task <OptimalConversionRateByPartnerResponse> GetOptimalCurrencyRateByPartnerAsync(Guid partnerId, Guid customerId, string from, string to)
        {
            var burnRules = await _campaignClient.BurnRules.GetAsync(new BurnRulePaginationRequest
            {
                CurrentPage = 1,
                PageSize    = 500,
                PartnerId   = partnerId
            });

            if (burnRules.BurnRules.Any())
            {
                var bestRule = burnRules.BurnRules
                               .Where(r => !r.UsePartnerCurrencyRate)
                               .OrderByDescending(r => r.AmountInCurrency / r.AmountInTokens)
                               .FirstOrDefault();

                if (bestRule != null)
                {
                    return(new OptimalConversionRateByPartnerResponse
                    {
                        UsedRate = await _rateCalculatorService.CalculateConversionRate(bestRule.AmountInCurrency.Value, bestRule.AmountInTokens.Value, from, to),
                        ConversionSource = ConversionSource.BurnRule,
                        SpendRuleId = bestRule.Id,
                        CustomerId = customerId
                    });
                }
            }

            var partner = await _partnerManagementClient.Partners.GetByIdAsync(partnerId);

            if (partner == null)
            {
                throw new PartnerNotFoundException($"Partner with id '{partnerId}' cannot be found.");
            }

            var conversionSource = ConversionSource.Partner;

            decimal amountInCurrency = 0;
            Money18 amountInTokens   = 0;

            if (partner.UseGlobalCurrencyRate)
            {
                conversionSource = ConversionSource.Global;
                var globalRate = await _currencyConvertorClient.GlobalCurrencyRates.GetAsync();

                amountInCurrency = globalRate.AmountInCurrency;
                amountInTokens   = globalRate.AmountInTokens;
            }
            else
            {
                amountInCurrency = partner.AmountInCurrency.Value;
                amountInTokens   = partner.AmountInTokens.Value;
            }

            return(new OptimalConversionRateByPartnerResponse
            {
                UsedRate = await _rateCalculatorService.CalculateConversionRate(amountInCurrency, amountInTokens, from, to),
                ConversionSource = conversionSource,
                SpendRuleId = null,
                CustomerId = customerId
            });
        }