reth_optimism_chainspec/basefee.rs
1//! Base fee related utilities for Optimism chains.
2
3use alloy_consensus::BlockHeader;
4use op_alloy_consensus::{decode_holocene_extra_data, EIP1559ParamError};
5use reth_chainspec::{BaseFeeParams, EthChainSpec};
6use reth_optimism_forks::OpHardforks;
7
8/// Extracts the Holocene 1599 parameters from the encoded extra data from the parent header.
9///
10/// Caution: Caller must ensure that holocene is active in the parent header.
11///
12/// See also [Base fee computation](https://github.com/ethereum-optimism/specs/blob/main/specs/protocol/holocene/exec-engine.md#base-fee-computation)
13pub fn decode_holocene_base_fee<H>(
14 chain_spec: impl EthChainSpec + OpHardforks,
15 parent: &H,
16 timestamp: u64,
17) -> Result<u64, EIP1559ParamError>
18where
19 H: BlockHeader,
20{
21 let (elasticity, denominator) = decode_holocene_extra_data(parent.extra_data())?;
22 let base_fee_params = if elasticity == 0 && denominator == 0 {
23 chain_spec.base_fee_params_at_timestamp(timestamp)
24 } else {
25 BaseFeeParams::new(denominator as u128, elasticity as u128)
26 };
27
28 Ok(parent.next_block_base_fee(base_fee_params).unwrap_or_default())
29}