Exemple #1
0
        private async Task <List <CustomTransaction> > GetSmartContractTransactionByAddress(string accountAddress,
                                                                                            List <Transaction> transactions, BigInteger timestamp)
        {
            var result = new List <CustomTransaction>();

            foreach (var t in transactions)
            {
                if (t.Input.StartsWith(Constants.Strings.TransactionType.Transfer))
                {
                    using (var scope = _scopeFactory.CreateScope())
                    {
                        var dbContext = scope.ServiceProvider.GetRequiredService <WalletDbContext>();

                        var decodedInput = InputDecoder.DecodeTransferInput(t.Input);

                        if (t.To.Equals(accountAddress, StringComparison.CurrentCultureIgnoreCase))
                        {
                            var status =
                                await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(t.TransactionHash);

                            bool isSuccess = true;
                            if (status != null)
                            {
                                if (status.Status.Value == 0)
                                {
                                    isSuccess = false;
                                }
                            }

                            var token = dbContext.Erc20Tokens.FirstOrDefault(tok =>
                                                                             tok.Address.Equals(t.To, StringComparison.CurrentCultureIgnoreCase));

                            result.Add(new CustomTransaction()
                            {
                                TransactionHash = t.TransactionHash,
                                From            = t.From,
                                To              = decodedInput.To,
                                What            = token?.Symbol ?? "Unknown",
                                IsSuccess       = isSuccess,
                                ContractAddress = t.To,
                                DecimalValue    = Web3.Convert.FromWei(decodedInput.Value, token?.DecimalPlaces ?? 18),
                                Date            = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(
                                    (long)(timestamp))
                            });
                        }
                    }
                }
            }

            return(result);
        }
Exemple #2
0
        public List <BlockChainTransaction> GetLatestTransactions(int startBlockNumber, int endBlockNumber)
        {
            var result = new List <BlockChainTransaction>();
            var temp   = new List <BlockChainTransaction>();

            for (var i = startBlockNumber; i <= endBlockNumber; i++)
            {
                temp.Clear();
                try
                {
                    var block = web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(
                        new HexBigInteger(i)).Result;

                    foreach (var transact in block.Transactions)
                    {
                        if (transact.Input.Equals(Constants.Strings.TransactionType.Usual,
                                                  StringComparison.CurrentCultureIgnoreCase))
                        {
                            var status =
                                web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transact.TransactionHash)
                                .Result;
                            bool isSuccess = true;
                            if (status != null)
                            {
                                if (status.Status.Value == 0)
                                {
                                    isSuccess = false;
                                }
                            }

                            double value = 0;
                            try
                            {
                                value = (double)Web3.Convert.FromWei(transact.Value.Value, 18);
                            }
                            catch (Exception)
                            {
                            }

                            temp.Add(new BlockChainTransaction()
                            {
                                TransactionHash = transact.TransactionHash,
                                FromAddress     = transact.From,
                                ToAddress       = transact.To,
                                What            = "ETH",
                                IsSuccess       = isSuccess,
                                ContractAddress = String.Empty,
                                DecimalValue    = value,
                                Date            = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(
                                    (long)(block.Timestamp.Value)),
                                BlockNumber = (int)block.Number.Value
                            });
                        }
                        else if (transact.Input.StartsWith(Constants.Strings.TransactionType.Transfer))
                        {
                            using (var scope = _scopeFactory.CreateScope())
                            {
                                var dbContext = scope.ServiceProvider.GetRequiredService <WalletDbContext>();
                                var status    =
                                    web3.Eth.Transactions.GetTransactionReceipt
                                    .SendRequestAsync(transact.TransactionHash).Result;
                                bool isSuccess = true;
                                if (status != null)
                                {
                                    if (status.Status.Value == 0)
                                    {
                                        isSuccess = false;
                                    }
                                }

                                var decodedInput = InputDecoder.DecodeTransferInput(transact.Input);
                                var token        = dbContext.Erc20Tokens.FirstOrDefault(tok =>
                                                                                        tok.Address.Equals(transact.To, StringComparison.CurrentCultureIgnoreCase));


                                double value = 0;
                                if (token != null)
                                {
                                    try
                                    {
                                        value = (double)Web3.Convert.FromWei(decodedInput.Value,
                                                                             token?.DecimalPlaces ?? 18);
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }

                                temp.Add(new BlockChainTransaction()
                                {
                                    TransactionHash = transact.TransactionHash,
                                    FromAddress     = transact.From,
                                    ToAddress       = decodedInput.To,
                                    ContractAddress = transact.To,
                                    What            = token?.Symbol ?? "Unknown",
                                    IsSuccess       = isSuccess,
                                    DecimalValue    = value,
                                    Date            = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(
                                        (long)(block.Timestamp.Value)),
                                    BlockNumber = (int)block.Number.Value
                                });
                            }
                        }
                    }

                    result.AddRange(temp);
                }
                catch (RpcClientUnknownException e)
                {
                    i--;
                }
                catch (RpcClientTimeoutException)
                {
                    i--;
                }
                catch (Exception e)
                {
                }
            }
            return(result);
        }