public async Task <IActionResult> GetTokenHoldersInfoByDateTime(int skipElementsCount, int contractId, string secondsFrom, string secondsTo, SortOrder sortOrder = SortOrder.QuantityDesc) { try { var from = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Int64.Parse(secondsFrom)); var to = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Int64.Parse(secondsTo)); var logs = await _dbContext.CustomEventLogs.Where(l => l.ERC20TokenId == contractId) .Where(l => l.WhenDateTime >= from && l.WhenDateTime <= to).ToListAsync(); var holders = EventLogsExplorer.GetInfoFromLogs(logs); holders.ForEach(h => { h.Quantity = (h.TokensReceived - h.TokensSent) > 0 ? h.TokensReceived - h.TokensSent : 0; }); var result = SortHoldersInfo(sortOrder, holders.AsQueryable()); if (skipElementsCount == 0) { return(new OkObjectResult(result.Skip(skipElementsCount).Take(40).ToList())); } return(new OkObjectResult( new TokenHoldersViewModel() { HoldersInfo = result.Skip(skipElementsCount).Take(40).ToList(), SkipElementsCount = skipElementsCount } )); } catch (Exception e) { return(BadRequest(e.Message)); } }
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; } }
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; } }