public IResponse <List <EthereumBlockModel> > GetBlocksWithTransactions(long jobId, int maxBlockNumber)
        {
            var response = new Response <List <EthereumBlockModel> >
            {
                Value = new List <EthereumBlockModel>()
            };

            try
            {
                var blocks = _entity.Block.Where(b => b.JobTimelineId == jobId && b.BlockNumber > maxBlockNumber).ToList();

                foreach (var block in blocks)
                {
                    var blockToAdd = new EthereumBlockModel
                    {
                        BlockNumber       = (int)block.BlockNumber,
                        TimeStamp         = block.BlockTime,
                        BlockTransactions = new List <EthereumBlockTransactionModel>()
                    };

                    foreach (var transaction in block.BlockTransaction)
                    {
                        var transactionToAdd = new EthereumBlockTransactionModel
                        {
                            Hash = transaction.TxHash
                        };

                        blockToAdd.BlockTransactions.Add(transactionToAdd);
                    }

                    response.Value.Add(blockToAdd);
                }

                response.Status = Common.Enums.StatusEnum.Success;
            }
            catch (Exception ex)
            {
                _logger.Information($"BlockTransactionService.GetBlocksWithTransactions(jobId: {jobId}, maxBlockNumber: {maxBlockNumber})");
                _logger.Error(ex.Message);
                response.Status  = Common.Enums.StatusEnum.Error;
                response.Message = ex.Message;
            }

            return(response);
        }
Esempio n. 2
0
        public async Task <Response <List <EthereumBlockModel> > > GetBlocksWithTransactions(CryptoAdapterModel cryptoAdapter, int fromBlock, int toBlock, string address)
        {
            var response = new Response <List <EthereumBlockModel> >()
            {
                Value = new List <EthereumBlockModel>()
            };

            var web3 = InstantiateWeb3(cryptoAdapter);

            try
            {
                if (fromBlock > toBlock)
                {
                    _logger.Information($"EthereumAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}");
                    _logger.Error($"FromBlock value {fromBlock} is greater than ToBlock value {toBlock}");
                    response.Status = StatusEnum.Error;
                    return(response);
                }

                var stopwatch = Stopwatch.StartNew();

                for (int i = fromBlock; i <= toBlock; i++)
                {
                    var block = await web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(new HexBigInteger(i));

                    var blockModel = new EthereumBlockModel
                    {
                        BlockNumber       = (int)block.Number.Value,
                        TimeStamp         = UnixTimeStampToDateTime((double)block.Timestamp.Value),
                        BlockTransactions = new List <EthereumBlockTransactionModel>()
                    };

                    foreach (var transaction in block.Transactions)
                    {
                        var blockTransactionModel = new EthereumBlockTransactionModel
                        {
                            Hash  = transaction.TransactionHash,
                            Value = Web3.Convert.FromWei(transaction.Value),
                            From  = transaction.From,
                            To    = transaction.To
                        };

                        address = address.ToLower();

                        if (address == String.Empty || (String.Equals(transaction.From.ToLower(), address) || String.Equals(transaction.To?.ToLower(), address)))
                        {
                            var transactionReceipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transaction.TransactionHash);

                            var status = transactionReceipt.Status?.Value;

                            blockTransactionModel.Status = ConvertStatus(status);

                            blockModel.BlockTransactions.Add(blockTransactionModel);
                        }
                    }
                    response.Value.Add(blockModel);
                }

                stopwatch.Stop();
                _logger.Information($"EthereumAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}). Time elapsed: {stopwatch.Elapsed.TotalSeconds} seconds.");

                response.Status = StatusEnum.Success;
            }
            catch (Exception ex)
            {
                _logger.Information($"EthereumAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}");
                _logger.Error(ex.Message);
                response.Status  = StatusEnum.Error;
                response.Message = ex.Message;
            }

            return(response);
        }