Skip to content

Instantly share code, notes, and snippets.

@Akagi201
Created June 13, 2024 03:06
Show Gist options
  • Save Akagi201/22b868970e51b1bcc70bd3217c160525 to your computer and use it in GitHub Desktop.
Save Akagi201/22b868970e51b1bcc70bd3217c160525 to your computer and use it in GitHub Desktop.
def calc_next_block_base_fee(gas_used, gas_limit, base_fee, elasticity_multiplier, max_change_denominator):
# Calculate the target gas by dividing the gas limit by the elasticity multiplier.
gas_target = gas_limit // elasticity_multiplier
if gas_used == gas_target:
return base_fee
elif gas_used > gas_target:
# Calculate the increase in base fee based on the formula defined by EIP-1559.
return base_fee + max(
1,
base_fee * (gas_used - gas_target)
// (gas_target * max_change_denominator)
)
else:
# Calculate the decrease in base fee based on the formula defined by EIP-1559.
return base_fee - (
base_fee * (gas_target - gas_used)
// (gas_target * max_change_denominator)
)
def main():
# ethereum mainnet
# base fee increase
base_fee = 1000
gas_limit = 30_000_000
gas_used = 30_000_000
elasticity_multiplier = 2
max_change_denominator = 8
next_base_fee = calc_next_block_base_fee(gas_used, gas_limit, base_fee, elasticity_multiplier, max_change_denominator)
print("ethereum, base fee max increase:", (next_base_fee-base_fee)/base_fee) # 0.125
# base fee decrease
base_fee = 1000
gas_limit = 30_000_000
gas_used = 0
elasticity_multiplier = 2
max_change_denominator = 8
next_base_fee = calc_next_block_base_fee(gas_used, gas_limit, base_fee, elasticity_multiplier, max_change_denominator)
print("ethereum, base fee max decrease:", (next_base_fee-base_fee)/base_fee) # -0.125
# optimism
# base fee increase
base_fee = 1000
gas_limit = 60_000_000
gas_used = 60_000_000
elasticity_multiplier = 6
max_change_denominator = 50
next_base_fee = calc_next_block_base_fee(gas_used, gas_limit, base_fee, elasticity_multiplier, max_change_denominator)
print("optimism, base fee max increase:", (next_base_fee-base_fee)/base_fee) # 0.1
# base fee decrease
base_fee = 1000
gas_limit = 60_000_000
gas_used = 0
elasticity_multiplier = 6
max_change_denominator = 50
next_base_fee = calc_next_block_base_fee(gas_used, gas_limit, base_fee, elasticity_multiplier, max_change_denominator)
print("optimism, base fee max decrease:", (next_base_fee-base_fee)/base_fee) # -0.02
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment