Need Help Optimizing Gas Costs?
Get expert guidance on designing cost-effective smart contracts.
Curated List
Quick Take
Gas cost calculation: Transaction cost (ETH) = Gas used × Gas price. Gas price (post EIP-1559) = Base fee + Priority tip. Example: Simple ETH transfer: 21,000 gas × 16 gwei = 0.000336 ETH (~$0.67). ERC-20 transfer: ~65,000 gas (~$2.08). DeFi swap: ~150,000–200,000 gas (~$4.80–$6.40). NFT mint: ~70,000–100,000 gas (~$2.24–$3.20). L2 comparison: Ethereum L1 ($0.50–$10 transfer), Arbitrum ($0.01–$0.15), Base ($0.001–$0.05), Polygon PoS ($0.001–$0.01), Solana ($0.0003). Foundry's `forge test --gas-report` provides detailed gas usage metrics.
Ethereum Gas Fundamentals
Transaction cost (ETH) = Gas used × Gas price
Gas price (post EIP-1559) = Base fee + Priority tip
Base fee: Set by the network, burned (fluctuates with demand)
Priority tip: Set by user, goes to validator (typically 0.1–2 gwei)
Example (during moderate congestion):
Base fee: 15 gwei
Priority tip: 1 gwei
Total gas price: 16 gwei = 16 × 10^-9 ETH
Simple ETH transfer: 21,000 gas
Cost: 21,000 × 16 × 10^-9 ETH = 0.000336 ETH ≈ $0.67 at $2000/ETH
ERC-20 transfer: ~65,000 gas
Cost: 65,000 × 16 × 10^-9 ETH ≈ $2.08
Complex DeFi swap (Uniswap V3): ~150,000–200,000 gas
Cost: ~$4.80–$6.40
NFT mint (ERC-721): ~70,000–100,000 gas
Cost: ~$2.24–$3.20
Layer 2 Gas Comparison
| Chain | Simple Transfer | ERC-20 Transfer | DeFi Swap |
|---|---|---|---|
| Ethereum L1 | $0.50–$10 | $1–$20 | $3–$60 |
| Arbitrum One | $0.01–$0.15 | $0.02–$0.30 | $0.05–$0.50 |
| Optimism | $0.01–$0.10 | $0.02–$0.25 | $0.04–$0.40 |
| Base | $0.001–$0.05 | $0.002–$0.10 | $0.01–$0.20 |
| Polygon PoS | $0.001–$0.01 | $0.002–$0.02 | $0.005–$0.05 |
| Solana | $0.0003 | $0.0003 | $0.0003 |
Gas Cost Estimation in Foundry
# Estimate gas for a specific function call
forge test --match-test test_Deposit -vvvv 2>&1 | grep "gas"
# Generate gas report for entire test suite
forge test --gas-report
# Example gas report output:
# | Function | min | avg | median | max |
# | deposit | 45782 | 52341 | 50123 | 89234 |
# | withdraw | 38234 | 41234 | 40123 | 75234 |
# Profile specific transactions
forge snapshot --match-test test_LargeDeposit