If Ethereum is a car, the gas is literally the "gas" ⛽
Gas is the fuel using which the operations are done on the Ethereum blockchain. While the transactions are made on the network, the users are required to pay a transaction fee, that is the Gas fee.
Gas is a unit of computation done on the Ethereum network. It denotes the amount of effort that was required to execute a transaction. That's why a fee is charged by the user who wants to execute it, the user pays for the computation effort.
The gas fee is not limited to Ethereum, you will find the same in Polygon as well, basically anything compatible with Ethereum Virtual Machine.
The Gas fee is paid in the networks' native currency, here it is ether aka ETH.
But when you execute a transaction, the Gas fee is not exactly given in ether, it's given in something like gwei. Gwei is a denomination of ETH, just like how a meter is for a kilometer.
The value of 1 Gwei = 0.000000001 ETH = 10^(-9) ETH.
There is another denomination of Gwei, i.e Wei which is the smallest.
1 Wei = 10^(-18) ETH
For example, say the price of 1 gas is 200 Gwei. And for a transaction to send some ETH to another address is 21,000 gas. So your gas fee comes to => 21,000 * 200 = 42,00,000 Gwei = 0.0042 ETH
How the gas price for 1 gas is set is up to the user. In Wallets like Metamask, it gives you a reasonable amount depending on the network conditions. But to make the transaction go faster, you are sometimes prompted to pay more.
The higher the gas price, the more priority is given to that transaction by the miners as they earn a chunk out of it.
Calculating Gas
When a smart contract is deployed on the blockchain, it is compiled into the bytecode and then down to opcodes.
Opcodes are the operations that run directly on the Ethereum Virtual Machine, like Add, Mul, Div, Sub, etc. Each has a fixed gas cost. In a function of the smart contract, the Gas cost is the sum of all its Opcodes costs.
So more complex functions require more gas and simpler functions like sending ether, require less.
The Gas fee that Metamask asks you to pay is an estimate, it is possible that the execution took less gas fee, or you can set a limit on how much of the gas fee should be used, and the unsent amount gets refunded to your wallet.
Gas Limits
Each transaction has a gas limit that the user can specify. So if the execution requires more gas than the specified limit, it will fail and revert.
Similarly, the blockchain network has a limit on the maximum amount of Gas for each block. This is done so that the computation cost doesn't flow out of range and the nodes remain in sync with the network to handle more computationally complex functions.
London Upgrade
This upgrade was implemented to the Ethereum network in 2021 to improve the gas fee estimation and quicker execution of transactions.
Before this, wallets like Metamask will give you an estimate of the gas fee based on the history of the network. Now there is a base gas price fee on every block. This price per unit of gas is calculated on demand for the transaction to be included in a block.
The base fee is burnt to maintain equilibrium on ETH supply and burn rate. Also, a priority fee is charged which is used to reward miners for execution. Higher the priority fee, you know it.
The gas fee is calculated as => gas spent * (base fee + priority fee)
Block Sizes
Earlier the blocks had a fixed gas limit of 15 million gas. Now it can be increased to 30 million, by adjusting the base fee for the next coming block.
Why is the base fee adjusted? Because it is used to maintain the equilibrium between the supply and burn rate of ETH. So the base fee for the next block is either increased or decreased depending on the size of the current block.
The base fee can increase or decrease by a maximum of 12.5% per block if it crosses the 15 million gas limit. This jump in gas prices is the reason why the nodes will not execute 30 million gas blocks constantly.
Because if they do, down the line even a simple transaction will require a lot of gas fees.
That's all :)
Top comments (3)
Great article... what do you think about eth gas fee, will it go down given they have migrated to POS?
I am working on the blog about PoS next up, I'll mention this topic briefly in that.
But soon I will release a detailed article about blockchain sharding and scalability in Blockchains. Do give them a read 👍
Thanks @obasi042 . As for the eth gas fee, that actually depends on a few factors.
First of all Ethereum merge was one of them, but it didn't increase the speed of block creation a lot. Same goes for the gas fee, there is not much change after migration to POS.
The thing is it will be same for some more time, until Ethereum 2.0 launches the blockchain sharding. Then the networks will be more scalable and will be able to attract more node operators.
So as the scalability increases, more users adopt this, the prices are expected to go down. But it will take considerable amount of time.
Just to get a picture of scalability, think like Ethereum can process 10-15 transactions per second, but centralized systems can do like 30,000 transactions per second. That's why scalability is important to drive such decentralized systems.