Example #1
0
        public async Task <WalletTableModel> GetUpdatedWalletAsync(string userId)
        {
            WalletTableModel wallet = await _walletsRepository.GetUserWalletAsync(userId, "ZEC");

            if (wallet == null)
            {
                return(null);
            }

            List <IncomeWalletTableModel> incomeZecWallets = await _walletsRepository.GetUserIncomeWalletsByAcronimAsync(userId, "ZEC");

            List <IncomeTransactionTableModel> transactionModels = await _transactionsRepository.GetIncomeTransactions(userId, "ZEC");

            List <string> savedTransactions = transactionModels.Select(x => x.TransactionId).ToList();

            foreach (var incomeZecWallet in incomeZecWallets)
            {
                List <ZecDeltas> addressIncomeTransactions = GetAddressIncomingTransactions(incomeZecWallet.Address);

                foreach (var tx in addressIncomeTransactions)
                {
                    if (!savedTransactions.Contains(tx.TxId))
                    {
                        var transaction = await _transactionsRepository.CreateIncomeTransactionAsync(new IncomeTransactionTableModel()
                        {
                            CurrencyAcronim = "ZEC",
                            TransactionId   = tx.TxId,
                            Amount          = tx.Satoshis / 100000000,
                            UserId          = userId,
                            FromAddress     = null,
                            ToAddress       = tx.Address,
                            TransactionFee  = 0,
                        });


                        var result = await _balanceProvider.Income(wallet, transaction);

                        await _eventsRepository.CreateEventAsync(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);

            return(wallet);
        }
Example #2
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);
        }