AgentSkillsCN

solidity-foundry

使用Foundry框架与OpenZeppelin 5.4模式,开发Solidity 0.8.33智能合约。在编写、测试或审计智能合约,实施访问控制、可升级合约,或运用安全模式时使用此功能。可在Solidity开发、Foundry测试(forge test)、模糊测试、不变量测试、部署脚本,或采用CEI模式与重入保护等安全最佳实践时触发。

SKILL.md
--- frontmatter
name: solidity-foundry
description: Solidity 0.8.33 smart contract development with Foundry framework and OpenZeppelin 5.4 patterns. Use when writing, testing, or auditing smart contracts, implementing access control, upgradeable contracts, or security patterns. Triggers on Solidity development, Foundry testing (forge test), fuzz testing, invariant testing, deployment scripts, or security best practices like CEI pattern and reentrancy guards.

Solidity Best Practices

Professional Solidity 0.8.33 development with Foundry and OpenZeppelin 5.4.

Quick Reference

TaskReference
Project setup, config, structurereferences/foundry.md
Access control, vaults, governance, upgradesreferences/openzeppelin.md
Permit2, Uniswap V4 hooks, flash loans, clonesreferences/protocols.md
Reentrancy, oracles, timelocksreferences/security.md
Unit, fuzz, invariant, fork testingreferences/testing.md
Storage packing, unchecked, custom errorsreferences/gas-optimization.md

Foundry Commands

bash
# Build & test
forge build
forge test -vvv
forge test --match-test testFuzz_

# Gas optimization
forge test --gas-report
forge snapshot

# Fork testing
forge test --fork-url $ETH_RPC_URL

# Deploy
forge script script/Deploy.s.sol --rpc-url $RPC --broadcast

Project Structure

code
├── src/                 # Contracts
├── test/                # Tests (.t.sol)
├── script/              # Deploy scripts (.s.sol)
├── foundry.toml         # Config
└── remappings.txt       # Import mappings

Security Checklist

VulnerabilityPrevention
ReentrancyCEI pattern, ReentrancyGuard
Access ControlOwnable2Step, AccessManager
Oracle ManipulationTWAP, Chainlink staleness checks
Flash Loan AttacksSame-block checks, invariants
CentralizationTimelocks, multisig

Best Practices

  1. Foundry - Fastest toolchain, built-in fuzzing
  2. OpenZeppelin 5.x - AccessManager, Ownable2Step
  3. CEI Pattern - Checks-Effects-Interactions always
  4. Custom Errors - Save ~50 gas per revert
  5. Test Coverage - Unit + fuzz + invariant + fork