Ethereum is the second largest cryptocurrency by market cap today and with the introduction of smart contracts, it is regarded as a generation 2 cryptocurrency which allows a lot more utility than just a way to send and receive currency. To oversimplify, Ethereum introduced two different types of accounts — regular accounts operated by the owner of the private key and smart contract accounts which operate based on the code deployed to the network. Since any arbitrary code can be run on the Ethereum blockchain, to discourage unnecessary resource usage and to compensate the miners the concept of "gas" was introduced.
Note: Ethereum is in active development and these things keep changing. The post below details "gas" before and after the London Upgrade.
To understand the specifics of gas, it is important to understand blocks and block size in context of Ethereum. Blocks can be thought of as a batch of transactions which are cryptographically secured and linked together in a chain to establish an open and shared ledger. Basically, any transaction that happens on the Ethereum blockchain needs to be added to a block in order for it to be confirmed. These blocks need to be limited in size because if they are allowed to be arbitrarily large without any limits, only the nodes that have the capability of processing that level of computation would be able to function.
You can think of a block as a train going from A to B every few seconds. You'll need to buy a ticket to get on this train. If at some point, a lot of people want to get on the train, the ticket prices shoot up. Travellers who can't afford the high ticket cost would need to wait till the rush hour ends. This very loosely abstracts the issue of high gas fees on the Ethereum network. Let's dive more into the specifics.
Every transaction on the Ethereum blockchain requires gas so that the miners can be compensated and since Ethereum allows running actual code on the blockchain, gas discourages bad actors by making it economically infeasible to abuse the resources. The best analogy to understand gas would be to think about where the term comes from. Let's say you are travelling from San Francisco to Los Angeles. You'll need to cover a distance of ~600 KM. Imagine further that your car gives you a mileage of 60 KM per gallon. You'll need 10 gallons of gas to get to your destination. If the gas costs $3 per gallon, you'll end up paying $30 to get from SF to LA.
Similarly, on the Ethereum blockchain, each transaction has a required amount of gas units (limit). This is equivalent to the 10 gallons of gas required to travel 600 KM in our analogy. Unlike our distance analogy, you can potentially change it but it is best not to alter it and go with the wallet's suggestion. A standard ETH transfer requires 21,000 units of gas.
What you can alter is how much you are willing to pay for each unit of that gas. The cost per unit of gas is measured in
Gwei and it keeps fluctuating based on network congestion (similar to prices of fuel shooting up when the demand is high and supply is low).
Gwei is simply a denomination of ETH equal to 0.000000001 ETH. Take a look at Etherscan's gastracker. At the time of writing, the average gas price was 50 Gwei, meaning that each unit of gas is expected to cost 0.000000050 ETH.
If you wanted to transfer 1 ETH to your friend, you'll need 21,000 gas units each costing 0.000000050 ETH for a total cost of 0.00105 ETH. Before the London fork, 1.00105 ETH would be deducted from your balance. 1 ETH would be sent to your friend and 0.00105 ETH would be sent to the miner for their services.
Based on EIP-1559, Ethereum's gas model was overhauled in favor of making gas prices predictable and Ethereum deflationary. With the upgrade, Ethereum introduced a block base fee and a priority fee. The block-sizes were also made variable.
Essentially, each block has a base fee which is the minimum price per gas unit required to be included in the block. It is determined by the network based on the activity in the previous block. The base fee gets burnt which reduces the overall supply of ETH making it deflationary. Since miners won't be receiving the base fee, the tip serves as their compensation for mining the blocks.
Consider the same example as above. You want to transfer 1 ETH to your friend. Let's say the base fee is 50 Gwei and you include a tip (priority fee) of 10 Gwei for the miner. You would need 21,000 gas units costing (base fee + priority fee) per unit. Total gas fee would amount to, 21,000 * [(50 + 10) * 0.000000001] = 0.00126 ETH. 1.00126 ETH would be deducted from your balance. 1 ETH would be sent to your friend, 0.00021 ETH will be sent to the miner and 0.00105 ETH will be burnt.
To further understand this, let's look at a real world example. Here's an interesting one involving BAYC #3547. Essentially, the owner of this particular NFT set it for sale at a price of 0.75 ETH instead of 75 ETH and someone swooped in with a high fee to buy this unreal deal. This incident was covered by Bloomberg and a bunch of other media outlets.
Take a look at the transaction on Etherscan.
You may note that the value of the transaction was only 0.75 ETH whereas the Transaction Fee paid was ~8.5 ETH. The wallet originally suggested that this action would require 311,208 units of gas but only 256,075 (82.28%) ended up being used. The base fee of the block was 94.923309777 Gwei.
The amount burnt was 0.024307486551145275 ETH which is equal to: 256,075 (gas units used) * 94.923309777 Gwei (base fee) * 0.000000001 (convert Gwei to ETH).
The buyer added a maximum priority fee of 33,257.457060927 Gwei. This equates to 8.5 ETH tip for the miner based on the gas units used. With a tip that high, the miners are bound to include this transaction before anything else.
Hopefully, the concept of base fee and priority fee is clear by this point. How does all this make the gas fee predictable? Well, with the London upgrade, Ethereum supports variable block sizes. The target size of the block is 15 million gas. The block size limit is 30 million gas. For example, the above BAYC transaction would have occupied around ~0.85% of the block if the size was close to the limit.
If the included gas in the previous block exceeds the target amount, the base fee for the next block will be increased by a maximum of 12.5%. This allows a way of predicting the base fee for the next block. If the blocks continue to hit limits each time, because of exponential growth of the base fee, it would be economically infeasible for the block size to continue that trend forever.
Whenever you attempt to perform a transction on the Ethereum blockchain, whether it be minting a new NFT, staking a token or sending ETH to an account, you'll need to pay gas. You might have seen your MetaMask wallet suggest the gas amount and simply ask you to confirm. Here's the gas fee suggestion for an ETH transfer transaction.
MetaMask is suggesting that I allow a maximum of 86.3 Gwei for each of the 21,000 units of gas. This equates to 0.0018123 ETH. Since this is a transaction I don't need done urgently, I can actually reduce the suggested Max fee based on this week's gas trends. Based on this week's average gas prices, I will try to find the lowest average gas fee. If you don't mind risking the transaction failing, you can even look at the absolute lowest gas fee paid this week.
Based on the graph above, the minimum average gas fee was 51 Gwei and the absolute minimum for the week was 1 Gwei. Since I have no urgency for this transfer, I can actually reduce the Max fee from MetaMask's suggested 86.3 Gwei to 55 Gwei (avg. minimum + padding). Overnight when the network congestion decreases, the base fee will fall and my transaction will get picked up.
This may feel like a lot of work for a few cents but for more complicated transactions and for times when gas fees are high, you'd end up paying a lot more than you need to. Gas fees add up quite a bit and this is one way to avoid unnecessary overspending.
In the future, ETH 2.0's proof of stake model should eliminate these concerns and make this post obsolete.