Пример #1
0
        public string FiatExchange(string body)
        {
            JObject fieldData          = JsonConvert.DeserializeObject <JObject>(body);
            int     id                 = Convert.ToInt32(fieldData["id"]);
            string  selectedValueFrom  = Convert.ToString(fieldData["selectedValueFrom"]);
            string  selectedValueTo    = Convert.ToString(fieldData["selectedValueTo"]);
            double  amount             = Convert.ToDouble(fieldData["amountFrom"]);
            var     fiatCurrencyName   = _context.Currency.Where(i => i.CurrencyAbbreviation == selectedValueFrom).Select(i => i.CurrencyName).FirstOrDefault();
            var     cryptoCurrencyName = _context.CryptoCurrency.Where(i => i.CryptoCurrencyAbbreviation == selectedValueTo).Select(i => i.CryptoCurrencyName).FirstOrDefault();
            GetConversionRateAsync getConversionRateAsync = new GetConversionRateAsync();
            var         conversionRate = getConversionRateAsync.GetConversionRate(selectedValueFrom, selectedValueTo);
            FeesManager fee            = new FeesManager(_context);
            var         currentFee     = Convert.ToDouble((int)Math.Round((double)(Convert.ToDouble(fee.GetAllFees()) / 100) * amount));
            var         bankAccount    = _context.BankAccount.Where(i => i.IdUser == id && i.CurrencyName == fiatCurrencyName).FirstOrDefault();
            var         cryptoAccount  = _context.CryptoAccount.Where(i => i.CryptoCurrencyName == cryptoCurrencyName).FirstOrDefault();

            bankAccount.Sold -= (amount + currentFee);
            if (bankAccount.Sold < 0)
            {
                return(null);
            }
            if (conversionRate == 0)
            {
                cryptoAccount.Sold = amount;
                _context.SaveChanges();
                return("ok");
            }
            var test  = Convert.ToDouble(conversionRate);
            var test1 = test * amount;

            cryptoAccount.Sold += amount * Convert.ToDouble(conversionRate);
            _context.SaveChanges();

            string        type        = "Fiat Transaction";
            CryptoManager transaction = new CryptoManager(_context, _mapper);

            transaction.AddCryptoTransaction(selectedValueFrom, selectedValueTo, amount, type);
            return("ok");
        }