private void DoWork(object state) { Task.Run(async() => { try { using (var scope = _scopeFactory.CreateScope()) { if (_lastCheckedBlockNumber == 0) { _lastCheckedBlockNumber = (int)(await _explorer.GetLastAvailableBlockNumber()).Value; } if ((int)(await _explorer.GetLastAvailableBlockNumber()).Value > _lastCheckedBlockNumber) { var dbContext = scope.ServiceProvider.GetRequiredService <WalletDbContext>(); Transaction[] transactions = {}; if (_userInfo.onlineUsers.Count > 0) { transactions = await GetLastBlockTransactions(); } foreach (var user in _userInfo.onlineUsers) { var data = await dbContext.UserWatchlist .Where(w => w.UserEmail.Equals(user.Value.UserName, StringComparison.CurrentCultureIgnoreCase)).Include(w => w.NotificationOptions) .ToListAsync(); var ids = GetIdNotificatedAddresses(transactions.ToList(), data); var result = WatchlistHelper.OrganizeData(data); result.ForEach(e => { if (ids.Any(i => i == e.Account.Id)) { e.Account.IsNotificated = true; } if (ids.Any(i => i == e.Contract.Id)) { e.Contract.IsNotificated = true; } }); await _hubContext.Clients.Clients(user.Value.ConnectionId) .SendAsync("Message", result); } _lastCheckedBlockNumber++; } } } catch (Exception e) { //await _hubContext.Clients.All.SendAsync("Message", e.Message); } }); }
private void DoWork(object state) { if (_isRunning) { return; } Task.Run(async() => { try { using (var scope = _scopeFactory.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <WalletDbContext>(); if (!(dbContext.PageData.FirstOrDefault()?.IsTransactionsSaved ?? false)) { return; } _isRunning = true; _lastBlockNumber = (int)(await _explorer.GetLastAvailableBlockNumber()).Value; if (_lastCheckedBlockNumber == 0) { _lastCheckedBlockNumber = (int)(dbContext.BlockChainTransactions .Max(w => w.BlockNumber)); } if (_lastCheckedBlockNumber < _lastBlockNumber) { var transactions = _explorer.GetLatestTransactions(_lastCheckedBlockNumber, _lastCheckedBlockNumber); dbContext.BlockChainTransactions.AddRange(transactions); dbContext.SaveChanges(); _lastCheckedBlockNumber++; } _isRunning = false; } } catch (Exception e) { _isRunning = false; } }); }
private void DoWork(object state) { try { if (isRunning) { return; } isRunning = true; List <ERC20Token> tokens; using (var dbContext = new WalletDbContext(DbContextOptionsFactory.DbContextOptions())) { tokens = dbContext.Erc20Tokens.ToList(); } foreach (var token in tokens) { if (token.IsSynchronized) { continue; } var lastBlockNumber = (int)(_explorer.GetLastAvailableBlockNumber().Result.Value); var logs = _explorer.GetFullEventLogs(token, lastBlockNumber).Result; var holders = EventLogsExplorer.GetInfoFromLogs(logs); for (int i = 0; i < holders.Count; i++) { try { var balance = _explorer.GetTokenHolderBalance(holders[i].Address, token.Address).Result; holders[i].Quantity = Web3.Convert.FromWei(balance, token.DecimalPlaces); holders[i].ERC20TokenId = token.Id; } catch (Exception e) { i--; } } SaveToDb(logs); SaveToDb(holders); using (var dbContext = new WalletDbContext(DbContextOptionsFactory.DbContextOptions())) { token.LastSynchronizedBlockNumber = lastBlockNumber; token.IsSynchronized = true; dbContext.Erc20Tokens.Update(token); dbContext.SaveChanges(); } } isRunning = false; } catch (Exception e) { isRunning = false; } }
public async Task <IActionResult> SaveLatestTransactions() { var status = _dbContext.PageData.FirstOrDefault(); if (status != null && status.IsTransactionsSaved) { return(Ok()); } var lastKnownBlockNumber = (int)(await _explorer.GetLastAvailableBlockNumber()).Value; var tasks = new List <Task <List <BlockChainTransaction> > >(); for (int i = lastKnownBlockNumber - 5000; i < lastKnownBlockNumber; i += 100) { var i1 = i; var task = Task.Run(() => _explorer.GetLatestTransactions(i1, i1 + 99)); tasks.Add(task); } await Task.WhenAll(tasks); var result = new List <BlockChainTransaction>(); foreach (var task in tasks) { task.Result.ForEach(t => result.Add(t)); } _dbContext.ChangeTracker.AutoDetectChangesEnabled = false; var tempList = new List <BlockChainTransaction>(); foreach (var transact in result) { tempList.Add(transact); if (tempList.Count == 100) { try { _dbContext.BlockChainTransactions.AddRange(tempList); _dbContext.SaveChanges(); tempList.Clear(); } catch (Exception e) { tempList.Clear(); } } } _dbContext.ChangeTracker.AutoDetectChangesEnabled = true; var lastSavedNumber = (int)(_dbContext.BlockChainTransactions .Max(w => w.BlockNumber)); lastKnownBlockNumber = (int)(await _explorer.GetLastAvailableBlockNumber()).Value; var newTransacts = _explorer.GetLatestTransactions(lastSavedNumber, lastKnownBlockNumber); _dbContext.BlockChainTransactions.AddRange(newTransacts); _dbContext.SaveChanges(); var data = _dbContext.PageData.FirstOrDefault(); if (data != null) { data.IsTransactionsSaved = true; _dbContext.PageData.Update(data); } _dbContext.SaveChanges(); return(Ok()); }
private async void DoWork(object state) { try { if (_isRunning) { return; } _isRunning = true; List <ERC20Token> tokens; using (var dbContext = new WalletDbContext(DbContextOptionsFactory.DbContextOptions())) { tokens = dbContext.Erc20Tokens.ToList(); } foreach (var token in tokens) { if (!token.IsSynchronized) { continue; } var lastBlockNumber = (int)(_explorer.GetLastAvailableBlockNumber().Result.Value); var logs = await _explorer.GetFullEventLogs(token, lastBlockNumber, token.LastSynchronizedBlockNumber + 1); var holders = EventLogsExplorer.GetInfoFromLogs(logs); for (int i = 0; i < holders.Count; i++) { try { var balance = _explorer.GetTokenHolderBalance(holders[i].Address, token.Address).Result; holders[i].Quantity = Web3.Convert.FromWei(balance, token.DecimalPlaces); holders[i].ERC20TokenId = token.Id; } catch (Exception e) { i--; } } using (var dbContext = new WalletDbContext(DbContextOptionsFactory.DbContextOptions())) { foreach (var h in holders) { var holder = dbContext.TokenHolders.FirstOrDefault(e => e.Address.Equals(h.Address, StringComparison.CurrentCultureIgnoreCase)); if (holder != null) { holder.Quantity = h.Quantity; holder.GeneralTransactionsNumber += h.GeneralTransactionsNumber; holder.SentTransactionsNumber += h.SentTransactionsNumber; holder.ReceivedTransactionsNumber += h.ReceivedTransactionsNumber; holder.TokensSent += h.TokensSent; holder.TokensReceived += h.TokensReceived; dbContext.TokenHolders.Update(holder); } else { dbContext.TokenHolders.Add(h); } } token.LastSynchronizedBlockNumber = GetNewLastSearchedBlockNumber(lastBlockNumber, token.LastSynchronizedBlockNumber + 1); dbContext.Erc20Tokens.Update(token); dbContext.SaveChanges(); } SaveToDb(logs); _isRunning = false; } } catch (Exception e) { _isRunning = false; } }