Learn-skills.dev solidity-coding
[AUTO-INVOKE] MUST be invoked BEFORE writing or modifying any Solidity contract (.sol files). Covers pragma version, naming conventions, project layout, OpenZeppelin library selection standards, Chainlink integration, and anti-patterns. Trigger: any task involving creating, editing, or reviewing .sol source files.
install
source · Clone the upstream repo
git clone https://github.com/NeverSight/learn-skills.dev
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/NeverSight/learn-skills.dev "$T" && mkdir -p ~/.claude/skills && cp -r "$T/data/skills-md/0xlayerghost/solidity-agent-kit/solidity-coding" ~/.claude/skills/neversight-learn-skills-dev-solidity-coding && rm -rf "$T"
manifest:
data/skills-md/0xlayerghost/solidity-agent-kit/solidity-coding/SKILL.mdsource content
Solidity Coding Standards
Language Rule
- Always respond in the same language the user is using. If the user asks in Chinese, respond in Chinese. If in English, respond in English.
Coding Principles
- Pragma: Use
— keep consistent across all files in the projectpragma solidity ^0.8.20; - Dependencies: OpenZeppelin Contracts 4.9.x, manage imports via
remappings.txt - Error Handling: Prefer custom errors over
strings — saves gas and is more expressiverequire- Define:
error InsufficientBalance(uint256 available, uint256 required); - Use:
if (balance < amount) revert InsufficientBalance(balance, amount);
- Define:
- Documentation: All
/public
functions must have NatSpec (external
,@notice
,@param
)@return - Event Indexing: Only add
toindexed
type parameters — add comment if indexing other typesaddress - Special Keywords:
/immutable
/constant
/unchecked
must have inline comment explaining whyassembly
Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Contract / Library | PascalCase | , |
| Interface | + PascalCase | , |
| State variable / Function | lowerCamelCase | , |
| Constant / Immutable | UPPER_SNAKE_CASE | , |
| Event | PascalCase (past tense) | , |
| Custom Error | PascalCase | , |
| Function parameter | prefix for setter | |
- Forbidden: Pinyin names, single-letter variables (except
in loops), excessive abbreviationsi/j/k
Code Organization Rules
| Situation | Rule |
|---|---|
| Cross-contract constants | Place in |
| Interface definitions | Place in , separate from implementation |
| Simple on-chain queries | Use or |
| Complex multi-step operations | Use |
| Import style | Use named imports: |
Project Directory Structure
src/ — Contract source code interfaces/ — Interface definitions (I*.sol) common/ — Shared constants, types, errors (Const.sol, Types.sol) test/ — Test files (*.t.sol) script/ — Deployment & interaction scripts (*.s.sol) config/ — Network config, parameters (*.json) deployments/ — Deployment records (latest.env) docs/ — Documentation, changelogs lib/ — Dependencies (managed by forge install)
Configuration Management
— network RPC URLs, contract addresses, business parametersconfig/*.json
— latest deployed contract addresses, must update after each deploymentdeployments/latest.env
— compiler version, optimizer settings, remappingsfoundry.toml- Important config changes must be documented in the PR description
OpenZeppelin Library Selection Standards
When writing Solidity contracts, prioritize using battle-tested OpenZeppelin libraries over custom implementations. Select the appropriate library based on the scenario:
Access Control
| Scenario | Library | Import Path |
|---|---|---|
| Single owner management | | |
| Owner transfer needs safety | | |
| Multi-role permission (admin/operator/minter) | | |
| Need to enumerate role members | | |
| Governance with timelock delay | | |
Rule: Single owner →
Ownable2Step; 2+ roles → AccessControl; governance/DAO → TimelockController
Security Protection
| Scenario | Library | Usage |
|---|---|---|
| External call / token transfer | | Add modifier |
| Emergency pause needed | | Add to user-facing functions; keep admin functions unpaused |
| ERC20 token interaction | | Use / / instead of raw calls |
Rule: Any contract that transfers tokens or ETH MUST use
ReentrancyGuard + SafeERC20
Token Standards
| Scenario | Library | Notes |
|---|---|---|
| Fungible token | | Base standard |
| Token with burn mechanism | | Adds and |
| Token with max supply cap | | Enforces |
| Gasless approval (EIP-2612) | | Saves users approve tx gas |
| Governance voting token | | Snapshot-based voting power |
| NFT | | Base NFT standard |
| NFT with enumeration | | Supports queries |
| Multi-token (FT + NFT mixed) | | Game items, batch operations |
Utility Libraries
| Scenario | Library | Usage |
|---|---|---|
| Whitelist / airdrop verification | | Gas-efficient Merkle tree verification |
| Signature verification | + | Off-chain sign + on-chain verify |
| Auto-increment IDs | | Token ID, order ID generation |
| Batch function calls | | Multiple operations in one tx |
| Address set / uint set | | Iterable sets with O(1) add/remove/contains |
| Revenue sharing | | Split ETH/token payments by shares |
| Standardized yield vault | | DeFi vault standard |
Contract Upgrade
| Scenario | Library | Notes |
|---|---|---|
| Upgradeable contract (gas efficient) | | Upgrade logic in implementation contract |
| Upgradeable contract (admin separated) | | Upgrade logic in proxy, higher gas |
| Initializer (replace constructor) | | Use modifier instead of constructor |
Rule: New projects prefer
UUPSUpgradeable; always use Initializable for upgradeable contracts
Chainlink Integration
| Scenario | Library | Notes |
|---|---|---|
| Token price data | | Only for tokens with Chainlink feed; check data.chain.link |
| Verifiable randomness (lottery/NFT) | | On-chain provably fair random numbers |
| Automated execution (cron jobs) | | Replace centralized keepers |
| Cross-chain messaging | | Cross-chain token/message transfer |
Library Selection Decision Flow
Does contract handle user funds/tokens? ├── YES → Add ReentrancyGuard + SafeERC20 │ Does it need emergency stop? │ ├── YES → Add Pausable │ └── NO → Skip └── NO → Skip How many admin roles needed? ├── 1 role → Ownable2Step ├── 2+ roles → AccessControl └── DAO/governance → TimelockController Does contract need price data? ├── Token has Chainlink feed → AggregatorV3Interface ├── No Chainlink feed → Custom TWAP with min-liquidity check └── No price needed → Skip Will contract need upgrades? ├── YES → UUPSUpgradeable + Initializable └── NO → Standard deployment (immutable)
Anti-Patterns (Do NOT)
- Do NOT write custom
wrappers — usetransferSafeERC20 - Do NOT write custom access control modifiers — use
/OwnableAccessControl - Do NOT write custom pause logic — use
Pausable - Do NOT use
on Solidity >= 0.8.0 — overflow checks are built-inSafeMath - Do NOT use
— userequire(token.transfer(...))
viatoken.safeTransfer(...)SafeERC20 - Do NOT use
for auth — usetx.origin
withmsg.sender
/OwnableAccessControl
Foundry Quick Reference
# Create new project forge init <project-name> # Install dependency forge install OpenZeppelin/openzeppelin-contracts@v4.9.6 # Build contracts forge build # Format code forge fmt # Update remappings forge remappings > remappings.txt