Loading...
Loading...
Gas optimization patterns for Solidity smart contracts. Use when optimizing contract deployment costs, runtime gas usage, or storage efficiency. Covers storage packing, custom errors, immutable variables, calldata optimization, loop patterns, assembly usage, and Solady gas-optimized alternatives. Triggers on tasks involving gas optimization, storage layout, deployment cost reduction, or EVM efficiency.
npx skill4agent add whackur/solidity-agent-toolkit solidity-gas-optimization// BAD: 3 slots
uint128 a; uint256 b; uint128 c;
// GOOD: 2 slots
uint128 a; uint128 c; uint256 b;// BAD: revert("Unauthorized");
// GOOD:
error Unauthorized();
if (msg.sender != owner) revert Unauthorized();constantimmutable// GOOD: Saves ~20k gas per read vs storage
uint256 public constant FEE = 100;
address public immutable factory;calldata// BAD: function process(uint[] memory data)
// GOOD: function process(uint[] calldata data)unchecked// GOOD: Saves gas on every iteration
for (uint i = 0; i < len; ) {
// logic
unchecked { ++i; }
}&&||// GOOD: cheapCondition() checked first
if (cheapCondition() && expensiveCondition()) { ... }// GOOD: Cache length, use calldata/memory
uint len = arr.length;
for (uint i = 0; i < len; ++i) { ... }uint256bytes32string// GOOD: bytes32 is cheaper than string
bytes32 public constant NAME = "MyToken";TSTORETLOAD// GOOD: EIP-1153 (Solidity >=0.8.24)
assembly { tstore(slot, value) }// GOOD: Efficient transfer
assembly {
let success := call(gas(), to, amount, 0, 0, 0, 0)
}// GOOD: Use Solady's SafeTransferLib
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
SafeTransferLib.safeTransfer(token, to, amount);// GOOD: Deploy proxy instead of full contract
address instance = LibClone.clone(implementation);constantimmutablecalldatamemoryuncheckeduint256bytes32stringshort-circuitingexternalpublicSoladyTSTORETLOADsolidity-agent-toolkitgas_snapshotinspect_storageestimate_gas