Software Engineer

Solidity Gas Optimization β€” Agent Saved 40% on Contract Deployment

40% gas reduction in 15 minutesEngineering & DevOps4 min read

Key Takeaway

Our Web3 agent analyzed a Solidity smart contract function-by-function, found storage packing opportunities, calldata misuse, and redundant operations β€” then rewrote the contract to cut deployment gas by 40% in 15 minutes.

The Problem

Gas costs on Ethereum are a tax on bad code. Every wasted storage slot, every unnecessary memory copy, every unoptimized loop β€” you're paying for it. And your users are paying for it on every transaction.

We had a token vesting contract for an Artificial-Lab project. It worked. Tests passed. But deployment cost 3.2M gas, and key functions like claimVested() were running at 85K gas per call. For a contract that thousands of users would interact with, that's real money burned on inefficiency.

A senior Solidity developer could optimize it. In a day or two. We needed it in 15 minutes.

The Solution

Upload the contract to our Web3 sub-agent. It analyzes gas usage at the function level, identifies optimization opportunities by category, rewrites the contract, and generates a before/after Hardhat gas report.

The Process

Step 1: Gas Profiling

The agent first profiles every function using Hardhat's gas reporter:

javascriptShow code
// hardhat.config.js
module.exports = {
  gasReporter: {
    enabled: true,
    currency: 'USD',
    gasPrice: 30,
    showMethodSig: true,
    showTimeSpent: true,
  }
};

Step 2: Storage Packing

The original contract had three separate storage slots for related data:

solidityShow code
// BEFORE: 3 storage slots = 3 Γ— SSTORE operations
struct VestingSchedule {
    uint256 totalAmount;      // slot 0 (256 bits)
    uint256 startTime;        // slot 1 (256 bits)
    uint256 claimedAmount;    // slot 2 (256 bits)
    bool isRevocable;         // slot 3 (8 bits, wastes 248 bits)
    address beneficiary;      // slot 4 (160 bits, wastes 96 bits)
}

Agent rewrote it:

solidityShow code
// AFTER: 2 storage slots β€” packed tightly
struct VestingSchedule {
    uint128 totalAmount;      // slot 0, bits 0-127
    uint128 claimedAmount;    // slot 0, bits 128-255
    uint64 startTime;         // slot 1, bits 0-63 (good until year 2554)
    address beneficiary;      // slot 1, bits 64-223 (160 bits)
    bool isRevocable;         // slot 1, bit 224
}

Three slots became two. Every write saves 20,000 gas (cold SSTORE cost).

Step 3: Calldata vs Memory

solidityShow code
// BEFORE: copying array to memory unnecessarily
function batchClaim(uint256[] memory scheduleIds) external {

// AFTER: read directly from calldata β€” cheaper
function batchClaim(uint256[] calldata scheduleIds) external {

calldata is read-only and cheaper than memory for external function parameters. The agent flagged every instance.

Step 4: Loop Optimization

solidityShow code
// BEFORE: reading array length on every iteration
for (uint256 i = 0; i < schedules.length; i++) {

// AFTER: cache length, use unchecked increment (Solidity β‰₯0.8.0, no overflow risk)
uint256 len = schedules.length;
for (uint256 i = 0; i < len; ) {
    // ... loop body
    unchecked { ++i; }
}

Step 5: Verification

The agent ran the full test suite against the optimized contract β€” all 34 tests passing β€” then generated the comparative gas report.

The Results

FunctionBefore (gas)After (gas)Savings
Deploy3,200,0001,890,00041%
claimVested()85,20048,70043%
createSchedule()112,00068,40039%
batchClaim(10)620,000385,00038%
revoke()45,80031,20032%
Optimization time1-2 days (dev)15 minutes (agent)99%

At 30 gwei and ETH at $3,500, the deployment savings alone were ~$146. Over 10,000 claimVested() calls, that's $3,800 saved for users.

Try It Yourself

Start with Hardhat's gas reporter plugin β€” you can't optimize what you can't measure. Focus on storage packing first (biggest wins), then calldata, then loop optimizations. The Solidity compiler doesn't pack structs for you β€” that's on you, or on your agent.


Gas optimization isn't premature optimization. It's respecting your users' wallets.

SolidityGas OptimizationWeb3Smart ContractsEthereum

Want results like these?

Start free with your own AI team. No credit card required.

Solidity Gas Optimization β€” Agent Saved 40% on Contract Deployment β€” Mr.Chief