Created
June 15, 2022 08:46
-
-
Save liuyangc3/6b6359755b15811be5b6ed1acfaaa7d2 to your computer and use it in GitHub Desktop.
Example of how a block's base gas fee is calculated on Ethereum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reference: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md | |
BASE_FEE_MAX_CHANGE_DENOMINATOR = 8 | |
ELASTICITY_MULTIPLIER = 2 | |
# Calculate expected base fee for block 12965001 | |
current_block = 12965000 # London Upgrade block | |
# Get the following values from Etherscan: https://etherscan.io/block/12965000 | |
parent_base_fee_per_gas = 1000000000 # 1 Gwei | |
parent_gas_target = 15014561 | |
parent_gas_used = 30025257 | |
if parent_gas_used == parent_gas_target: | |
# Perfect, gas target was met, expected base fee stays the same | |
expected_base_fee_per_gas = parent_base_fee_per_gas | |
elif parent_gas_used > parent_gas_target: | |
# Spent more gas, expected base fee increases | |
gas_used_delta = parent_gas_used - parent_gas_target | |
base_fee_per_gas_delta = max(parent_base_fee_per_gas * gas_used_delta // parent_gas_target // BASE_FEE_MAX_CHANGE_DENOMINATOR, 1) | |
expected_base_fee_per_gas = parent_base_fee_per_gas + base_fee_per_gas_delta | |
else: | |
# Spent less gas, expected base fee decreases | |
gas_used_delta = parent_gas_target - parent_gas_used | |
base_fee_per_gas_delta = parent_base_fee_per_gas * gas_used_delta // parent_gas_target // BASE_FEE_MAX_CHANGE_DENOMINATOR | |
expected_base_fee_per_gas = parent_base_fee_per_gas - base_fee_per_gas_delta | |
# In this example expected_base_fee_per_gas = 1124967822 (1.124967822 Gwei) | |
# Answer matches block exactly: https://etherscan.io/block/12965001 | |
print("Expected base fee per gas for block " + str(current_block + 1) + ": " + str(expected_base_fee_per_gas)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment