Skip to content

Commit

Permalink
feat(eip1559): Support Optimism Canyon hardfork (alloy-rs#1010)
Browse files Browse the repository at this point in the history
* feat(eip1559): Support Optimism Canyon harfork

* fix
  • Loading branch information
moricho authored and ben186 committed Jul 27, 2024
1 parent 62a30f2 commit 0f3a3d0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/eips/src/eip1559/basefee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use crate::{
eip1559::constants::{
BASE_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR, DEFAULT_ELASTICITY_MULTIPLIER,
OP_MAINNET_EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR_CANYON,
OP_MAINNET_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR,
OP_MAINNET_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
OP_SEPOLIA_EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR_CANYON,
OP_SEPOLIA_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR,
OP_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
},
Expand Down Expand Up @@ -44,6 +46,14 @@ impl BaseFeeParams {
}
}

/// Get the base fee parameters for Optimism Mainnet (post Canyon)
pub const fn optimism_canyon() -> Self {
Self {
max_change_denominator: OP_MAINNET_EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR_CANYON,
elasticity_multiplier: OP_MAINNET_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
}
}

/// Get the base fee parameters for Optimism Sepolia
pub const fn optimism_sepolia() -> Self {
Self {
Expand All @@ -52,6 +62,14 @@ impl BaseFeeParams {
}
}

/// Get the base fee parameters for Optimism Sepolia (post Canyon)
pub const fn optimism_sepolia_canyon() -> Self {
Self {
max_change_denominator: OP_SEPOLIA_EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR_CANYON,
elasticity_multiplier: OP_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
}
}

/// Get the base fee parameters for Base Sepolia
pub const fn base_sepolia() -> Self {
Self {
Expand All @@ -60,6 +78,14 @@ impl BaseFeeParams {
}
}

/// Get the base fee parameters for Base Sepolia (post Canyon)
pub const fn base_sepolia_canyon() -> Self {
Self {
max_change_denominator: OP_SEPOLIA_EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR_CANYON,
elasticity_multiplier: BASE_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER,
}
}

/// Calculate the base fee for the next block based on the EIP-1559 specification.
///
/// See also [calc_next_block_base_fee]
Expand Down
6 changes: 6 additions & 0 deletions crates/eips/src/eip1559/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub const DEFAULT_ELASTICITY_MULTIPLIER: u64 = 2;
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_SEPOLIA_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR: u128 = 50;

/// Base fee max change denominator for Optimism Sepolia as defined in the Optimism Canyon hardfork.
pub(crate) const OP_SEPOLIA_EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR_CANYON: u128 = 250;

/// Base fee max change denominator for Optimism Sepolia as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER: u128 = 6;
Expand All @@ -38,6 +41,9 @@ pub(crate) const OP_SEPOLIA_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER: u128 = 6;
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_MAINNET_EIP1559_DEFAULT_BASE_FEE_MAX_CHANGE_DENOMINATOR: u128 = 50;

/// Base fee max change denominator for Optimism Mainnet as defined in the Optimism Canyon hardfork.
pub(crate) const OP_MAINNET_EIP1559_BASE_FEE_MAX_CHANGE_DENOMINATOR_CANYON: u128 = 250;

/// Base fee max change denominator for Optimism Mainnet as defined in the Optimism
/// [transaction costs](https://community.optimism.io/docs/developers/build/differences/#transaction-costs) doc.
pub(crate) const OP_MAINNET_EIP1559_DEFAULT_ELASTICITY_MULTIPLIER: u128 = 6;
Expand Down
32 changes: 32 additions & 0 deletions crates/eips/src/eip1559/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ mod tests {
}
}

#[test]
fn calculate_optimism_canyon_base_fee_success() {
let base_fee = [
1000000000, 1000000000, 1000000000, 1072671875, 1059263476, 1049238967, 1049238967, 0,
1, 2,
];
let gas_used = [
10000000, 10000000, 10000000, 9000000, 10001000, 0, 10000000, 10000000, 10000000,
10000000,
];
let gas_limit = [
10000000, 12000000, 14000000, 10000000, 14000000, 2000000, 18000000, 18000000,
18000000, 18000000,
];
let next_base_fee = [
1020000009, 1016000000, 1013142859, 1091550909, 1073187043, 1045042012, 1059031864, 1,
2, 3,
];

for i in 0..base_fee.len() {
assert_eq!(
next_base_fee[i],
calc_next_block_base_fee(
gas_used[i] as u128,
gas_limit[i] as u128,
base_fee[i] as u128,
BaseFeeParams::optimism_canyon(),
) as u64
);
}
}

#[test]
fn calculate_base_sepolia_base_fee_success() {
let base_fee = [
Expand Down

0 comments on commit 0f3a3d0

Please sign in to comment.