Tutorial·10 मिनट का पठन·Solingo द्वारा

Ethereum Gas को समझें — Complete Guide 2026

Gas mechanism की deep dive: कैसे काम करता है, optimization techniques, EIP-1559, और 2026 में gas costs reduce कैसे करें।

# Ethereum Gas को समझें — Complete Guide 2026

Gas Ethereum ecosystem का fundamental concept है। यह समझना critical है efficient contracts लिखने और users के लिए costs minimize करने के लिए।

इस comprehensive guide में, हम cover करेंगे gas mechanism, pricing, optimization techniques और 2026 best practices।

Gas क्या है?

Gas computational work की unit है Ethereum पर। हर operation जो EVM execute करता है gas cost करता है।

Why Gas Exists:

  • Spam prevention: Random operations costly बनाता है
  • Resource allocation: Miners/validators को compensate करता है
  • Halting problem: Infinite loops prevent करता है
  • Fair pricing: Complex operations ज्यादा cost करते हैं
  • Gas Anatomy

    Key Terms:

    Transaction Cost = Gas Used × Gas Price
    
    

    Gas Used : Actual computation done (units)

    Gas Price : Price per gas unit (gwei)

    Gas Limit : Maximum gas willing to spend

    Base Fee : Network-set minimum (EIP-1559)

    Priority Fee : Tip to validators

    Max Fee : Maximum total willing to pay

    Example:

    Gas Used: 21,000 (simple ETH transfer)
    

    Base Fee: 20 gwei

    Priority Fee: 2 gwei

    Total Price: 22 gwei

    Cost = 21,000 × 22 gwei

    = 462,000 gwei

    = 0.000462 ETH

    ≈ $1.39 (at $3000/ETH)

    EIP-1559 (London Fork)

    2021 में introduced, fundamentally changed gas।

    Before EIP-1559:

    Gas Price = Auction (users bid)
    

    All fees → Miners

    High volatility

    After EIP-1559:

    Max Fee = Base Fee + Priority Fee
    
    

    Base Fee:

    • Algorithm-determined
    • Burns (destroyed, not to validators)
    • Adjusts based on block fullness

    Priority Fee:

    • User-set tip
    • Goes to validators
    • Incentivizes inclusion

    Fee Calculation:

    // User specifies:
    

    maxFeePerGas = 50 gwei // Willing to pay maximum

    maxPriorityFeePerGas = 2 gwei // Tip to validator

    // Network determines:

    baseFeePerGas = 20 gwei // Current base fee

    // Actual fee paid:

    effectivePriorityFee = min(maxPriorityFeePerGas, maxFeePerGas - baseFeePerGas)

    = min(2, 50 - 20)

    = 2 gwei

    actualFeePerGas = baseFeePerGas + effectivePriorityFee

    = 20 + 2

    = 22 gwei

    // Refund:

    refund = (maxFeePerGas - actualFeePerGas) × gasUsed

    = (50 - 22) × 21000

    = 588,000 gwei

    Gas Costs by Operation

    Storage Operations (Most Expensive):

    | Operation | Gas Cost | Notes |

    |-----------|----------|-------|

    | SSTORE (new) | 20,000 | Write to new storage slot |

    | SSTORE (update) | 5,000 | Update existing slot |

    | SSTORE (delete) | -15,000 | Gas refund for deletion |

    | SLOAD | 2,100 | Read from storage |

    Memory Operations:

    | Operation | Gas Cost |

    |-----------|----------|

    | MSTORE | 3 |

    | MLOAD | 3 |

    | Memory expansion | 3 + quadratic |

    Stack Operations:

    | Operation | Gas Cost |

    |-----------|----------|

    | PUSH/POP | 3 |

    | DUP/SWAP | 3 |

    | ADD/SUB | 3 |

    | MUL/DIV | 5 |

    Other Operations:

    | Operation | Gas Cost |

    |-----------|----------|

    | CALL (non-zero value) | 9,000 |

    | CALL (zero value) | 2,600 |

    | SELFDESTRUCT | 5,000 |

    | CREATE | 32,000 |

    | KECCAK256 | 30 + 6/word |

    | LOG | 375 + 375/topic + 8/byte |

    Transaction Base Cost:

    | Type | Gas Cost |

    |------|----------|

    | Transaction | 21,000 |

    | + Contract creation | +32,000 |

    | + Calldata (zero byte) | +4 |

    | + Calldata (non-zero) | +16 |

    Gas Optimization Techniques

    1. Storage Optimization

    ❌ Inefficient:

    contract Inefficient {
    

    uint256 public a; // Slot 0

    uint256 public b; // Slot 1

    uint256 public c; // Slot 2

    function update() public {

    a = 1; // 20,000 gas

    b = 2; // 20,000 gas

    c = 3; // 20,000 gas

    // Total: 60,000 gas

    }

    }

    ✅ Efficient (Storage Packing):

    contract Efficient {
    

    uint128 public a; // Slot 0 (first half)

    uint128 public b; // Slot 0 (second half)

    uint128 public c; // Slot 1 (first half)

    function update() public {

    a = 1; // 20,000 gas (first write to slot 0)

    b = 2; // 5,000 gas (update slot 0)

    c = 3; // 20,000 gas (first write to slot 1)

    // Total: 45,000 gas (25% saving!)

    }

    }

    2. Use Calldata Instead of Memory

    ❌ Expensive:

    function process(uint256[] memory data) public {
    

    // memory copy: expensive

    }

    ✅ Cheap:

    function process(uint256[] calldata data) public {
    

    // No copy, direct access

    // ~200 gas saved per element

    }

    3. Cache Storage Reads

    ❌ Multiple reads:

    function inefficient() public view returns (uint256) {
    

    return storedValue + storedValue + storedValue;

    // 3 × 2100 = 6300 gas

    }

    ✅ Single read:

    function efficient() public view returns (uint256) {
    

    uint256 temp = storedValue; // 2100 gas

    return temp + temp + temp; // 9 gas

    // Total: 2109 gas (66% saving!)

    }

    4. Use Events Instead of Storage

    ❌ Store in contract:

    struct Action {
    

    address user;

    uint256 amount;

    uint256 timestamp;

    }

    Action[] public history; // Very expensive!

    function record(uint256 amount) public {

    history.push(Action(msg.sender, amount, block.timestamp));

    // ~40,000 gas per entry

    }

    ✅ Emit events:

    event ActionRecorded(
    

    address indexed user,

    uint256 amount,

    uint256 timestamp

    );

    function record(uint256 amount) public {

    emit ActionRecorded(msg.sender, amount, block.timestamp);

    // ~1,500 gas

    }

    // Query events off-chain!

    5. Use Custom Errors (Solidity 0.8.4+)

    ❌ String errors:

    require(msg.sender == owner, "Unauthorized: caller is not the owner");
    

    // ~5,000 gas

    ✅ Custom errors:

    error Unauthorized(address caller);
    
    

    if (msg.sender != owner) {

    revert Unauthorized(msg.sender);

    }

    // ~2,000 gas (60% saving!)

    6. Use Immutable/Constant

    ❌ Regular storage:

    address public owner; // 2100 gas per read

    ✅ Immutable/Constant:

    address public immutable owner; // Set in constructor, 100 gas per read
    

    uint256 public constant FEE = 100; // Inlined by compiler, 0 gas!

    7. Unchecked Math (When Safe)

    ❌ Default (overflow checks):

    function increment(uint256 x) public pure returns (uint256) {
    

    return x + 1; // ~30 gas

    }

    ✅ Unchecked (no overflow checks):

    function increment(uint256 x) public pure returns (uint256) {
    

    unchecked {

    return x + 1; // ~10 gas

    }

    // सिर्फ use करें जब overflow impossible हो!

    }

    8. Short-Circuit Conditionals

    // ❌ Expensive condition first
    

    require(expensiveCheck() && cheapCheck());

    // ✅ Cheap condition first

    require(cheapCheck() && expensiveCheck());

    // अगर cheapCheck fails, expensiveCheck skip हो जाता है

    9. Batch Operations

    ❌ Multiple transactions:

    // User pays 21,000 × 3 = 63,000 base gas
    

    token.transfer(alice, 100);

    token.transfer(bob, 200);

    token.transfer(charlie, 300);

    ✅ Single batched transaction:

    function batchTransfer(
    

    address[] calldata recipients,

    uint256[] calldata amounts

    ) public {

    // User pays 21,000 base gas once

    for (uint i = 0; i < recipients.length; i++) {

    _transfer(msg.sender, recipients[i], amounts[i]);

    }

    }

    10. Optimize Loops

    ❌ Inefficient:

    for (uint256 i = 0; i < array.length; i++) {
    

    // array.length read every iteration!

    }

    ✅ Efficient:

    uint256 length = array.length; // Cache length
    

    for (uint256 i = 0; i < length; ) {

    // Loop body

    unchecked { ++i; } // Unchecked increment

    }

    Gas Profiling Tools

    1. Foundry Gas Reports

    forge test --gas-report

    Output:

    | Function      | avg   | median | max    |
    

    |---------------|-------|--------|--------|

    | transfer | 51234 | 51234 | 51234 |

    | approve | 44556 | 44556 | 44556 |

    2. Hardhat Gas Reporter

    // hardhat.config.js
    

    require("hardhat-gas-reporter");

    module.exports = {

    gasReporter: {

    enabled: true,

    currency: "USD",

    coinmarketcap: "YOUR_API_KEY"

    }

    };

    3. Tenderly Debugger

    • Visual gas usage per line
    • Stack traces
    • State changes

    4. Remix Gas Profiler

    Built-in gas estimation per transaction।

    2026 Gas Landscape

    Current Gas Prices (Mainnet):

    | Network Activity | Base Fee | Priority Fee | Total |

    |-----------------|----------|--------------|-------|

    | Low | 5-15 gwei | 0.5-1 gwei | 5-16 gwei |

    | Medium | 15-40 gwei | 1-2 gwei | 16-42 gwei |

    | High | 40-100 gwei | 2-5 gwei | 42-105 gwei |

    | Extreme | 100-300+ gwei | 5-20 gwei | 105-320+ gwei |

    Transaction Costs (at 30 gwei):

    | Operation | Gas | Cost (ETH) | Cost (USD @ $3000) |

    |-----------|-----|------------|-------------------|

    | ETH transfer | 21,000 | 0.00063 | $1.89 |

    | ERC-20 transfer | 65,000 | 0.00195 | $5.85 |

    | Uniswap swap | 150,000 | 0.0045 | $13.50 |

    | NFT mint | 200,000 | 0.006 | $18 |

    | Contract deploy | 1,000,000+ | 0.03+ | $90+ |

    L2 Solutions (2026):

    | Network | Gas Cost vs L1 | Typical Transfer Cost |

    |---------|----------------|----------------------|

    | Ethereum L1 | 1x | $2-10 |

    | Arbitrum | 0.01-0.05x | $0.02-0.50 |

    | Optimism | 0.01-0.05x | $0.02-0.50 |

    | Base | 0.01-0.05x | $0.02-0.50 |

    | zkSync | 0.01-0.03x | $0.02-0.30 |

    | Polygon zkEVM | 0.005-0.02x | $0.01-0.20 |

    Best Practices Checklist

    • [ ] Storage packing for related variables
    • [ ] calldata for external function parameters
    • [ ] Cache storage reads in loops
    • [ ] Custom errors instead of strings
    • [ ] immutable for constructor-set values
    • [ ] constant for compile-time values
    • [ ] Events for historical data
    • [ ] Batch operations when possible
    • [ ] Unchecked math जहां safe हो
    • [ ] Short-circuit boolean logic
    • [ ] Gas profiling before deployment
    • [ ] Consider L2 deployment

    निष्कर्ष

    Gas optimization Solidity development का critical skill है। छोटे improvements compound होकर significant savings बन सकते हैं।

    Key takeaways:

    • Storage सबसे expensive है
    • Cache storage reads
    • Custom errors use करें
    • Calldata > memory for external functions
    • Profile gas regularly
    • Consider L2 for cheaper transactions

    अगले कदम: Solingo पर gas optimization challenges solve करें!

    ---

    अतिरिक्त Resources:

    Practice में लगाने के लिए तैयार हैं?

    Solingo पर interactive exercises के साथ इन concepts को apply करें।

    मुफ्त में शुरू करें