From 845ae92eeab9e62ce2960727400eb51a5568e096 Mon Sep 17 00:00:00 2001 From: 0xfourzerofour Date: Thu, 21 Sep 2023 16:02:47 -0400 Subject: [PATCH] fix(polygon): move fallback code to polygon file --- crates/sim/src/gas/gas.rs | 16 +++------------- crates/sim/src/gas/polygon.rs | 27 +++++++++++++++++++++------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/crates/sim/src/gas/gas.rs b/crates/sim/src/gas/gas.rs index a1ed0f923..df2c88f19 100644 --- a/crates/sim/src/gas/gas.rs +++ b/crates/sim/src/gas/gas.rs @@ -13,7 +13,7 @@ use rundler_types::{ use rundler_utils::math; use tokio::try_join; -use super::polygon::{Polygon, MAINNET_MAX_PRIORITY_FEE_DEFAULT, MUMBAI_MAX_PRIORITY_FEE_DEFAULT}; +use super::polygon::Polygon; // Gas overheads for user operations // used in calculating the pre-verification gas @@ -272,19 +272,9 @@ impl FeeEstimator

{ async fn get_priority_fee(&self) -> anyhow::Result { if POLYGON_CHAIN_IDS.contains(&self.chain_id) { - let gas_oracle = Polygon::new(Arc::clone(&self.provider)).category(GasCategory::Fast); - + let gas_oracle = + Polygon::new(Arc::clone(&self.provider), self.chain_id).category(GasCategory::Fast); let fees = gas_oracle.estimate_eip1559_fees().await?; - - if fees.1.is_zero() { - let fallback = match self.chain_id { - x if x == Chain::Polygon as u64 => MAINNET_MAX_PRIORITY_FEE_DEFAULT, - x if x == Chain::PolygonMumbai as u64 => MUMBAI_MAX_PRIORITY_FEE_DEFAULT, - _ => 0, - }; - return Ok(fallback.into()); - } - Ok(fees.1) } else if self.use_bundle_priority_fee { self.provider.get_max_priority_fee().await diff --git a/crates/sim/src/gas/polygon.rs b/crates/sim/src/gas/polygon.rs index 7f59aad0e..66109be98 100644 --- a/crates/sim/src/gas/polygon.rs +++ b/crates/sim/src/gas/polygon.rs @@ -3,18 +3,19 @@ use std::sync::Arc; use ethers::{ prelude::gas_oracle::{GasCategory, Result}, providers::ProviderError, - types::{BlockNumber, U256}, + types::{BlockNumber, Chain, U256}, }; use rundler_provider::Provider; use serde::Deserialize; -pub(crate) const MUMBAI_MAX_PRIORITY_FEE_DEFAULT: u64 = 1_500_000_000; -pub(crate) const MAINNET_MAX_PRIORITY_FEE_DEFAULT: u64 = 30_000_000_000; +const MUMBAI_MAX_PRIORITY_FEE_DEFAULT: u64 = 1_500_000_000; +const MAINNET_MAX_PRIORITY_FEE_DEFAULT: u64 = 30_000_000_000; #[derive(Debug)] pub(crate) struct Polygon

{ provider: Arc

, gas_category: GasCategory, + chain_id: u64, } #[derive(Clone, Copy, Deserialize, PartialEq)] @@ -27,10 +28,11 @@ impl

Polygon

where P: Provider, { - pub(crate) fn new(provider: Arc

) -> Self { + pub(crate) fn new(provider: Arc

, chain_id: u64) -> Self { Self { provider, gas_category: GasCategory::Standard, + chain_id, } } @@ -70,14 +72,20 @@ where .fold(U256::from(0), |acc, val| acc.saturating_add(*val)) .div_mod(U256::from(fee_history.base_fee_per_gas.len())); - let estimate = calculate_estimate_from_rewards(&fee_history.reward, base_fee_per_gas); + let estimate = + calculate_estimate_from_rewards(&fee_history.reward, base_fee_per_gas, self.chain_id); + Ok(estimate) } } /// Calculates the estimate based on the index of inner vector /// and skips the average if block is empty -fn calculate_estimate_from_rewards(reward: &[Vec], base_fee_per_gas: U256) -> GasEstimate { +fn calculate_estimate_from_rewards( + reward: &[Vec], + base_fee_per_gas: U256, + chain_id: u64, +) -> GasEstimate { let (sum, count): (U256, U256) = reward .iter() .filter(|b| !b[0].is_zero()) @@ -91,6 +99,13 @@ fn calculate_estimate_from_rewards(reward: &[Vec], base_fee_per_gas: U256) if !count.is_zero() { let (avg, _mod) = average.div_mod(count); average = avg; + } else { + let fallback = match chain_id { + x if x == Chain::Polygon as u64 => MAINNET_MAX_PRIORITY_FEE_DEFAULT.into(), + x if x == Chain::PolygonMumbai as u64 => MUMBAI_MAX_PRIORITY_FEE_DEFAULT.into(), + _ => U256::zero(), + }; + average = fallback; } GasEstimate {