From 24f388bba4d3d7711aad80884e0fe3fe4889ed00 Mon Sep 17 00:00:00 2001 From: 0xfourzerofour Date: Thu, 21 Sep 2023 17:07:03 -0400 Subject: [PATCH] feat(polygon): add fallback for max priority --- src/common/gas.rs | 4 ++-- src/common/polygon.rs | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/common/gas.rs b/src/common/gas.rs index 7f7939b8d..e4c51b106 100644 --- a/src/common/gas.rs +++ b/src/common/gas.rs @@ -210,8 +210,8 @@ 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?; Ok(fees.1) } else if self.use_bundle_priority_fee { diff --git a/src/common/polygon.rs b/src/common/polygon.rs index 61bab96cf..6a7669416 100644 --- a/src/common/polygon.rs +++ b/src/common/polygon.rs @@ -3,16 +3,20 @@ use std::sync::Arc; use ethers::{ prelude::gas_oracle::{GasCategory, Result}, providers::ProviderError, - types::{BlockNumber, U256}, + types::{BlockNumber, Chain, U256}, }; use serde::Deserialize; use crate::common::types::ProviderLike; +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)] @@ -25,10 +29,11 @@ impl

Polygon

where P: ProviderLike, { - 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, } } @@ -68,14 +73,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()) @@ -89,6 +100,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 {