// GET: WalletsController public async Task <ActionResult> Index() { List <CurrencyTableModel> currencies = await _walletsRepository.GetCurrenciesAsync(); IndexModel model = new IndexModel(); model.Currencies = currencies; model.UserWallets = new List <WalletTableModel>(); model.UserIncomeWallets = new List <IncomeWalletTableModel>(); var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (!string.IsNullOrEmpty(userId)) { model.UserWallets = await _transactionManager.GetUpdatedWalletsAsync(userId); model.UserIncomeWallets = await _walletsRepository.GetUserIncomeWalletsAsync(userId); } return(View(model)); }
public async Task <List <WalletTableModel> > GetUpdatedWalletsAsync(string userId) { await _zecService.GetUpdatedWalletAsync(userId); var incomeWallets = await _walletsRepository.GetUserIncomeWalletsAsync(userId); var wallets = await _walletsRepository.GetUserWalletsAsync(userId); List <IncomeTransactionTableModel> incomeLastTransactions = await _transactionsRepository.GetLastIncomeTransactionsByUserIdAsync(userId) ?? new List <IncomeTransactionTableModel>(); var coinServices = _coinManager .CoinServices .Where(x => incomeWallets .Any(y => x.CoinShortName == y.CurrencyAcronim)) .ToList(); // убирает лишние сервисы, останутся только те у которых юзер имеет кошелёк foreach (var coin in coinServices) { var currencyTableModel = await _walletsRepository.GetCurrencyByAcronimAsync(coin.CoinShortName); var transactionsInBlockchain = coin.ListTransactions(userId); var lastTr = incomeLastTransactions .FirstOrDefault(tr => tr.CurrencyAcronim == coin.CoinShortName); List <TransactionResponse> newTransactionsInBlockchain; if (lastTr == null) { newTransactionsInBlockchain = transactionsInBlockchain; } else { newTransactionsInBlockchain = transactionsInBlockchain.Where(x => x.Time > lastTr.Date).ToList(); //дата в секундах лежит, я не переводил //можно как в блокчейне написать BlockTime //поменять } var wallet = wallets.FirstOrDefault(t => t.CurrencyAcronim == coin.CoinShortName); foreach (var blockchainTransaction in newTransactionsInBlockchain) { var transaction = ConvertTransactionResponseToIncomeTransaction(blockchainTransaction, coin.CoinShortName, wallet.Id, userId); var result = await _balanceProvider.Income(wallet, transaction); var ev = new EventTableModel() { UserId = userId, Type = (int)EventTypeEnum.Income, Comment = $"Income transaction {transaction.CurrencyAcronim}", WhenDate = DateTime.Now, CurrencyAcronim = transaction.CurrencyAcronim, StartBalance = result.StartBalanceReceiver, ResultBalance = result.ResultBalanceReceiver, PlatformCommission = result.Commission, Value = transaction.Amount }; transaction.PlatformCommission = result.Commission; wallet.Value = result.ResultBalanceReceiver.Value; transaction.PlatformCommission = result.Commission; await _transactionsRepository.CreateIncomeTransactionAsync(transaction); await _walletsRepository.UpdateWalletBalanceAsync(wallet); await _eventsRepository.CreateEventAsync(ev); } } return(wallets); }