Skip to content

Commit

Permalink
fix(polygon): move fallback code to polygon file
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfourzerofour committed Sep 21, 2023
1 parent f9ae585 commit 845ae92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
16 changes: 3 additions & 13 deletions crates/sim/src/gas/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -272,19 +272,9 @@ impl<P: Provider> FeeEstimator<P> {

async fn get_priority_fee(&self) -> anyhow::Result<U256> {
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
Expand Down
27 changes: 21 additions & 6 deletions crates/sim/src/gas/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P> {
provider: Arc<P>,
gas_category: GasCategory,
chain_id: u64,
}

#[derive(Clone, Copy, Deserialize, PartialEq)]
Expand All @@ -27,10 +28,11 @@ impl<P> Polygon<P>
where
P: Provider,
{
pub(crate) fn new(provider: Arc<P>) -> Self {
pub(crate) fn new(provider: Arc<P>, chain_id: u64) -> Self {
Self {
provider,
gas_category: GasCategory::Standard,
chain_id,
}
}

Expand Down Expand Up @@ -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<U256>], base_fee_per_gas: U256) -> GasEstimate {
fn calculate_estimate_from_rewards(
reward: &[Vec<U256>],
base_fee_per_gas: U256,
chain_id: u64,
) -> GasEstimate {
let (sum, count): (U256, U256) = reward
.iter()
.filter(|b| !b[0].is_zero())
Expand All @@ -91,6 +99,13 @@ fn calculate_estimate_from_rewards(reward: &[Vec<U256>], 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 {
Expand Down

0 comments on commit 845ae92

Please sign in to comment.