What is a Gas Limit?
A gas limit is the maximum amount of gas units you’re willing to spend on a single transaction. It acts as a safety cap — if a transaction requires more gas than the limit, it runs out of gas and reverts, but you still pay for the gas consumed up to that point.
Think of it like a budget at a gas station: you tell the pump “I’m willing to spend up to $50.” If the tank fills at $40, you pay $40. If it needs $60, the pump stops at $50 and the tank isn’t full.
How Gas Limit Works
Each EVM operation consumes a specific amount of gas:
| Operation | Gas Cost |
|---|---|
| Simple ETH transfer | 21,000 |
| ERC-20 token transfer | ~50,000 |
| DEX swap | ~150,000 |
| Complex smart contract | 500,000+ |
| Block gas limit | 30,000,000 (Ethereum) |
When you set a gas limit of 200,000 for a DEX swap:
- If the swap consumes 150,000 gas → you pay for 150,000, the remaining 50,000 is refunded
- If the swap requires 250,000 gas → transaction reverts at 200,000, you pay for 200,000 (gas spent up to the revert)
Why Gas Limits Exist
- Prevent infinite loops — a buggy or malicious smart contract with an infinite loop would run forever without a gas limit
- Protect block space — the block gas limit (30M on Ethereum) caps how much computation fits in one block
- User protection — prevents accidental spending of unlimited gas on a broken contract
Setting the Right Gas Limit
- Too low: Transaction reverts (out of gas) — you lose the gas fee and the transaction fails
- Too high: No harm — unused gas is refunded. But the gas is “locked” until the transaction confirms
- Just right: Slightly above the estimated gas to account for variance
Most wallets auto-estimate the gas limit. Advanced users can adjust it manually.
Frequently Asked Questions
Q: What happens when a transaction runs out of gas? A: The transaction reverts (fails), but all gas consumed up to the revert point is charged. State changes are rolled back, but you don’t get a refund on the gas. This is why it’s important to set a sufficient gas limit.
Q: What’s the block gas limit? A: On Ethereum, the block gas limit is 30 million gas units. This means all transactions in a block combined cannot exceed 30 million gas. This cap ensures blocks don’t get too large and that validators can process them in time.
Q: Why does MetaMask set my gas limit so high sometimes? A: Wallets estimate gas conservatively to avoid out-of-gas reverts. The estimate includes a safety buffer for variance in contract execution. The unused gas is refunded, so setting a slightly higher limit doesn’t cost extra.