What is Gas Fee? ⛽
As the Ethereum network is a decentralised blockchain with no singular entity or authority overseeing its operation, a mechanism is put in place to avoid the network from being congested or spammed with transactions. This mechanism charges senders of transactions with a small fee called gas fee which is then used to reward block producers who validate transactions on the network.
This fee is in the form of ETH which is also used on the Ethereum network to facilitate value transfers, block producing payouts and smart contract executions.
How gas fee works is similar to how gas/fuel works for our vehicles. In order to get from Point A to Point B, the vehicle needs to be filled up with a sufficient amount of gas.
Similarly, for a transaction on the Ethereum network to be successful, the sender must provide a sufficient amount of gas to pay for gas fees.
How To Provide Gas Fee For A Transaction?
The gas fee is deducted from the remaining ETH balance of your address and not from the amount of ETH or tokens that you are sending. It is also deducted automatically/concurrently with your transaction in a single event so you do not have to worry about "forgetting" to pay gas fees.
As an example, say you have a balance of 10 ETH in your address and are looking to send 1 ETH to a friend of yours. The gas fee for this transaction is (as an example) 0.5 ETH. From such, your frieqnd will receive 1 ETH while the gas fee of 0.5 ETH is deducted from your balance. Both the 1 ETH and 0.5 ETH will be deducted in one single transaction hash simultaneously. Once this transaction is successful, your address should have a balance of 8.5 ETH left.
How To Set/Calculate Gas Fee?
Depending on the wallet used, you may or may not be able to set the gas fee manually. With that said, in setting the gas fee there are two variables to keep in mind.
Gas Price: Similar to how petroleum gas/fuel is priced (price per litre, e.g.: $1/litre), gas price is the price of a unit of gas that a user is willing to pay for a transaction (Gas price is calculated in GWEI, 1 GWEI is 0.000000001 ETH).
Gas Limit: Continuing the petroleum analogy, gas limit is how much gas a user is willing to allocate for the transaction.
In conclusion, Gas Fee = Gas Price x Gas Limit
As an example, say the price of petroleum is $1 a litre and we are estimating that 10 litres of it would be enough for a road trip. We will need to allocate 10 litres of $1/litre fuel for the trip, which amounts to a total of $10 that we need to have prepared for fuel.
Similarly, if we set the gas price at 1 GWEI and the gas limit at 21,000, we will need to have at least 0.000021 ETH (gas price x gas limit / 0.000000001 x 21,000) inside of our wallet to pay for the transaction's gas fee.
Do take note that for a normal transaction (sending ETH), a gas limit of 21,000 is often enough. But for a transaction that involves interacting with a smart contract, 21,000 is not enough. If you are interacting with smart contracts, please set a higher gas limit.
Post EIP-1559 Gas Fee Mechanic
On 5th August 2021, Ethereum underwent a major network upgrade dubbed the London Hard Fork. Contained within the hard fork are five Ethereum Improvement Proposals (EIP). In particular, EIP-1559 changes the gas fee mechanics for Ethereum.
Instead of a first-price auction mechanic, gas fees are calculated using a hybrid system involving base fees and tips:
Base Fee: Determined by the network and subsequently burned.
Max Priority Fee: Determined by the user and is optional. Priority Fee is also known as Miner Tip as it is paid directly to block producers.
Max Fee Per Gas: Maximum amount the user is willing to pay to get their transaction included in a block.
The goal of EIP-1559 is to provide a better fee estimation and reduce variance in times of high demand. Legacy transactions remain supported and are known as Type 0, while EIP-1559 transactions are known as Type 2. Users may view the type of a transaction in the Transaction Details page.
How Much Is Enough?
As gas fee is ultimately used as an incentive for block producers in the Ethereum network, the answer to this question can never be one objective figure. This is because gas fee is subject to the congestion of the blockchain at any given time. In times of higher congestion, you would usually need to set a higher gas fee than in times of normal congestion.
To tackle the subjectivity of gas fee, Etherscan has a feature called Gas Tracker which you can refer to here. It analyses the traffic on the blockchain and recommends suitable gas prices for transactions to be made at a particular time.
Out of Gas
When a transaction has failed due to an “Out of Gas” error, this means the gas limit set for a transaction is below the required gas needed to execute it. The transaction value does not leave your address but gas fee is deducted because of the computational cost incurred. You will need to reinitiate the transaction with an appropriate gas limit. Wallet services will usually suggest a gas limit for your transactions. Alternatively, you can also look at similar/past transactions made using related contracts which have been successfully processed to estimate a suitable gas limit to set.
Possible reasons for an “Out of Gas” error warning:
- Insufficient amount of gas limit provided
- If you are interacting with a Contract, the minimum amount of gas limit required is > 21,000 (try increasing your gas limit)
- Contract execution exceeds the minimum gas limit required
- Contract execution error occurred, but the Virtual Machine (VM) execution continues until all of the gas limit is exhausted.
Always remember to have a little extra ETH than you need inside your address. Regardless of whether you are sending ETH or tokens, please take note that the gas fee is always in the form of ETH and it is always wise to keep a little extra ETH inside your address for gas purposes.