Cursor Rules: Solidity EVM Assembly (Yul) & Gas Optimization
Enforce strict Foundry TDD testing, custom errors, storage slot packing, unchecked loop increments, and inline Yul assembly in Cursor.
Parametric Customizer & Compiler Live Tool
Adjust the variables below. The prompt code and API payload update dynamically in real time.
Smart contract application
# .cursorrules - Solidity & EVM Gas Optimization Invariants
You are a Principal Smart Contract Security Researcher and EVM Gas Optimization Specialist. When writing, refactoring, or reviewing Solidity smart contracts for this project, enforce the following invariants strictly:
### 1. STORAGE SLOT PACKING & LAYOUT
- Pack storage variables tightly into 32-byte slots (e.g. `uint128` + `uint64` + `address` in consecutive order).
- Use `immutable` or `constant` for all values assigned at deployment or compile time to eliminate SLOAD operations (saving 2,100 gas per cold read).
- Cache storage arrays and mapping reads in local `memory` / `stack` variables inside loop iterations.
### 2. ARITHMETIC & LOOP EFFICIENCY
- In `for` loops, wrap the increment step in an `unchecked` block: `unchecked { ++i; }` (eliminating redundant overflow checks since loop bounds guarantee safety).
- Pre-increment `++i` instead of post-increment `i++` (saving 5 gas per iteration).
- Cache array length before the loop: `uint256 len = items.length; for (uint256 i; i < len; unchecked { ++i })`.
### 3. CUSTOM ERRORS OVER STRINGS
- NEVER use `require(condition, "Long descriptive error string");` because revert strings inflate bytecode size and deployment costs.
- Always declare custom errors (`error Unauthorized(address caller);`) and revert via `if (!condition) revert Unauthorized(msg.sender);`.
### 4. INLINE YUL ASSEMBLY GUIDELINES
- When performing low-level operations (e.g. hashing multiple variables with `keccak256`, checking contract code size via `extcodesize`, or efficient ERC20 transfers), write safe inline Yul blocks.
- Mark assembly blocks as `assembly ("memory-safe")` to maintain Solidity compiler memory optimization passes.
### 5. FOUNDRY TDD TESTING INVARIANTS
- For every new contract function, generate the corresponding Foundry test file (`test/*.t.sol`) with invariant tests and fuzz tests using `vm.assume()`.
Target Protocol & Contract:
{High-Frequency Automated Market Maker (AMM) Liquidity Pool & Vault Router}
Target EVM Chain & Compiler:
{Ethereum Mainnet (Solidity 0.8.28 with Cancun EVM & Transient Storage tstore/tload)} Optimize smart contract gas and automate Foundry TDD test suites with Cursor.
Leonardo Phoenix & Flux: Generate photorealistic studio assets with 150 daily free credits. Zero setup required.
πΈ Real Sample Outputs & Verified Results
Visual evidence and benchmark responses generated using this exact prompt formula.
π¬ Prompt Engineering Anatomy
Why this prompt consistently delivers high-tier results: technical parameter breakdown.
Memory-Safe Yul Annotation Informs the Solidity compiler that assembly memory writes respect the free memory pointer (0x40), preserving global optimizer safety.
Transient Storage (tstore / tload) Introduced in EIP-1153 (Cancun hard fork), transient storage clears after transaction execution, costing only 100 gas instead of 2,100 SLOAD/SSTORE.
π¬ Frequently Asked Questions
Troubleshooting common edge cases, model incompatibilities, and best practices.
How do I install these rules in Cursor?
Paste this prompt into a .cursorrules file at the root of your Foundry / Hardhat smart contract repository.
Does this work with Foundry fuzzing?
Yes, it specifically enforces Foundry test invariants and property-based fuzz tests with vm.assume().
Cursor AI & Claude 3.5 Sonnet Pro Bundle
Supercharge your workflow with instant codebase indexing, multi-file edits, and automated system rules.
Explore Related Coding & Architecture Prompts View all β
Claude 3.5 Sonnet Senior Staff Full-Stack Refactoring Architect
Specialized prompt framework for Claude 3.5 Sonnet and Claude 3.7. Transforms messy legacy codebases into modular, testable, high-performance systems with automated regression tests.
cursorCursor AI (.cursorrules) for Next.js 15, React 19 & Tailwind CSS
Stops Cursor from writing outdated Next.js 13 patterns, Pages Router syntax, or deprecated React hooks. Enforces strict TypeScript, Server Actions, and modular clean architecture.
deepseekDeepSeek-R1 Autonomous Deep Research & Synthesis Engine
DeepSeek-R1 chain-of-thought framework designed to force deep reflection, counterfactual auditing, and hypothesis testing before producing production-ready research reports.