Previous Module
Transaction Debugging

⚡ Gas Optimization: Save Users Money

Learn advanced techniques to minimize gas costs

⚡ Why Gas Optimization Matters

Every operation in the EVM costs gas. Deploy an unoptimized NFT contract? Pay $500 in deployment fees. Let users mint with wasteful code? They pay $50+ per transaction when gas spikes to 200 gwei. Gas optimization isn't premature optimization—it's the difference between a usable dApp and an expensive failure. A single SSTORE costs 20,000 gas (vs 3 gas for memory). Loops that read storage can drain wallets. But with proper patterns—packed storage, caching, efficient loops—you can reduce costs by 50-90%. This module teaches battle-tested techniques from production contracts (Uniswap, Aave, OpenSea) that save millions in gas fees.

🎮 Interactive: Gas Cost Calculator

Select an operation and gas price to see real-world costs. Understanding these numbers is the first step to optimization.

SSTORE (new)
Write new value to storage
Gas Units
20,000
Cost (ETH)
0.001000
Cost (USD)
$3.00
💡 Cost Breakdown
At 50 gwei gas price: 20,000 gas × 50 gwei = 1,000,000 gwei (0.001000 ETH ≈ $3.00 at $3000/ETH)

📊 The Gas Cost Hierarchy

Storage (SSTORE)Permanent on-chain data
20,000 gas
External Call (CALL)Cross-contract interaction
700+ gas
Storage Read (SLOAD)Read from storage
2,100 gas
Memory (MSTORE)Temporary data
3 gas

Storage is 6,600x more expensive than memory. Every optimization centers on avoiding unnecessary storage operations.

🎯 Real-World Impact

Uniswap V3 Router: Optimized from 180k to 120k gas per swap via storage packing and efficient math. Saved users $50M+ in gas fees.
OpenSea Seaport: 35% gas reduction vs Wyvern by eliminating redundant checks and using bitmaps. NFT trades cost $10 instead of $15.
Aave V3: Assembly optimizations reduced liquidation costs by 25%. During March 2020 crash, this meant the difference between liquidating positions or not.

💡 What You'll Learn

  • Storage patterns: Packing variables, mappings vs arrays, memory caching techniques
  • Code optimization: Loop efficiency, short-circuiting, unchecked math, function modifiers
  • Advanced techniques: Assembly, calldata vs memory, bitmap tricks, proxy patterns
  • Profiling tools: Hardhat gas reporter, Foundry gas snapshots, eth-gas-reporter