I often see people confused about how Ethereum fees work or why transactions fail. So let’s try to explain everything you need to know about Ethereum gas fees!
The concept is also true for sidechains and L2s like Polygon and Arbitrum (Nova)! Especially when I talk about failed transactions and how to fix it, this is more intended for cheap L2s and not the main-chain where each mistake costs you 10$!
Note only the first three paragraphs are required for the basics, the later part explains why the most common problems happen and how to resolve them.
What is gas?
Simply speaking: Gas is the amount of work a certain transaction causes for the network. This will only depend on the transaction, and be fully independent of the current load of the network. Some examples can be seen in this table:
Type Gas ETH transfer 21,000 ERC20 token transfer ~50,000 Uniswap V2 Swap ~105,000 Uniswap V2 Add Liquidity ~130,000 Uniswap V3 Swap ~130,000 Uniswap V3 Add Liquidity ~450,000
As you see, the only thing that can be exactly estimated is an ETH transfer, this is the minimum fee. Different token contracts can require different fees and for DEXes the gas fee can even vary a lot more.
You have basically no control over the gas usage, your wallet will automatically set a gas limit. We will look into this later again.
The gas fee
The gas fee is the amount of Gwei you pay per unit of gas your transaction consumes. This will depend on the current load of the network and is basically “auctioned”, meaning the validators will include the transactions with the highest gas fees. You can see an estimate for the current gas fees on gas trackers like the one from Etherscan.
One Gwei is 10-9 ETH = 0.000000001 ETH.
Simplified, you will pay one gas fee per unit of gas your transaction consumes:
Fee = Gas Units * Gas Fee.
The London upgrade changed this forumula a little bit by splitting the gas fee into base fee and priority fee:
Fee = Gas Units * (Base Fee + Priority Fee)
Your “max base fee” must be larger than the Base Fee set by the network, and your Priority fee is the tip you give the validator. The larger the tip, the more likely you will be included, especially in congested times.
Note if the Max Base Fee is higher than the actual Base Fee, the difference will be refunded to you. The priority fee will always be paid.
Example
Let’s say you make a Swap for 105,000 gas and the fee is 50 Gwei. The fee for the transaction is 105,000 * 50 Gwei = 5.25m Gwei = 0.00525 ETH = ~10$
Pending transaction
If your transaction is pending, you set either the Max Gas Fee or the Priority Fee too low. You can compare your transaction, which should already be visible on the block explorer, with a gas estimator to see where to problem is.
Especially if your Base Fee is too low, waiting will often not resolve the issue, you need to increase the fee. In Metamask you can do this pretty easy directly in the “Activity” tab by clicking on “Speed Up”.
This will actually broadcast a new transaction with the same Nonce, effectively overwriting the old transaction. That’s why even “Canceling” a transaction will still require a small fee, because this just replaces the transaction with a zero-transfer to self or similar.
If you want to make another transaction, you have to make sure you have no pending transaction left!
Failed transactions and why they still charge the fee
One important thing to understand: An ETH transaction fails because of the user and not because of the network. This can have different reasons, for example a critical error during a smart contract interaction, like a requirement missing or a bug.
Even if your transaction fails, you pay the fee. Some find this strange, but it actually makes perfect sense. The validator did the work, but the transaction failed because of an error (of the user!). The fee is the only thing that stops an attacker to spam the network with endless impossible transactions.
The reason this doesn’t exist in Bitcoin: Bitcoin is not Turing Complete – if a transaction is valid or not can be determined by any node without needing a lot of computation power. In fact, a transaction can’t fail on Bitcoin.
Gas limit
Any ETH transaction will include a gas limit – there is no way to know exactly how much gas a transaction will consume before it happens, so you set the maximum amount of gas a transaction is allowed to use up. No matter what happens, the tx will not consume more gas. Also a successful transaction will never consume more gas than it needs, no matter what the gas limit was set to.
This one is super important, because from my experience it’s the #1 reason a user will encounter a failed transaction. Usually your wallet or the website you are connected to will estimate the gas limit correctly.
Sometimes however, especially after a contract got updated, a website will tell your wallet a wrong gas limit and the transaction will fail. Sadly, not all transactions will clearly say so on the block explorer. See this two trancactions which both failed because they ran out of gas:
Transaction 1: Failed because of gas limit, gives clear error message
Transaction 2: Failed because of gas limit, gives generic error message
If you are not sure, you can check the actual gas limit in the failed transaction on a block explorer: A failed transaction with nearly 100% gas usage will often indicate your transaction actually ran out of gas:
To fix this, you have to broadcast the transaction again with a higher gas limit. If this happens on Nova, I usually just double the fee and this usually fixes the issue.
If you set the gas limit to 2x or 10x of the first value, your next transaction will also be 2x or 10x more in fees if it fails again. Strictly speaking, there can be different reasons for a transaction reaching the gas limit – like a tx caught in an infinite loop. So try at your own risk!
Edit fees in Metamask
You can manually edit the gas fees (which consists of the base fee and priority fee) and gas limit in Metamask. The setting is a little hidden, but on each transaction you can reach it by first clickig at “Market” on the fee tab and then on “Advanced”:
Sources
Gas Fee estimator showing gas costs
TL;DR
Gas fees on Ethereum are actually pretty simple:
Your transaction consumes a certain amount of gas – more complex transactions (like a swap) require more gas than a simple transfer. The load of the network will determine the gas price.
In the end you pay the gas fee for each unit of gas your transaction consumes.
If your gas price is too low, your transaction will remain pending.
If your gas limit is too low, your transaction will fail.
submitted by /u/Maxx3141
[link] [comments]