public TransactionSummary(string toAddress, List <string> excludeFromAddresses, Transaction transaction, EthGasStation ethGasStation, bool enableEthGasStation) { EthGasStation = ethGasStation; _EnableEthGasStation = enableEthGasStation; ToAddress = toAddress; ExcludeFromAddresses = excludeFromAddresses; Blocks = transaction.Blocks.Select(b => new Block(b)).ToList(); if (!Blocks.Any()) { return; } BlockNumber = Blocks[0].BlockNumber; BlockTimestamp = Blocks[0].Timestamp; BasedOnNumberOfBlocks = Blocks.Count(); GasUsedPercent = Math.Round(Blocks.Average(b => b.GasUsedPercent), 3); var allSuccessTransactions = transaction.AllBlocks.SelectMany(b => b.Results.Where(r => r.BlockNumber != null && r.Status == "success")).ToArray(); var allSuccessGasPrice = allSuccessTransactions.Select(result => result.GasPrice).OrderBy(p => p).ToArray(); if (allSuccessGasPrice.Any()) { HighestGasPriceGwei = UnitConversion.Convert.FromWei(allSuccessGasPrice.Max(), toUnit: UnitConversion.EthUnit.Gwei); LowestGasPriceGwei = UnitConversion.Convert.FromWei(allSuccessGasPrice.Min(), toUnit: UnitConversion.EthUnit.Gwei); Percentile80GasPriceGwei = UnitConversion.Convert.FromWei(allSuccessGasPrice[(int)(allSuccessGasPrice.Count() * 0.8)], toUnit: UnitConversion.EthUnit.Gwei); Percentile60GasPriceGwei = UnitConversion.Convert.FromWei(allSuccessGasPrice[(int)(allSuccessGasPrice.Count() * 0.6)], toUnit: UnitConversion.EthUnit.Gwei); Percentile40GasPriceGwei = UnitConversion.Convert.FromWei(allSuccessGasPrice[(int)(allSuccessGasPrice.Count() * 0.4)], toUnit: UnitConversion.EthUnit.Gwei); Percentile20GasPriceGwei = UnitConversion.Convert.FromWei(allSuccessGasPrice[(int)(allSuccessGasPrice.Count() * 0.2)], toUnit: UnitConversion.EthUnit.Gwei); Percentile10GasPriceGwei = UnitConversion.Convert.FromWei(allSuccessGasPrice[(int)(allSuccessGasPrice.Count() * 0.1)], toUnit: UnitConversion.EthUnit.Gwei); var monitorBlocks = transaction.Blocks. SelectMany(b => b.Results. Where(r => ExcludeFromAddresses.All(a => !a.Equals(r.From, StringComparison.OrdinalIgnoreCase))). Select(r => r.GasPrice)). OrderBy(p => p). ToArray(); var maxMonitorGwei = UnitConversion.Convert.FromWei((monitorBlocks.Any() ? monitorBlocks.Max() : 0), toUnit: UnitConversion.EthUnit.Gwei); HighestTxPriceOrPercentile80GasPriceGwei = new decimal[] { maxMonitorGwei, Percentile80GasPriceGwei }.Max(); HighestTxPriceOrPercentile60GasPriceGwei = new decimal[] { maxMonitorGwei, Percentile60GasPriceGwei }.Max(); HighestTxPriceOrPercentile40GasPriceGwei = new decimal[] { maxMonitorGwei, Percentile40GasPriceGwei }.Max(); HighestTxPriceOrPercentile20GasPriceGwei = new decimal[] { maxMonitorGwei, Percentile20GasPriceGwei }.Max(); HighestTxPriceOrPercentile10GasPriceGwei = new decimal[] { maxMonitorGwei, Percentile10GasPriceGwei }.Max(); } }
public EthGasStationArgs(EthGasStation ethGasStation) => EthGasStation = ethGasStation;
public APIResponseArgs(string address, string[] excludeAddresses, Transaction transaction, EthGasStation ethGasStation, bool enableEthGasStation) =>
private void RunMeter(string toAddress, CancellationToken token) { Transaction.MonitorAddress = toAddress; var lastBlockNo = new BigInteger(0); var web3 = new Web3(string.IsNullOrWhiteSpace(_UserWeb3) ? InfuraDevWeb3 : _UserWeb3); while (!token.IsCancellationRequested) { try { var blockNo = new HexBigInteger(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync().Result.Value); if (blockNo.Value > lastBlockNo) { if (lastBlockNo == 0) { Transaction.AddBlockByNumber(blockNo, web3); } else { for (var tempBlockNo = (lastBlockNo + 1); tempBlockNo <= blockNo.Value; tempBlockNo++) { Transaction.AddBlockByNumber(new HexBigInteger(tempBlockNo), web3); } } lastBlockNo = blockNo; if (_EnableEthGasStation) { Task.Run(() => EthGasStation.GetLatestGasStation()). ContinueWith(task => { if (task.Result != null && (EthGasStation == null || task.Result.BlockNumber > EthGasStation.BlockNumber)) { EthGasStation = task.Result; OnEthGasStation?.Invoke(this, new EthGasStation.EthGasStationArgs(EthGasStation)); } }); } } } catch (AggregateException aEx) { var errMessage = new StringBuilder(); errMessage.AppendLine(aEx.Message); if (!aEx.InnerExceptions.Any()) { if (aEx.InnerException != null) { errMessage.AppendLine(" " + aEx.InnerException.Message); } } else { foreach (var ex in aEx.InnerExceptions) { errMessage.AppendLine(" " + ex.Message); if (ex.InnerException != null) { errMessage.AppendLine(" " + ex.InnerException.Message); } } } OnMessage?.Invoke(this, new MessageArgs(errMessage.ToString())); } catch (Exception ex) { var errMessage = new StringBuilder(); errMessage.AppendLine(ex.Message); if (ex.InnerException != null) { errMessage.AppendLine(" " + ex.InnerException.Message); } OnMessage?.Invoke(this, new MessageArgs(errMessage.ToString())); } GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized, false); Task.Delay((int)(_DelayLoopSec * 1000)); } }