As more people dive into the world of non-fungible tokens (NFTs), Decentralized Finance (DeFi), and Web3, the need to understand the different parts of an Ethereum transaction becomes increasingly more important. This is especially true to ensure users aren’t overpaying in gas fees as gas prices continue to spike.
Regardless of experience level, navigating gas prices can be difficult. And one of the most challenging aspects of gas is understanding transaction gas limits. Conceptually, transaction gas limit is simple—it’s the maximum amount of gas allocated for a transaction. In practice, however, the way the transaction gas limit works is not intuitive.
As transactions become increasingly more complex, setting the proper gas limit could be the difference between a transaction successfully making it on-chain and a transaction failing before all operations could be fully executed. And inputting the correct amount makes sure only the necessary amount of funds are allocated from your wallet.
What is gas and how is it used?
Before explaining gas limit, it is important to understand what gas is and how it is used in Ethereum transactions. This section will keep things high-level, but you can always find a deep dive into gas on our ETH Gas 101 blog.
All Ethereum accounts and smart contracts are executed through the Ethereum Virtual Machine (EVM). Performing any kind of operations on the EVM, such as conducting a transaction, requires some amount of gas. The more complex an operation, the more units of gas it will cost to execute.
At a high level, the total amount of gas a user ends up spending to execute a transaction is calculated by multiplying the price of gas (set by the user) by the number of units of gas required to execute the transaction.
Total gas spent (transaction fee) = gas price (base fee + priority fee) * units of gas tx requires
For context, a standard peer-to-peer ETH transfer requires no more than 21,000 units of gas. More complex transactions, however, could use hundreds of thousands of units, if not more depending on the smart contract functions called.
What is Transaction Gas Limit?
Setting a gas limit defines the upper bound of gas a transaction is allocated to consume. The gas limit will influence a transaction in one of two ways:
- If the gas limit is equal to or higher than the amount of gas required to execute the transaction, then the transaction will successfully execute (if there are no other issues);
- If the gas limit is less than what is needed to execute the transaction, then the transaction fails (out of gas).
Do I get my gas back for a failed transaction (out of gas)?
A transaction gas limit that is too low will result in a failed transaction and lost gas fees. This is because miners must consume gas to even attempt to execute the transaction, even if the transaction is not successful.
Alternatively, setting a low gas price (max fee) will result in a transaction getting stuck, being dropped from mempools, or pending indefinitely until either the network conditions match the price or the user executes a speed-up/cancel transaction.
What is the purpose of transaction gas limits?
On Ethereum, the number of pending transactions that can fit into the upcoming block depends on how many units of gas each transaction in the block might spend. When miners create new blocks, the total sum of the transaction gas limits in the block must be less than the block’s gas limit.
Since the London hard fork, each block has a target size of 15 million units of gas but the actual size of a block will vary depending on network demand. The maximum size of a block, or the block gas limit, is 30 million gas.
Determining the proper gas limit
It’s important to understand that lowering your gas limit won’t help reduce your gas burden because it doesn’t change the amount of resources needed to actually process a given transaction. Most wallets will automatically set a gas limit based on the address or contract you are choosing to transact with.
Public blockchain explorers can be used to better understand the transaction costs associated with a smart contract by analyzing others’ interactions with the contract. Users can use this information to input different gas limits in Blocknative’s Transaction Preview API and test their own transaction before committing it on-chain to determine if it will succeed or fail under the current market conditions. Create an account and explore our detailed API documents to get started today.
Master the Mempool today.
Blocknative's proven & powerful enterprise-grade infrastructure makes it easy for builders and traders to work with mempool data.Start for free