Skip to content

Commit

Permalink
feat(mempool): impl calculate-gas-price according to EIP 1559
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Jul 30, 2024
1 parent aef480c commit d3d087d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@ pub struct BlockInfo {
pub use_kzg_da: bool,
}

impl BlockInfo {
/// Calculate the base fee for the next block according to EIP-1559.
///
/// # Parameters
/// - `current_price`: The base fee of the current block.
/// - `current_gas_used`: The total gas used in the current block.
/// - `current_gas_target`: The target gas usage per block (usually half of the gas limit).
///
/// # Returns
/// The price for the next block.
pub fn calculate_gas_price(
current_price: u64,
current_gas_used: u64,
current_gas_target: u64,
) -> u64 {
const BASE_FEE_MAX_CHANGE_DENOMINATOR: u64 = 2;

let gas_delta = current_gas_used.abs_diff(current_gas_target);
let price_change =
(current_price * gas_delta) / current_gas_target / BASE_FEE_MAX_CHANGE_DENOMINATOR;

// Calculate the new price
if current_gas_used > current_gas_target {
current_price + price_change
} else {
current_price - price_change
}
}
}

#[derive(Clone, Debug)]
pub struct GasPrices {
pub eth_l1_gas_price: NonZeroU128, // In wei.
Expand Down

0 comments on commit d3d087d

Please sign in to comment.