Software Engineer
Solidity Gas Optimization β Agent Saved 40% on Contract Deployment
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
| Function | Before (gas) | After (gas) | Savings |
|---|---|---|---|
| Deploy | 3,200,000 | 1,890,000 | 41% |
| claimVested() | 85,200 | 48,700 | 43% |
| createSchedule() | 112,000 | 68,400 | 39% |
| batchClaim(10) | 620,000 | 385,000 | 38% |
| revoke() | 45,800 | 31,200 | 32% |
| Optimization time | 1-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.
Related case studies
Software Engineer
Generating Hardhat Test Suites β 50 Edge Cases the Developer Missed
An AI agent generated 50 Hardhat test cases for a Solidity contract in 10 minutes β 8 revealed undocumented behavior. See how automated test generation catches what developers miss.
Software Engineer
Smart Contract Audit β Found 3 Vulnerabilities Before Deployment
An AI agent audited a Solidity smart contract in 5 minutes and found 3 vulnerabilities including a critical reentrancy bug β work that would cost $5-50K from a professional auditor.
Full-Stack Developer
Auto-Generated API Documentation β From Code to Docs in 3 Minutes
How an AI agent reads Django views, serializers, and models to generate complete OpenAPI specs and markdown API docs in 3 minutes. Auto-updates on every merge.
Want results like these?
Start free with your own AI team. No credit card required.