Part 2: Decoding Ethereum dApp Development: Nonce, Gas, and Fees Explained
Simplifying key concepts in EVM blockchain development: Nonce, Gas, Gas Limit, Gas Price, maxFeePerGas, maxPriorityFeePerGas.
After my previous post, you should be familiar with the concepts of the nonce in your dApp. Today, I will be trying to solve the next important puzzle in transaction preparation, and it is fee management for EVM-based blockchains, taking Ethereum as a primary example.
To be able to send a transaction to the blockchain, you must specify how much ETH you are willing to pay for this transaction - transaction fee. To be able to do it, you must understand how to calculate the fee - you don't want to overpay, but you don't want to underpay.
Let me describe to you the important parts of the transaction fee.
Gas, or gas unit, is the smallest unit for measuring the computational effort during the execution of the transaction. Every time you perform a transaction, there is a load on the EVM to process it, perform all the actions in the transaction, update the state, etc. The minimum number of gas units required for the simplest transaction is 21,000. This is the plain transfer of the ETH between two blockchain addresses. Whenever you want to interact with the smart contract, send ERC-20 tokens like USDC, or execute a DEX trade, you must know beforehand how much gas your transaction will consume.
The gas limit provided for the transaction is the maximum amount of gas you want this transaction to consume. It's an upper limit, not the actual used gas. Sometimes, you can't estimate gas precisely, or you want to have some safety net above your calculated usage.
On the other side, if you provide a gas limit that is insufficient to perform the transaction, the operation will fail and consume all of your gas. This could be very costly, especially during the contract deployments, when you want to deploy, e.g., NFT collection for 2M gas, but provide only 1,9M gas limit. Your contract will not be deployed, but you will burn a whole 1,9M of gas, which is your real money.
Gas price is the amount of the ETH you will pay for 1 gas unit. The total final fee you might pay for the transaction is calculated as gas limit * gas price = fee. Gas price is entered into the transaction in the smallest unit of ETH, which is wei.
You need to provide the gas price big enough that your transaction is not stuck in the mempool but small enough so you won't pay more than you should. One of the best estimation tools can be found here.
Gas used can't be estimated before the transaction is actually included in the block and executed. This value is the final gas the EVM consumes, and it tells you the real costs. So the formula for the final fee paid by the transaction will be updated to gas used * gas price = paid fee.
It's not simple to provide all the values for the transaction correctly. The wrong gas limit will result in a lost fee for a failed transaction, a small gas price will lead to a stuck transaction, and, on the opposite, a high gas price will lead to your money being wasted. It's much better to leverage some SDK, which could simply and effectively take care of the correct estimation of the gas needed for the transaction and the price paid for the gas unit. In the next post, I will describe the EIP-1559 enhancement for the Ethereum network, where gas price is calculated differently, and you can save some ETH on the transaction fees. Stay tuned.