public IEnumerable <MevBundle> GetBundles(BlockHeader parent, long gasLimit) { MevBundle?bestBundle = null; UInt256 bestMevEquivalentPrice = 0; long totalGasUsed = 0; long maxGasUsed = gasLimit * _maxBundlesGasUsedRatio / 100; foreach (var bundle in _bundleSource.GetBundles(parent, gasLimit)) { if (maxGasUsed - totalGasUsed < 21000) { break; } SimulatedMevBundle simulatedMevBundle = _bundleSimulator.Simulate(parent, gasLimit, bundle); UInt256 tailGas = _tailGasPriceCalculator.Calculate(parent, 0, simulatedMevBundle.GasUsed); if (simulatedMevBundle.MevEquivalentGasPrice > bestMevEquivalentPrice && simulatedMevBundle.MevEquivalentGasPrice > tailGas) { if (simulatedMevBundle.GasUsed + totalGasUsed <= maxGasUsed) { totalGasUsed += simulatedMevBundle.GasUsed; bestMevEquivalentPrice = simulatedMevBundle.MevEquivalentGasPrice; bestBundle = bundle; } } } if (bestBundle is null) { return(Enumerable.Empty <MevBundle>()); } return(Enumerable.Repeat(bestBundle, 1)); }
public void Test(TestJson testJson) { TestSimulator sim = new(testJson); ITailGasPriceCalculator tailGas = testJson.TailGasType switch { TailGasType.Any => new ConstantTailGasPriceCalculator(0.GWei()), TailGasType.Constant80 => new ConstantTailGasPriceCalculator(80.GWei()), _ => throw new ArgumentOutOfRangeException() }; IBundleSource selector = testJson.SelectorType switch { SelectorType.V1 => new V1Selector(sim, sim), SelectorType.V2 => new V2Selector(sim, sim, tailGas, testJson.MaxGasLimitRatio), _ => throw new ArgumentOutOfRangeException() }; IEnumerable <MevBundle> selected = selector.GetBundles(_blockHeader, testJson.GasLimit !.Value); SimulatedMevBundle[]? simulated = sim.Simulate(_blockHeader, testJson.GasLimit !.Value, selected).ToArray(); long totalGasUsedByBundles = simulated.Sum(s => s.GasUsed); long gasLeftForTransactions = testJson.GasLimit !.Value - totalGasUsedByBundles; IEnumerable <Transaction>?txs = sim.GetTransactions(_blockHeader, gasLeftForTransactions); UInt256 totalProfit = simulated.Aggregate <SimulatedMevBundle, UInt256>(0, (profit, x) => profit + x.Profit); totalProfit += txs.Aggregate <Transaction, UInt256>(0, (profit, x) => profit + (x.GasPrice * (UInt256)x.GasLimit)); totalProfit.Should().Be(testJson.OptimalProfit !.Value, testJson.Description); }
void Add <T>(IBundleSource <T> bundleSource) where T : Bundle { var result = bundleSource.GetBundles(GetBundleFactory <T>(), application); Tuple <object, CreateBundleContainer> existingTuple; if (bundleSourceResultsByType.TryGetValue(typeof(T), out existingTuple)) { var existingResult = (IEnumerable <T>)existingTuple.Item1; var existingAction = existingTuple.Item2; // Concat the two bundle collections. // Keep the existing initialization action. bundleSourceResultsByType[typeof(T)] = Tuple.Create( (object)existingResult.Concat(result), existingAction ); } else { bundleSourceResultsByType[typeof(T)] = Tuple.Create <object, CreateBundleContainer>( result, CreateBundleContainer <T> ); } }
public IEnumerable <Transaction> GetTransactions(BlockHeader parent, long gasLimit) { using CancellationTokenSource cancellationTokenSource = new(_timeout); Task <IEnumerable <MevBundle> > bundlesTasks = _bundleSource.GetBundles(parent, _timestamper.UnixTime.Seconds, gasLimit, cancellationTokenSource.Token); IEnumerable <MevBundle> bundles = bundlesTasks.Result; // Is it ok as it will timeout on cancellation token and not create a deadlock? foreach (MevBundle bundle in bundles) { foreach (BundleTransaction transaction in bundle.Transactions) { yield return(transaction); } } }
public IEnumerable <MevBundle> GetBundles(BlockHeader parent, long gasLimit) { MevBundle?bestBundle = null; long bestAdjustedGasPrice = 0; foreach (var bundle in _bundleSource.GetBundles(parent, gasLimit)) { SimulatedMevBundle simulatedMevBundle = _bundleSimulator.Simulate(parent, gasLimit, bundle); if (simulatedMevBundle.AdjustedGasPrice > bestAdjustedGasPrice) { bestBundle = bundle; } } if (bestBundle is null) { return(Enumerable.Empty <MevBundle>()); } return(Enumerable.Repeat(bestBundle, 1)); }