Skip to content

Commit

Permalink
fix(fees): take max of fee history and provided prio values
Browse files Browse the repository at this point in the history
  • Loading branch information
dancoombs committed Sep 22, 2023
1 parent f766b09 commit 10499d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/common/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ impl<P: ProviderLike> FeeEstimator<P> {
if POLYGON_CHAIN_IDS.contains(&self.chain_id) {
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)
gas_oracle.estimate_priority_fee().await
} else if self.use_bundle_priority_fee {
self.provider.get_max_priority_fee().await
} else {
Expand Down
47 changes: 18 additions & 29 deletions src/common/polygon.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;
use std::{cmp, sync::Arc};

use ethers::{
prelude::gas_oracle::GasCategory,
types::{BlockNumber, Chain, U256},
};
use futures_util::TryFutureExt;
use serde::Deserialize;

use crate::common::types::ProviderLike;
Expand Down Expand Up @@ -52,37 +51,30 @@ where
}

/// Estimates max and priority gas and converts to U256
pub(crate) async fn estimate_eip1559_fees(&self) -> anyhow::Result<(U256, U256)> {
let estimate = self.calculate_fees().await?;
Ok((estimate.max_fee, estimate.max_priority_fee))
pub(crate) async fn estimate_priority_fee(&self) -> anyhow::Result<U256> {
let provider_fut = self.provider.get_max_priority_fee();
let fee_history_fut = self.calculate_from_fee_history();
let (provider_estimate, fee_history_estimate) =
tokio::try_join!(provider_fut, fee_history_fut)?;
Ok(cmp::max(provider_estimate, fee_history_estimate))
}

/// Perform a request to the gas price API and deserialize the response.
pub(crate) async fn calculate_fees(&self) -> anyhow::Result<GasEstimate> {
let gas_percentile = Vec::from([self.gas_category_percentile()]);
let base_fee_fut = self.provider.get_base_fee();
let fee_history_fut = self
// Perform a request to the gas price API and deserialize the response.
async fn calculate_from_fee_history(&self) -> anyhow::Result<U256> {
let fee_history = self
.provider
.fee_history(15, BlockNumber::Latest, &gas_percentile);
let (base_fee_per_gas, fee_history) = tokio::try_join!(
base_fee_fut,
fee_history_fut.map_err(|e| anyhow::anyhow!("Failed to get fee history {e:?}"))
)?;

let estimate =
calculate_estimate_from_rewards(&fee_history.reward, base_fee_per_gas, self.chain_id);

Ok(estimate)
.fee_history(15, BlockNumber::Latest, &[self.gas_category_percentile()])
.await?;
Ok(calculate_estimate_from_rewards(
&fee_history.reward,
self.chain_id,
))
}
}

/// 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,
chain_id: u64,
) -> GasEstimate {
fn calculate_estimate_from_rewards(reward: &[Vec<U256>], chain_id: u64) -> U256 {
let (sum, count): (U256, U256) = reward
.iter()
.filter(|b| !b[0].is_zero())
Expand All @@ -105,8 +97,5 @@ fn calculate_estimate_from_rewards(
average = fallback;
}

GasEstimate {
max_priority_fee: average,
max_fee: base_fee_per_gas + average,
}
average
}

0 comments on commit 10499d7

Please sign in to comment.