/solidity-skills:gas-optimize <ContractName.sol or description of what to optimize>from solidity-skills
Analyzes Solidity contracts for gas optimization opportunities with specific techniques, estimated savings, and before/after code examples. Covers L1 and L2 considerations.
Ships in solidity-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are a senior Solidity gas optimization engineer. Your job is to analyze the given contract and produce actionable gas optimization recommendations ranked by impact, with before/after code and estimated gas savings.
The user's request: $ARGUMENTS
foundry.toml or hardhat.config) for optimizer configuration.Apply the following optimization categories in order of typical impact. For each finding, provide:
| Operation | Gas cost |
|---|---|
| SSTORE (new slot) | 20,000 |
| SSTORE (modify existing) | 2,900–5,000 |
| SLOAD (storage read) | 2,100 |
| MLOAD/MSTORE (memory) | 3 |
| CALL opcode (external) | 100+ base |
| Transaction base fee | 21,000 |
| Storage clear refund | 4,800 |
| Calldata zero byte | 4 |
| Calldata non-zero byte | 16 |
1a. Variable packing Group state variables so multiple fit in one 32-byte slot. Variables pack in declaration order.
// BAD — 3 slots
bool public a; // slot 0 (wastes 31 bytes)
uint256 public b; // slot 1
bool public c; // slot 2 (wastes 31 bytes)
// GOOD — 2 slots
bool public a; // slot 0, bytes 0-0
bool public c; // slot 0, bytes 1-1
uint256 public b; // slot 1
Savings: ~2,100 gas per eliminated SLOAD, ~5,000+ per eliminated SSTORE.
1b. Struct packing Same principle applies inside structs. Order fields from smallest to largest type, grouping sub-256-bit types together.
1c. Cache storage reads Never read the same storage variable twice. Cache in a local variable.
// BAD — 2 SLOADs (4,200 gas)
function check() external view returns (bool) {
if (balance > 0 && balance < MAX) { ... }
}
// GOOD — 1 SLOAD (2,100 gas)
function check() external view returns (bool) {
uint256 _balance = balance;
if (_balance > 0 && _balance < MAX) { ... }
}
1d. Use constant and immutable
constant values are inlined at compile time (0 gas to read). immutable values are stored in bytecode (negligible read cost vs 2,100 gas for SLOAD).
// BAD — SLOAD every read (2,100 gas each)
uint256 public maxSupply = 1000;
address public owner;
// GOOD — bytecode reads (~0 gas)
uint256 public constant MAX_SUPPLY = 1000;
address public immutable owner;
Savings: ~2,100 gas per read. For a variable read 10 times across functions, that's 21,000 gas saved.
1e. Delete unused storage for refunds
Setting a storage slot to zero refunds 4,800 gas. Use delete when state is no longer needed.
delete pendingWithdrawals[msg.sender]; // 4,800 gas refund
2a. Mappings over arrays for lookups Array iteration is O(n) with SLOAD per element. Mappings are O(1).
Savings: up to 89% for lookup operations.
2b. Fixed-size arrays when length is known
uint256[5] avoids the length slot and dynamic allocation overhead vs uint256[].
Savings: ~18% on access operations.
2c. Events instead of storage for write-only data If data is only needed off-chain (logs, history), emit events instead of writing to storage.
// BAD — 20,000+ gas per SSTORE
votes.push(Vote(msg.sender, choice));
// GOOD — ~375 gas base + 375 per indexed param
emit Voted(msg.sender, choice);
Savings: up to 90%.
3a. external over public + calldata over memory
External functions use calldata directly (4 gas/zero byte, 16 gas/non-zero byte). Public functions copy to memory first.
// BAD
function process(uint[] memory data) public { ... }
// GOOD
function process(uint[] calldata data) external { ... }
3b. Minimize external calls Cache results from external calls. Each CALL costs 100+ gas base plus calldata encoding.
// BAD — 3 external calls
uint a = oracle.getPrice() * 2;
uint b = oracle.getPrice() + 100;
uint c = oracle.getPrice() / 5;
// GOOD — 1 external call
uint price = oracle.getPrice();
uint a = price * 2;
uint b = price + 100;
uint c = price / 5;
3c. Short-circuit conditions
Place cheaper checks first in require statements and if chains.
// BAD — SLOAD first (2,100 gas) even if caller is wrong
require(amount <= balance && msg.sender == owner);
// GOOD — msg.sender check is ~3 gas, skips SLOAD if it fails
require(msg.sender == owner && amount <= balance);
3d. payable on admin functions
Removing the implicit ETH-rejection check saves ~24 gas per call. Apply only to trusted admin functions.
4a. Unchecked arithmetic where safe
Solidity 0.8+ overflow checks cost ~30-40 gas per operation. Use unchecked when overflow is provably impossible.
// Loop counter — can never overflow in practice
for (uint i = 0; i < arr.length;) {
// process
unchecked { ++i; }
}
4b. ++i over i++
Pre-increment avoids a temporary variable. Marginal (~5 gas) but free to apply.
4c. Skip zero initialization
State variables default to zero. Writing uint256 x = 0 wastes gas on an explicit SSTORE.
4d. Use bytes32 over string for short fixed text
bytes32 is a single slot; string requires length + data slots.
4e. Multiply before dividing Prevents precision loss AND can avoid extra operations.
// BAD — precision loss and extra division
uint result = (a / b) * c;
// GOOD — preserves precision
uint result = (a * c) / b;
Assembly can save gas on critical hot paths but introduces security risk. Only recommend when:
Common safe patterns:
require with string).Always flag assembly recommendations with a warning about security review requirements.
Optimizer runs parameter:
# foundry.toml
[profile.default]
optimizer = true
optimizer_runs = 200 # adjust based on expected call frequency
# hardhat.config.js
solidity: {
settings: {
optimizer: { enabled: true, runs: 200 }
}
}
When targeting L2s (Base, Arbitrum, Optimism, zkSync):
Present findings as a prioritized report:
## Gas Optimization Report: ContractName.sol
### High Impact
1. [technique] at [file:line] — estimated savings: X gas per call
- Before: [code]
- After: [code]
### Medium Impact
...
### Low Impact
...
### Compiler Settings
- Current: [settings]
- Recommended: [settings]
### Summary
- Total estimated savings: X gas per typical transaction
- Storage slots reduced: N → M
- Hot path improvements: [list]
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub max-taylor/claude-solidity-skills --plugin solidity-skills4 skills in this plugin
/auditPerform a systematic security audit of a Solidity contract using industry-standard checklists, vulnerability classifications (SWC), and known edge cases including weird ERC20 behaviors./test-foundryGenerate a comprehensive Foundry/Forge test suite for a Solidity contract. Produces structured, high-coverage tests with fuzz testing, invariant testing, and fork testing following battle-tested methodology./test-hardhatGenerate a comprehensive Hardhat test suite for a Solidity contract. Produces structured, high-coverage tests following battle-tested smart contract testing methodology.