Introduction to Gas
On Ethereum, all transactions involve executing specific operations that change the state of the network, such as transferring account balances, deploying new contracts, or interacting with existing contracts. These operations consume computational resources, leading to a cost known as “Gas.” Users initiating transactions on Ethereum need to pay a fee for these computational resources in the form of Gas. Gas measures the amount of computational resources required to execute a transaction.
Each operation in a transaction consumes a certain amount of Gas, with more complex operations requiring higher computational resources, thus necessitating more Gas. This design ensures that contract transactions can be halted, preventing endless consumption of computational resources. Through Gas, Ethereum transactions can be controlled and prevented from consuming resources indefinitely.
Formula to calculate Ethereum Gas Fee
In Ethereum, the transaction fee (Gas Fee) can be expressed by the following formula:
Total Fee = Gas Price * Gas Used
After the London Upgrade on Ethereum, the calculation method for transaction fees changed. The Gas Price now consists of a Base Fee and a Priority Fee.
Base Fee
Each block has a base fee, and when a sender initiates a transaction, the set base fee cannot be lower than the base fee of that blockchain. The base fee of a block is related to the gas consumed in the previous block. Once a transaction is included in a block, its base fee is directly burned:
Burned Gas Fee = Base Fee * Gas Used
Priority Fee
In Ethereum, if every transaction pays the same unit fee, they need to queue up for processing. However, users can pay miners a tip to prioritize their transactions. This tip is known as the Priority Fee. Unlike the base fee, all fees generated by the priority fee go to miners:
Miner Tip = Priority Fee * Gas Used
Therefore, the total fee can be rewritten as:
Total Fee = (Base Fee + Priority Fee) * Gas Used
Max Fee
For users, setting the base fee and priority fee for each transaction can be cumbersome. Hence, the concept of “Max Fee Per Gas” emerged. Since users cannot determine the base fee set by the previous block, they can directly set a “Max Fee Per Gas” (masFeePerGas) to limit the cost they are willing to pay per unit of gas during transaction execution. To ensure a transaction’s successful execution, the Max Fee must exceed the sum of the Base Fee and Priority Fee. Any excess amount beyond this sum will be refunded to the transaction sender after the transaction is execute