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

modification: Eip1559Estimation return type #352

Merged
merged 2 commits into from
Mar 21, 2024
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
12 changes: 6 additions & 6 deletions crates/provider/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
chain::ChainStreamPoller,
heart::{Heartbeat, HeartbeatHandle, PendingTransaction, PendingTransactionConfig},
utils::{self, EstimatorFunction},
utils::{self, Eip1559Estimation, EstimatorFunction},
PendingTransactionBuilder,
};
use alloy_json_rpc::{RpcParam, RpcReturn};
Expand Down Expand Up @@ -546,9 +546,9 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync
) -> TransportResult<()> {
let gas = self.estimate_eip1559_fees(estimator).await;

gas.map(|(max_fee_per_gas, max_priority_fee_per_gas)| {
tx.set_max_fee_per_gas(max_fee_per_gas);
tx.set_max_priority_fee_per_gas(max_priority_fee_per_gas);
gas.map(|estimate| {
tx.set_max_fee_per_gas(estimate.max_fee_per_gas);
tx.set_max_priority_fee_per_gas(estimate.max_priority_fee_per_gas);
})
}

Expand Down Expand Up @@ -761,7 +761,7 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync
async fn estimate_eip1559_fees(
&self,
estimator: Option<EstimatorFunction>,
) -> TransportResult<(U256, U256)> {
) -> TransportResult<Eip1559Estimation> {
let base_fee_per_gas = match self.get_block_by_number(BlockNumberOrTag::Latest, false).await
{
Ok(Some(block)) => match block.header.base_fee_per_gas {
Expand Down Expand Up @@ -796,7 +796,7 @@ pub trait Provider<N: Network, T: Transport + Clone = BoxTransport>: Send + Sync
)
};

Ok((max_fee_per_gas, max_priority_fee_per_gas))
Ok(Eip1559Estimation { max_fee_per_gas, max_priority_fee_per_gas })
}

/// Get the account and storage values of the specified account including the merkle proofs.
Expand Down
9 changes: 9 additions & 0 deletions crates/provider/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ pub const EIP1559_FEE_ESTIMATION_REWARD_PERCENTILE: f64 = 20.0;
/// An estimator function for EIP1559 fees.
pub type EstimatorFunction = fn(U256, &[Vec<U256>]) -> (U256, U256);

/// Return type of EIP1155 gas fee estimator.
#[derive(Debug, Clone, Copy)]
pub struct Eip1559Estimation {
/// The base fee per gas.
pub max_fee_per_gas: U256,
/// The max priority fee per gas.
pub max_priority_fee_per_gas: U256,
}

fn estimate_priority_fee(rewards: &[Vec<U256>]) -> U256 {
let mut rewards =
rewards.iter().filter_map(|r| r.first()).filter(|r| **r > U256::ZERO).collect::<Vec<_>>();
Expand Down
Loading