NexaPay
    Back to Blog
    Engineering

    Gas Optimization on Polygon PoS: Reducing Settlement Costs by 94%

    Engineering diary: How we achieved sub-$0.001 transaction costs at scale

    Alex Rivera

    Alex Rivera

    Blockchain Engineer

    January 9, 202616 min read
    Gas Optimization on Polygon PoS: Reducing Settlement Costs by 94%

    Key Takeaways

    • Transaction batching reduces per-payment gas cost by 87%
    • Merkle tree commitments enable off-chain aggregation
    • Average settlement cost: $0.00089 per payment
    • Daily gas budget reduced from $14,000 to $840

    When we launched on Polygon, naive implementation cost us $0.017 per settlement. At 50,000 daily transactions, we were burning $850/day in gas. Sustainable at our margins, but leaving significant optimization on the table.

    The Batching Architecture

    Individual on-chain settlements are wasteful. Each transaction carries base costs for signature verification, state access, and event emission. Batching amortizes these costs across many settlements.

    // Before: Individual settlements
    for (settlement of settlements) {
      await contract.settle(settlement); // $0.017 each
    }
    
    // After: Batched settlements
    const merkleRoot = computeMerkleRoot(settlements);
    await contract.settleBatch(merkleRoot, proofs); // $0.00089 per settlement

    We batch settlements every 15 minutes or 1,000 payments, whichever comes first. A single on-chain transaction commits a Merkle root; individual merchants can prove inclusion and claim funds independently.

    Calldata Compression

    Ethereum's calldata costs 16 gas per non-zero byte. We compress settlement data using domain-specific encoding, reducing average calldata size from 847 bytes to 124 bytes per settlement.

    • Address compression using registry indexes (20 bytes → 3 bytes)
    • Amount encoding with decimal scaling (32 bytes → 6 bytes)
    • Timestamp delta encoding (8 bytes → 2 bytes)
    Alex Rivera

    Written by Alex Rivera

    Blockchain Engineer