public async Task <BalanceProviderModel> Income(WalletTableModel walletTableModel,
                                                        IncomeTransactionTableModel incomeTransaction)
        {
            var currency = await _walletsRepository.GetCurrencyByAcronimAsync(incomeTransaction.CurrencyAcronim);

            var balanceProviderModel = new BalanceProviderModel();

            balanceProviderModel.PercentCommission    = currency.PercentCommissionForIncomeTransaction;
            balanceProviderModel.StartBalanceReceiver = walletTableModel.Value;

            if (currency.PercentCommissionForIncomeTransaction != null)
            {
                balanceProviderModel.Commission = incomeTransaction.Amount * currency.PercentCommissionForIncomeTransaction.Value;

                balanceProviderModel.ResultBalanceReceiver = walletTableModel.Value + (incomeTransaction.Amount - balanceProviderModel.Commission.Value);
            }
            else
            {
                balanceProviderModel.ResultBalanceReceiver = walletTableModel.Value + incomeTransaction.Amount;
            }

            return(balanceProviderModel);
        }
Example #2
0
        public async Task <IActionResult> Index(string currency)
        {
            var _currency = await _walletsRepository.GetCurrencyByAcronimAsync(currency);

            if (_currency != null)
            {
                return(View("GeneralWithdrawPage", new GeneralWithdrawModel(_currency.Name)));
            }

            var userId  = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var wallets = await _walletsRepository.GetUserWalletsAsync(userId);

            return(View(wallets));
        }
Example #3
0
        public async Task <IActionResult> Coins(string currency)
        {
            var cur = await _walletsRepository.GetCurrencyByAcronimAsync(currency);

            if (cur != null)
            {
                var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
                SendCoinsViewModel model = new();
                model.Currency   = currency;
                model.Balance    = (await _walletsRepository.GetUserWalletAsync(userId, currency)).Value;
                model.Commission = 0;
                return(View(model));
            }
            return(NotFound());
        }
Example #4
0
        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);
        }