Ethereum Gas Fee Explained

Yu-Kai Tseng
CryptoLab.Network
Published in
4 min readMar 22, 2022

Recently the Car Man’s disastrous whitelist sale failed, many whitelist holders received the “out of gas” error when minting their NFTs. This guide will explain to you what is the gas fee and why did the minting failed. I will try to narrate it with a not-too-technical way, for technical guide, you can read Gas and Fees on ethereum.org.

What is the gas fee?

On Internet, the costs is paid by business owners. You rarely have to pay for your actions. For example, it does not cost you to search in Google or see stock price changes. However, the operations you do on the Internet actually costs. The servers fulfill your search need power, and the power companies charge the fee of use to Google instead of you.

In Crypto world, the costs is paid by end users. Why? There is no free lunch in Crypto, anything you do needs to be verified by someone. And they need to be motivated. The gas fee, is one of the way to motivate verifiers.

On Ethereum, any operations with “write” actions cost. For example, it costs when you transfer tokens, it costs when you mint an NFT because you “write” data into the chain because they need to be verified to be legal.

How to calculate the gas fee?

The Ethereum gas fee is calculated by what and how many operations do you use, multiply by the gas price (and additional tipping you set) of that block. For example, if you do an action on chain, and the total gas used is 100000. Assume the gas price of the block is 50 GWEI, and you don’t add any tips, it would cost you 100000 * 50 = 5000000 GWEI, or 0.005 ETH to do the action.

But how do you calculate the gas fee in advance of a transaction, that’s barely possible for a human. Besides, how do you make sure what you want to operate does not cost you incredibly high gas which would consume all of your money?

About Gas Limit

To resolve the problems, Ethereum instead asks you to enter a “gas limit”, which defines the maximum gas you want to use for a transaction. It protects you from consuming too much gas without knowing it in advance. Thus for a transaction, you have to enter a gas limit. During the transaction, the gas is consumed by each operation. For example, assume you set the gas limit for a minting transaction to 200000. Store your data in the contract may costs you 20000 and you have 180000 gas left.

The total transaction fee = gas used (limit) * (gas price+ tip)

The gas limit only defines the max gas you want to use, not the gas to be consumed. If you set gas limit to 200000 and spend 165000 when minting an NFT, the gas fee is 165000 * gas price.

The next question is, what should you fill the gas limit field? You can input any gas limit below a block gas limit, it is dynamic at about 30 million. Wallets such as MetaMask can estimate the gas for you. The wallets parse the transaction bytecode in advance to see how much gas would a transaction consumes.

How did the “out of gas” happened and how to avoid it?

If the gas consumed during a transaction is over the limit, an “out of gas” error is generated and the transaction is reverted. But why? You just said the wallets can estimate it.

Yeah, but in certain conditions, the wallets cannot precisely estimate it. For example, if a contract method contains for-loops, the wallets cannot estimate it because they do not know how many times the loop could be ran.

The “Car Man” NFT is just a good example. The whitelist data are stored in its contract. And they iterate the for-loop to check if a sender is an owner. As the whitelist could contain hundreds or even thousands of entries, it costs a lot because each for-loop costs gas. If unfortunately, your records is at near the end of the whitelistedAddresses, all the gas could be used before the contract found your record and you are “out of gas”.

There are two problems in their mechanism. One, they should not store all the whitelist data to their contract, it very expensive. Second, they should not use for-loop to check the existence. They should just save the whitelist in a map and do something like this.

require(_whitelist[msg.sender], "Not in whitelist");

No for-loop is needed at all.

For a better solution, they should use a backend service, which generates asymmetric signatures, or use the MerkleProof to avoid writing a lot of data on chain.

The next time you want to mint an NFT. Read the contract or at least ask someone about their contract first to avoid minting a garbage.

--

--