Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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;
            }
        }