Skip to content

Commit 1610c13

Browse files
authored
chore: use alloy calc next block base fee (#7614)
1 parent f840dbd commit 1610c13

File tree

3 files changed

+5
-26
lines changed

3 files changed

+5
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/anvil/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ethers = { workspace = true, features = ["rustls", "ws", "ipc", "optimism"] }
3939
alloy-primitives = { workspace = true, features = ["serde"] }
4040
alloy-consensus = { workspace = true, features = ["k256", "kzg"] }
4141
alloy-network.workspace = true
42+
alloy-eips.workspace = true
4243
alloy-rlp.workspace = true
4344
alloy-signer = { workspace = true, features = ["eip712"] }
4445
alloy-signer-wallet = { workspace = true, features = ["mnemonic"] }

crates/anvil/src/eth/fees.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::eth::{
22
backend::{info::StorageInfo, notifications::NewBlockNotifications},
33
error::BlockchainError,
44
};
5+
use alloy_eips::{calc_next_block_base_fee, eip1559::BaseFeeParams};
56
use alloy_primitives::B256;
67
use anvil_core::eth::transaction::TypedTransaction;
78
use foundry_evm::revm::primitives::SpecId;
@@ -28,11 +29,8 @@ pub const INITIAL_GAS_PRICE: u128 = 1_875_000_000;
2829
/// Bounds the amount the base fee can change between blocks.
2930
pub const BASE_FEE_CHANGE_DENOMINATOR: u128 = 8;
3031

31-
/// Elasticity multiplier as defined in [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559)
32-
pub const EIP1559_ELASTICITY_MULTIPLIER: u128 = 2;
33-
3432
pub fn default_elasticity() -> f64 {
35-
1f64 / BASE_FEE_CHANGE_DENOMINATOR as f64
33+
1f64 / BaseFeeParams::ethereum().elasticity_multiplier as f64
3634
}
3735

3836
/// Stores the fee related information
@@ -127,28 +125,7 @@ impl FeeManager {
127125
if self.base_fee() == 0 {
128126
return 0
129127
}
130-
calculate_next_block_base_fee(gas_used, gas_limit, last_fee_per_gas)
131-
}
132-
}
133-
134-
/// Calculate base fee for next block. [EIP-1559](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md) spec
135-
pub fn calculate_next_block_base_fee(gas_used: u128, gas_limit: u128, base_fee: u128) -> u128 {
136-
let gas_target = gas_limit / EIP1559_ELASTICITY_MULTIPLIER;
137-
138-
if gas_used == gas_target {
139-
return base_fee
140-
}
141-
if gas_used > gas_target {
142-
let gas_used_delta = gas_used - gas_target;
143-
let base_fee_delta =
144-
std::cmp::max(1, base_fee * gas_used_delta / gas_target / BASE_FEE_CHANGE_DENOMINATOR);
145-
base_fee + base_fee_delta
146-
} else {
147-
let gas_used_delta = gas_target - gas_used;
148-
let base_fee_per_gas_delta =
149-
base_fee * gas_used_delta / gas_target / BASE_FEE_CHANGE_DENOMINATOR;
150-
151-
base_fee.saturating_sub(base_fee_per_gas_delta)
128+
calc_next_block_base_fee(gas_used, gas_limit, last_fee_per_gas, BaseFeeParams::ethereum())
152129
}
153130
}
154131

0 commit comments

Comments
 (0)