protected void AddTransactionsInDb(IDalContext context, Dictionary <string, List <TransactionElement> > rpcTransactions, List <string> updateDbTransactionsIds, Action <HotWalletModel, TransactionElement> actionAddTransactionInDb) { var localWallets = new Dictionary <string, HotWalletModel>(); var localTransactions = new Dictionary <string, TransactionElement>(); var dbWallets = context.HotWallets.ToList(); dbWallets.ForEach(dbWallet => localWallets[dbWallet.Address] = dbWallet); var addRpcTransactionsIds = rpcTransactions.Keys .SelectMany(key => rpcTransactions[key].Select(te => te.TxId)).Except(updateDbTransactionsIds).ToList(); if (addRpcTransactionsIds.Count == 0) { return; } rpcTransactions.Keys.SelectMany(key => rpcTransactions[key]) .ForEach(te => localTransactions[te.TxId] = te); addRpcTransactionsIds.ForEach(id => { var localTransaction = localTransactions[id]; var localWallet = localWallets[localTransaction.Address]; if (localTransaction.Confirmations <= MaxGenerationForConfirmation) { actionAddTransactionInDb(localWallet, localTransaction); } }); context.SaveChangesAsync().Wait(); }
protected List <string> UpdateDbTransactions <TDbTransactonModel>(IDalContext context, Dictionary <string, List <TransactionElement> > rpcTransactions, Func <IDalContext, List <TDbTransactonModel> > getDbTransactions) where TDbTransactonModel : TransactionModel { var dbTransactions = getDbTransactions(context); var transactionsForUpdate = dbTransactions.Where(t => t.Confirmations < MaxGenerationForConfirmation).ToList(); if (dbTransactions.Count == 0) { return(Enumerable.Empty <string>().ToList()); } foreach (var dbTransaction in transactionsForUpdate) { if (!rpcTransactions.ContainsKey(dbTransaction.Address)) { continue; } var isExistTransactionElement = rpcTransactions[dbTransaction.Address].Any(te => string.Compare(te.TxId, dbTransaction.TxId, StringComparison.OrdinalIgnoreCase) == 0); if (!isExistTransactionElement) { continue; } var rpcTransaction = rpcTransactions[dbTransaction.Address].First(te => string.Compare(te.TxId, dbTransaction.TxId, StringComparison.OrdinalIgnoreCase) == 0); dbTransaction.Confirmations = rpcTransaction.Confirmations; } context.SaveChangesAsync().Wait(); return(dbTransactions.Select(transaction => transaction.TxId).ToList()); }
protected void UpdateWallets(IDalContext context, List <ListReceivedByAddressResponseElement> wallets) { var walletsAdresses = wallets.Select(w => w.Address).ToList(); var walletsDictionary = new Dictionary <string, ListReceivedByAddressResponseElement>(wallets.Count); wallets.ForEach(wallet => walletsDictionary[wallet.Address] = wallet); var dbWallets = context.HotWallets.ToList(); var dbWalletsAddresses = dbWallets.Select(dbWallet => dbWallet.Address).ToList(); dbWallets.ForEach(dbWallet => dbWallet.Amount = walletsDictionary[dbWallet.Address].Amount); context.SaveChangesAsync().Wait(); walletsAdresses.Except(dbWalletsAddresses).ForEach(address => { var addWallet = walletsDictionary[address]; context.HotWallets.Add(new HotWalletModel { Account = addWallet.Account, Address = addWallet.Address, Amount = addWallet.Amount }); }); context.SaveChangesAsync().Wait(); }