public ListDTO <CryptoAccountDTO> WithdrawToWallet(int id, double amount) { listAccounts.Items = new List <CryptoAccountDTO>(); var cryptoAccount = _context.CryptoAccount.Find(id); FeesManager flatRateFee = new FeesManager(_context); var flatRate = flatRateFee.GetAllFlatRateFees(); cryptoAccount.Sold -= (amount + flatRate); if (cryptoAccount.Sold < 0) { return(null); } string type = "Withdraw"; AddCryptoTransaction(cryptoAccount.CryptoCurrencyName, cryptoAccount.CryptoCurrencyName, amount, type); _context.SaveChanges(); var bankAccountList = _context.CryptoAccount; foreach (var item in bankAccountList) { var items = _mapper.Map <CryptoAccountDTO>(item); listAccounts.Items.Add(items); } return(listAccounts); }
public ListDTO <BankAccountDTO> WithdrawFromBankAccount(int id, double amount) { accountList.Items = new List <BankAccountDTO>(); var bankAccount = _context.BankAccount.Find(id); FeesManager flatRateFee = new FeesManager(_context); var flatRate = flatRateFee.GetAllFlatRateFees(); bankAccount.Sold -= (amount + flatRate); if (bankAccount.Sold < 0) { return(null); } string type = "Withdraw"; AddTransaction(bankAccount, amount, type); _context.SaveChanges(); var bankAccountList = _context.BankAccount; foreach (var item in bankAccountList) { var items = _mapper.Map <BankAccountDTO>(item); accountList.Items.Add(items); } return(accountList); }
public string ExchangeCryptoToFiat(string body) { var cryptoCurrencyName = ""; var fiatCurrencyName = ""; JObject fieldData = JsonConvert.DeserializeObject <JObject>(body); int id = Convert.ToInt32(fieldData["id"]); string selectedValueTo = Convert.ToString(fieldData["selectedValueFrom"]); string selectedValueFrom = Convert.ToString(fieldData["selectedValueTo"]); double amount = Convert.ToDouble(fieldData["amountFrom"]); cryptoCurrencyName = _context.CryptoCurrency.Where(i => i.CryptoCurrencyAbbreviation == selectedValueTo).Select(i => i.CryptoCurrencyName).FirstOrDefault(); fiatCurrencyName = _context.Currency.Where(i => i.CurrencyAbbreviation == selectedValueFrom).Select(i => i.CurrencyName).FirstOrDefault(); if (fiatCurrencyName == null) { fiatCurrencyName = _context.CryptoCurrency.Where(i => i.CryptoCurrencyAbbreviation == selectedValueTo).Select(i => i.CryptoCurrencyName).FirstOrDefault(); GetConversionRateAsync getConversionRateAsyncCrypto = new GetConversionRateAsync(); double conversionRateCrypto = getConversionRateAsyncCrypto.GetConversionRate(selectedValueFrom, selectedValueTo); FeesManager feeCrypto = new FeesManager(_context); var currentFeeCrypto = Convert.ToDouble((int)Math.Round((double)(Convert.ToDouble(feeCrypto.GetAllFees()) / 100) * amount)); var cryptoAccountFrom = _context.CryptoAccount.Where(i => i.CryptoCurrencyName == cryptoCurrencyName).FirstOrDefault(); var cryptoAccountTo = _context.BankAccount.Where(i => i.CurrencyName == fiatCurrencyName).FirstOrDefault(); cryptoAccountFrom.Sold -= (amount + currentFeeCrypto); if (cryptoAccountFrom.Sold < 0) { return(null); } cryptoAccountTo.Sold = amount * Convert.ToDouble(conversionRateCrypto); _context.SaveChanges(); string type = "Crypto Transaction"; CryptoManager transaction = new CryptoManager(_context, _mapper); transaction.AddCryptoTransaction(selectedValueFrom, selectedValueTo, amount, type); return("ok"); } 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 acountTo = _context.BankAccount.Where(i => i.CurrencyName == fiatCurrencyName).FirstOrDefault(); var acountFrom = _context.CryptoAccount.Where(i => i.CryptoCurrencyName == cryptoCurrencyName).FirstOrDefault(); acountFrom.Sold -= (amount + currentFee); if (acountFrom.Sold < 0) { return(null); } acountTo.Sold += amount * Convert.ToDouble(conversionRate); string typetransaction = "Withdraw Crypto Transaction"; CryptoManager transactionCrypto = new CryptoManager(_context, _mapper); transactionCrypto.AddCryptoTransaction(selectedValueTo, selectedValueFrom, amount, typetransaction); _context.SaveChanges(); return("ok"); }
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"); }