Gas is a security model. It prevents infinite loops and denial-of-service attacks by metering computational work. Every EVM opcode has a fixed gas cost, making execution time predictable and finite.
Gas Accounting Rules Every CTO Should Know
A first-principles breakdown of Ethereum's gas accounting system. This isn't about saving pennies; it's about understanding the core security and economic model that governs every transaction, block, and protocol on the network.
Introduction: Gas is a Security Model, Not a Tax
Gas is a resource metering and anti-DoS mechanism, not a simple transaction fee.
Gas is not a tax. This framing causes misaligned incentives. Users see it as a cost, but validators see it as a resource. This disconnect drives poor UX patterns like gas sponsorship.
Account Abstraction (ERC-4337) exploits this. It separates the fee payer from the transaction signer, enabling sponsored transactions and gasless onboarding. This breaks the native security model, outsourcing trust.
Evidence: Ethereum processes ~15M gas per block. This hard cap is the network's primary security parameter, not a revenue target. Arbitrum and Optimism use L1 gas as their final security anchor.
Executive Summary: The Three Non-Negotiable Rules
Gas is not just a fee; it's a system-wide accounting mechanism for state transitions. Ignoring its rules leads to failed transactions, exploited contracts, and unsustainable protocol economics.
The 63/64 Rule: Gas Forwarding is a Trap
EIP-2929 made first-time storage access more expensive, but the gasleft() check for forwarding is based on the post-call gas. This creates a critical mismatch.
- Key Risk: A sub-call can consume more gas than its stipend, causing the parent call to revert.
- Key Mitigation: Use hardcoded gas limits for internal calls, never forward
gasleft().
Gas Stipend != Execution Cost
A CALL with value forwards a 2300 gas stipend for the recipient's logic. This is a hardcoded constant, not a measurement of actual opcode costs.
- Key Constraint: 2300 gas only covers ~7
SSTOREoperations for a zero-to-non-zero write. - Key Design: Any contract receiving ETH must have a
receive()function with minimal, fixed-cost logic (e.g., a simple log).
Precompiles Break the Pricing Model
Precompiled contracts (e.g., ecrecover, sha256) have fixed gas costs defined in the protocol. These costs are not dynamically adjusted for real-world hardware.
- Key Anomaly: Precompile costs can become economically irrational (e.g.,
sha256is often cheaper to compute off-chain). - Key Audit Point: Heavy use of
modexp(modular exponentiation) in L2 sequencers can be a hidden cost center and DOS vector.
The Accounting Ledger: From User Intent to Finalized Block
A breakdown of the non-negotiable accounting principles that govern transaction execution and settlement on EVM chains.
Gas is prepaid execution insurance. Users must attach sufficient ETH or the chain's native token to cover the maximum computational cost of their transaction before execution begins, with unused gas refunded. This prevents infinite loops and denial-of-service attacks by making computation expensive.
The 21k gas base fee is the immutable cost of writing any transaction to a block, covering signature verification and storage overhead. Complex operations like storage writes or cryptographic calls consume additional opcode gas, making contract interactions orders of magnitude more expensive than simple transfers.
EIP-1559 creates a dynamic fee market. The protocol burns a base fee that adjusts per block based on network demand, while users bid a priority fee (tip) to miners/validators for faster inclusion. This mechanism makes fee estimation more predictable than the first-price auction model.
Layer-2s abstract gas with data availability. Optimistic rollups like Arbitrum batch transactions and post proofs to Ethereum, charging users a fee composed of L2 execution cost and the cost to post data to L1. This is why an Arbitrum swap can cost $0.10 while a similar Ethereum swap costs $5.
Account abstraction (ERC-4337) decouples payment from execution. With smart accounts, a third-party paymaster can sponsor gas fees, allowing users to pay in ERC-20 tokens like USDC. This enables seamless onboarding but introduces new trust assumptions and accounting complexity for the sponsoring entity.
Gas Cost Matrix: The Price of EVM Opcodes
A comparison of gas costs for fundamental EVM operations, from cheap arithmetic to expensive storage writes. Essential for optimizing contract logic and predicting transaction fees.
| Operation / Opcode | Gas Cost (Base) | Dynamic Factors | Optimization Insight |
|---|---|---|---|
Arithmetic (ADD, SUB) | 3 gas | Fixed | Cheapest computation; batch operations. |
Bitwise Logic (AND, XOR) | 3 gas | Fixed | Inexpensive data masking and checks. |
Memory Expansion (per word) | 3 gas | Scales with new memory usage | Pre-allocate memory to avoid linear growth cost. |
Keccak256 Hash (SHA-3) | 30 gas + 6 gas/word | Scales with input data size | Cache hashes off-chain when possible; major cost center. |
SLOAD (Cold Storage Read) | 2100 gas | Warm access: 100 gas | The first read is expensive; design for data locality. |
SSTORE (Set to Non-Zero) | 20000 gas (cold) / 2900 gas (dirty) | Refunds on clearing; warm slot: 100 gas | Most expensive opcode; use mappings over arrays, pack variables. |
External Call (CALL) | 2600 gas (base) + value transfer cost | Adds stipend for gas forwarding | Minimize cross-contract calls; batch interactions. |
Contract Creation (CREATE) | 32000 gas | Plus init code execution cost | Use CREATE2 for predictable addresses; factory patterns add overhead. |
The Surge & Verge: Accounting for a Parallel, Stateless Future
Ethereum's scaling roadmap redefines gas accounting, forcing CTOs to model for parallel execution and stateless verification.
Gas accounting is now multidimensional. The EIP-4844 blob market and EIP-7623 execution gas reform separate data availability from execution costs. This creates a two-dimensional fee market where L2s like Arbitrum and Optimism must optimize for both blob space and L1 gas.
Parallel execution breaks linear cost models. Rollups like Monad and Sei implement parallel EVMs, where transaction throughput is non-linear with gas. Your cost model must account for state access patterns and contention, not just opcode sums.
Stateless verification inverts validation logic. The Verge's stateless clients, using Verkle proofs, shift the cost of state proof verification from the network to the prover. This makes proof aggregation services like Risc Zero and Succinct critical infrastructure for cost efficiency.
Evidence: Arbitrum Stylus benchmarks show parallel execution reduces certain batch processing costs by 10x, but introduces new bottlenecks in shared state access that increase variance.
CTO FAQ: Practical Gas Accounting
Common questions about the fundamental rules and risks of gas accounting for blockchain architects.
Gas accounting tracks and attributes costs, while gas sponsorship pays them on behalf of users. Accounting is the ledger (e.g., a protocol's internal tally), and sponsorship is the payment method (e.g., using a Pimlico paymaster or Biconomy relayer). You need robust accounting to enable secure and verifiable sponsorship.
Takeaways: Architecting for the New Gas Economy
Gas is no longer just a cost; it's a core design primitive. Here are the non-negotiable rules for modern protocol architecture.
Separate Gas Abstraction from Sponsorship
The Problem: Users hate managing gas tokens. The Solution: Decouple the 'who pays' from 'what is paid with'. Use Paymasters (like EIP-4337 Account Abstraction) for sponsorship and ERC-20s for payment.
- Key Benefit: Enable true gasless onboarding and enterprise-grade UX.
- Key Benefit: Isolate protocol logic from volatile native token economics.
Treat Gas as a Bidding War, Not a Fixed Fee
The Problem: Static gas estimates fail during volatility. The Solution: Architect for Priority Gas Auctions (PGAs) and dynamic fee markets like EIP-1559. Use oracles (e.g., Chainlink Gas Station) for real-time estimates.
- Key Benefit: Guarantee transaction inclusion during high-demand periods.
- Key Benefit: Optimize cost by targeting base fee vs. priority tip trade-offs.
Enforce Gas Accounting at the Contract Level
The Problem: Unbounded loops and storage writes cause out-of-gas reverts. The Solution: Implement internal gas metering. Use patterns like gas refunds for storage clearance and checkpoints for batch operations.
- Key Benefit: Predictable execution costs prevent protocol insolvency from failed txns.
- Key Benefit: Enables complex, multi-step intents (see: UniswapX, CowSwap) without user-side gas guessing.
Abstract Cross-Chain Gas with Intent Standards
The Problem: Users must hold gas tokens on every chain. The Solution: Adopt intent-based architectures and universal gas tokens. Leverage systems like LayerZero's Omnichain Fungible Tokens (OFT) and Circle's CCTP for native gas payment.
- Key Benefit: Single-asset liquidity management across Ethereum, Arbitrum, Base.
- Key Benefit: Removes the bridging step for gas, a major UX failure point.
Shift Cost to the L2, Not the User
The Problem: L1 gas costs make micro-transactions impossible. The Solution: Design for L2s (e.g., Arbitrum, Optimism, zkSync) as the primary execution layer. Use L1 only for final settlement and high-value assertions.
- Key Benefit: Reduce transaction costs by 100-1000x for end-users.
- Key Benefit: Enable new economic models (sub-cent fees, social apps).
Audit for State Bloat & Gas Inefficiency
The Problem: Unchecked storage growth permanently increases baseline gas costs for all users. The Solution: Treat state size as a critical KPI. Implement state rent proposals, periodic state roots, or forced migration paths.
- Key Benefit: Protect long-term chain viability and validator decentralization.
- Key Benefit: Avoid the 'Ethereum State Size' problem in your own protocol's design.
Get In Touch
today.
Our experts will offer a free quote and a 30min call to discuss your project.