Introduction
Blockchains facilitate incredible things, from transferring money, to building decentralized apps, and storing important data. In contrast to traditional systems, blockchains are maintained by many contributors called miners or validators.
Miners and validators execute transactions in a block, and compute the resulting changes to the network. Transactions in blocks have fees associated with them, called gas fees, that serve as a defense mechanism protecting blockchains from attack. Today’s write-up explains how. Enjoy.
Sections
What is The Halting Problem?
Why Do Blockchains Have Gas Fees?
What is the Gas Limit?
How Do Gas Prices Work?
How Do Wallets Estimate Gas Fees?
How Can I Minimize Gas Fees I Pay?
In Conclusion
*Thank you to Ziad for proofreading.
The Halting Problem
Let’s start with an old computer science problem solved by Alan Turing himself; The Halting Problem. The Halting Problem says that the only way to find out if a program will halt execution at some point (as it should), is to execute that program. There is no way to determine if there will be a result without working to compute that result. This simple discovery is the essence for why gas exists on Ethereum.
Why Blockchains Have Gas Fees
Blockchains must always be operational, which requires miners/validators to continuously execute transactions. These transactions are broadcast in programming languages like Solidity or the lower-level bytecode, which means that transactions are themselves programs. Validators execute transactions and compute the changes our transactions have on the network, but remember the Halting Problem here. A malicious person could send a transaction that never halts, keeps looping, and drains validator resources. Gas serves as the protection against this behavior.
When we send transactions, we must associate them with gas fees we’re willing to pay. This gas cost covers the resources used to compute our transaction - but is also a limit on the resources the transaction can take. If a transaction runs out of gas, execution stops and the transaction is discarded. This is far better than having transactions stuck in loops, draining resources.
How Gas Fees Work
Gas Limit
Gas is measured in units. Each operation that can be performed requires units of gas based on how much effort it requires. Complex transactions like deploying a smart contract or minting an NFT require more effort to compute than a simple transfer of currency. The difference of effort is measured in the different units of gas required.
The units of gas used by a transaction is expressed in its gas limit. The higher the gas limit, the more effort validators can spend executing it. If a transaction requires more effort to execute than the gas allocated to it, the transaction will fail. When transactions fail due to this “out of gas” error, validators stop executing them and keep the gas for the effort they already spent.
The gas limit prevents blockchains from experiencing the Halting Problem. No transaction can execute indefinitely unless it also has an indefinite amount of gas associated. This in unrealistically expensive, especially since as we will see next, gas costs money.
Gas Price
The cost of each unit of gas is measured in sub-units of Ethereum’s currency, called gwei. The cost of a transaction is the gas limit multiplied by the gas price. Transactions can also include an additional tip to the validator, called a priority fee. You can find the gas price at a given time using Etherscan.
1 ETH splits into one billion gwei, but even with such small amounts costs can add up. The price in gwei that a transaction will require is based on current network traffic. These prices are very dynamic. When many people are performing transactions at the same time, the price of gas in gwei tends to go up. This is because blocks are limited in size to 30 million units of gas maximum, and people will outbid each other to ensure their transactions are executed.
Block space is limited, so the price of that space changes with how popular it is at a given point in time. This is the other side of the defense; performing an attack that spams a network to drain it of its resources (known as a DDoS attack) would increase gas prices for each subsequent transaction in that attack, making it increasingly expensive.
A notably different example is Solana which has a fixed gas fee for all transactions. This has proven to not be an adequate defense, causing the chain to stop operating on many occasions.
Gas Estimation
Since gas fees can change both based on transaction complexity and network traffic, they need to be estimated separately for every transaction. These estimates must be done accurately, since how much gas a transaction has will determine if it succeeds or fails. The price can be easily obtained, but the gas limit requires more work. Conveniently, wallets on Ethereum do this work for us automatically using an operation called “estimateGas.”
estimateGas is a function that validators can perform which simulates the transaction offline to determine how many units of gas it requires. When preparing a transaction, wallets automatically connect to services like Infura or Alchemy which have clients (i.e. validator software) that simulate the transaction. This estimation is usually inflated just to be safe, and is how our transactions always know how much gas they require.
If a transaction has more units of gas in its gas limit then the transaction requires, the remaining amount is not used, and the cost for that unused gas is refunded to the sender.
Keeping Gas Costs Low
There are some things we can do to minimize the amount we spend on gas. First, if a transaction is not urgent, you can reduce the gas price associated with it. This will cause the transaction to sit in waiting until the average gas price goes down to the same level. This is especially worth keeping in mind at times when gas can spike unnaturally high, sometimes above 200 gwei (10x the average). Remember that you decide how much gas you pay.
Beyond that, it’s worth exploring Layer 2’s which have growing ecosystems and applications that can be used while enjoying far cheaper fees. For more on how Layer 2’s work, check out my last write-up on The Layer 2 Landscape.
Conclusion
Gas fees are easier to accept once seen as a defense mechanism instead of an arbitrary fee for transactions. On Ethereum today, gas fees for transactions are actually burned instead of going to validators. This constant reduction in supply benefits all holders of Ethereum by deflating the currency. In essence, gas fees are the insurance that all users pay to keep the network resistant to attack.
Thank You & Additional Reading!
Thanks a lot for reading! Here are some more resources if you'd like to dive deeper.
Please like this post and sign up for more simple write-ups on blockchain concepts.
If you’d like me to cover any topics or have any questions, reach out in the comments!
Stay kind. Stay curious.