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 and that is by charging senders of transactions with a small fee called gas fee with which is then used to reward miners who validate transactions in the network.
This fee is in the form of Ether (ETH) which is also used in the Ethereum network to facilitate value transfers, mining payouts and smart contract executions.
How gas fee works is similar to how gas/fuel works for our vehicle. In order to get from Point A to Point B, the vehicle needs to be filled up with a sufficient amount of fuel first.
Similarly, for a transaction in the Ethereum network to be successful, the sender must provide a reasonable amount of gas fee first.
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 ERC tokens that you are sending. Also, the gas fee is deducted automatically concurrently with your transaction in a single event so you do not have to worry about "forgetting" to pay the gas fee.
For example, let say, you have 10 ETH in your address and you are looking to send 1 ETH to a friend of yours. The gas fee for the transaction is (for the sake of setting an example) 0.5 ETH. From such, your friend will get that 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. And once this transaction is successful, your address should have 8.5 ETH left.
How To Set/Calculate Gas Fee?
Depending on the wallet that you use, you may or may not be able to set the gas fee manually. However, in setting up the gas fee, there are two variables to it to mind.
Gas Price: As to how petroleum fuel is priced (price per litre, e.g.: $1/litre), gas price is the price of a unit of gas that we are willing to pay for the transaction. (Gas price is calculated in GWEI, 1 GWEI is 0.000000001 ETH)
Gas Limit: Moving along with the petroleum fuel comparison, gas limit is how much gas that we are willing to allocate for the transaction.
In conclusion, Gas Fee = Gas Price x Gas Limit
As an example, let say, the price of petroleum fuel is $1 a litre and we are estimating that 10 litres of it would be enough for our trip, we will then allocate 10 litres of $1/litre fuel for the trip, which will amount to a total sum 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, in the end we will have to have at least 0.000021 ETH (gas price x gas limit / 0.000000001 x 21000) inside of our wallet to pay for the transaction's gas fee.
Do take note that for a normal transaction, 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) and in particular EIP-1559, which changes the gas fee mechanics for Ethereum.
Instead of a first-price auction mechanic, gas fee will be calculated using a hybrid system involving base fees and tips:
Base Fee: Determined by the network and is subsequently burned.
Max Priority Fee: Determined by the user and is optional. Also known as Miner Tip as it is paid directly to miners.
Max Fee Per Gas: Maximum amount users are willing to pay to get their transactions 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 known as Type 0, while EIP-1559 transactions are known as Type 2. Users may view the type of transaction on our Transaction Details page.
How Much Is Enough?
As gas fee is ultimately used as an incentive for the miners 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 times of normal congestion.
So, to tackle the subjectivity of gas fee, we have a feature called Gas Tracker which you can refer to here that analyses the traffic of the blockchain and suggests the most recommended gas price for a transaction at that time.
Out of Gas
When a transaction has failed due to “Out of Gas”, this means the gas limit set during the transaction is below the required gas needed to perform the transaction. The value of the transaction does not leave your address but the 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/contracts which have been successfully processed to find out the gas limit required.
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 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 ERC tokens, please take note that the gas fee is always in the form of ETH and so it is always wise to keep a little extra ETH inside your address for gas purposes.