Virtual fuel for your Ethereum transactions
Welcome back! This is Part 2 of a miniseries on Ethereum transactions and gas fees. Gas fees can be confusing for beginners, but they're really important to understand. Having a strong grasp on transaction fees can wind up saving you lots of money as you interact with Ethereum.
If you aren't familiar with Ethereum transactions, this post probably won't make a lot of sense to you. Here's Part 1.
What are Gas Fees?
As I discussed last week, Ethereum is based on transactions. Transactions can be anything from transferring ETH between addresses to making trades on Uniswap. All of this transaction data is publicly stored on the Ethereum blockchain. Importantly, it costs real money (ETH) to write transactions to Ethereum. This ETH is called a gas fee.
Why must we pay to store data on chain? Transactions require computational work, so we must include some payment to the miners as a reward for performing that work. Requiring a transaction fee also ensures that spammers can't submit unlimited transactions and take over the network.
Gas fees depend on a few things: transaction complexity (in gas units), gas prices, and the gas limit.
It's helpful to think of gas on Ethereum just like you would think about gas used for cars in the real-world. Keep this analogy in mind as we walk through each part of gas fees.
Gas units are the units of computation. You can think of this like you think of "gallons" in our car analogy. You can use real-world gas to go anywhere, just like you can use Ethereum gas to make any transaction you want.
In our car analogy, transaction complexity is analogous to the number of miles driven. The more miles you drive, the more gallons of gas you'll need.
A more complex smart contract will require more computational power to execute. Thus, you'll need more gas units to execute the transaction.
The gas price is how much you are willing to pay for one gas unit.
It's analogous to the price of a gallon of gas in the real-world. The more expensive a gallon of gas, the more expensive it will be to make a road trip to the Grand Canyon. On Ethereum, the more expensive the gas price, the more expensive it will be to make a trade on Sushiswap.
Gas prices are quoted in a denomination called gwei, a sub-denomination of ETH. 1 gwei = 1x10⁻⁹ ETH (0.000000001 ETH = 1 gwei). It's much easier to read gwei than ETH, so we use gwei by default when referring to gas prices.
Where to Find Gas Prices
Just like gas prices in the real-world, gas prices fluctuate on Ethereum, too. As demand for writing transactions to the chain increases, so does the price. Supply is relatively fixed (only a certain amount of computational work can be included in 1 block). Thus, the more people who want to make transactions, the higher the gas price.
Since Ethereum is sub-scale today, users feel the pain during high-demand shocks. When Uniswap launched its governance token, for example, many people tried buying $UNI at the same time. This led to the network being inundated with transaction requests. Gas prices soared and making trades on Uniswap's platform cost over $100.
You can save a lot of money by waiting to transact until gas prices are very low.
My favorite place to check gas prices is Etherscan. They provide a handy tracker that lets you see the recommended gas fees for different types of transactions.
What's a Good Gas Price?
Gas prices can swing wildly. A great gas price is in the single digits, and anything over 70 can start to become really expensive. Etherscan provides a nice 7 day historical gas price tracker. I'd say the prices below are fine - not great and not terrible (on average, between 20-40 gwei).
As time goes on, Ethereum will be able to scale and gas fees should drop dramatically. With ETH 2.0 planned upgrades and side chains, the ecosystem should be able to scale far beyond where it is today.
The blockchain is a chain of blocks. Each block contains many transactions (around 70 on Ethereum). Blocks of transactions are created by miners; the miner chooses which transactions to include in their new blocks. To incentivize the miners to include our transactions in their blocks, we must pay them enough ETH.
Miners are economically savvy. When given the choice between a low gas fee and a high gas fee, they'll choose to include the high gas fee transaction on their block so they can make the most money.
The higher the gas fee you pay, the more miners will be incentivized to run your transactions and include them in their blocks. That means that the more gas you pay, the faster your transaction will process. The way you control how fast your transaction processes is through the gas price.
Paying above-average gas prices will get your transaction confirmed very quickly. This would be like purchasing an expensive type of gas for your car that allowed it to drive much faster than it could on standard gas.
If you have a transaction that's not time-sensitive, you can always pay below-average gas prices for your transaction. This would be like purchasing a lower-quality fuel that causes your car to get to its destination more slowly. You'll still get to your destination, it'll just take longer.
This is where the analogy breaks down. On Ethereum, if you use gas that's too cheap, your transaction could fail! With a gas price that's too low, miners won't pick up the transaction and include it in their blocks. If that happens, the gas you spent on the transaction will be lost forever and you'll have to submit a new transaction.
The gas limit is the maximum amount of ETH that you're willing to pay for a transaction.
Continuing with our car analogy, the gas limit is analogous to how much gas you put in your tank before a road trip. For the purposes of this example, let's assume that you can only fill up your tank once before the trip. If you put in 10 gallons more than you need, you'll be able to use those gallons to drive somewhere else in the future. But if you put in 5 gallons fewer than needed to get to your destination, you'll be stranded and the gas will be lost.
On Ethereum, the gas limit must be higher than the gas units required to run a transaction. If you submit a transaction with a gas limit of 100,000 units and the real cost is 21,000 gas units, you'll receive the 79,000 units of gas back after the transaction is complete.
If you instead submit a transaction with only 20,000 units as your gas limit, the transaction will fail and you won't receive any of your gas back. This is because the miner has already performed 20,000 units of work on the transaction and must be compensated for that work. Just like you can't get real world gas back after you burn it in your car, once gas is spent on Ethereum, it's gone, whether it was enough to get you to your destination or not!
The Most Expensive Transaction in Ethereum History
It's important to understand gas fees before making transactions. Not understanding gas fees properly can cost you a lot of money.
A mistake with transaction fields led to the most expensive transaction in Ethereum history, costing the user $2.6 million.
You'll probably never spend $2.6 million on a transaction fee, but it still pays to know what you’re doing when submitting transactions.