Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fees): take max of fee history and provided prio values #410

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_estimate, fee_history_estimate) = tokio::try_join!(
self.provider.get_max_priority_fee(),
self.calculate_from_fee_history()
)?;
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
}