Post London upgrade which implements EIP-1559, Ethereum has changed how block size and transaction fees works.
Block Gas Limit
Earlier Ethereum block gas limit was calculated using the below formula:
This formula changes the gas limit of blocks by 0.0976% in either direction.
Post London upgrade, the way block gas limit works has changed. This change was implemented to improve user experience during network congestion as users had to wait for long time for their transaction to be included in a block. Now blocks can include more transactions in a block when there is large volume of transactions.
Now each block has a target size of 15 million gas. The block size can increase or decrease based on conjunction of the network. The block size can increase maximum upto 30 million gas. The protocol tries to achieve an average block size of 15 million gas by using an algorithm based on base fee of block.
Ethereum now introduces a base fee for every every block. The base fee mechanism is mainly designed to burn ether and also keep the average block size to 15 million gas. Each transaction in the block has to pay the base fees. The base fee is burnt by the network.
The base fee of current block is proportional the increase or decrease in previous block size w.r.t to target size. The base fee of current block can increase or decrease by maximum 12.5%. So the base fee is calculated this way:
- If the previous block size was 15 million then base fee of the current block will remain same
- If the previous block size was 30 million the base fee of the current block will increase by 12.5%
- If the previous block size was between 15 to 30 million then current block size will increase by less than 12.5%
- If the previous block size was between 0 to 15 million then current block size will decrease by less than 12.5%
The exact value of baseFee is something that you cannot determine when creating a transaction because you don’t know in which block your transaction will be included in and what will be the base fee of the previous block.
Regardless of the baseFee, you also need to pay some ether to the miners to include the transaction in a block. We will now look into how we specify the fee we want to pay to the miners.
EIP-1559 includes two new transaction fields called as maxFeePerGas and maxPriorityFeePerGas for this.
Let’s first look into maxFeePerGas. It is the sum of base fee and miner fee in total you are willing to pay. If you don’t specify this field when creating a transaction then it means you are not paying any miner fee so miners will not mine your transaction. Therefore you must specify this field. So suppose you decide you want to set it to 100 Gwei and baseFee is 80 Gwei then miners take 20 Gwei as their fees.
Now the question is how will you determine the value of maxFeePerGas when creating a transaction? You can decide the miner fee by finding the average miner fee of last few blocks and then increase or decrease the value based on priority of the transaction. But the exact base fee is something complicated to find. For base fee, you can find the base fee of next block and consider the double of that as the base fee. This way you are sure that you have specified sufficient maxFeePerGas to cover both miner fee and base fee.
You can find the base fee of next block using the below code: web3.eth.getBlock(“pending”).then((block) => console.log(block.baseFeePerGas));
But now there is a problem. As you have added double the base fee to the maxFeePerGas so if the base fee is less than that which is going to happen most probably than miners will end up taking more miner fee because the remaining fee will be more than miner fee. To solve this problem maxPriorityFeePerGas field was introduced. This is used to specify the maximum miner fee you are willing to pay so that we don’t end up paying more to the miners and the remaining fee is refunded to the sender account.
The miner fee can be less than the maxPriorityFeePerGas but not more. The miner fee will be less than the maxPriorityFeePerGas when the specified base fee is enough to cover the base fee and needs to use fee from the miner fee to cover the additional base fee.
Ethereum still supports legacy transactions which include gasPrice field. In this case gasPrice is treated as maxFeePerGas. EIP-2718 implements supports for EIP-1557 and other new transaction types without loosing support for legacy transactions.
Monitoring Gas Price
To monitor the base fee and miner fee of the network you can use the below links: