重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
solidity-coding by 0xlayerghost/solidity-agent-kit
npx skills add https://github.com/0xlayerghost/solidity-agent-kit --skill solidity-codingpragma solidity ^0.8.19; — 保持项目内所有文件一致remappings.txt 管理导入require 字符串 — 节省 Gas 且更具表现力
error InsufficientBalance(uint256 available, uint256 required);if (balance < amount) revert InsufficientBalance(balance, amount);public / 函数必须包含 NatSpec 注释(、、)广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
external@notice@param@returnaddress 类型参数添加 indexed — 如果索引其他类型需添加注释immutable / constant / unchecked / assembly 必须附带内联注释说明原因| 元素 | 约定 | 示例 |
|---|---|---|
| 合约 / 库 | PascalCase | MyToken, StakingPool |
| 接口 | I + PascalCase | IMyToken, IStakingPool |
| 状态变量 / 函数 | lowerCamelCase | totalSupply, claimDividend |
| 常量 / 不可变变量 | UPPER_SNAKE_CASE | MAX_SUPPLY, ROUTER_ADDRESS |
| 事件 | PascalCase (过去式) | TokenTransferred, PoolCreated |
| 自定义错误 | PascalCase | InsufficientBalance, Unauthorized |
| 函数参数 | Setter 函数参数前缀 _ | function setFee(uint256 _fee) |
i/j/k 除外)、过度缩写| 情况 | 规则 |
|---|---|
| 跨合约常量 | 放置在 src/common/Const.sol |
| 接口定义 | 放置在 src/interfaces/I<Name>.sol,与实现分离 |
| 简单的链上查询 | 使用 Foundry cast CLI (call / send) |
| 复杂的多步骤操作 | 使用 Foundry 脚本 (*.s.sol) |
| 导入风格 | 使用命名导入:import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
src/ — 合约源代码
interfaces/ — 接口定义 (I*.sol)
common/ — 共享常量、类型、错误 (Const.sol, Types.sol)
test/ — 测试文件 (*.t.sol)
script/ — 部署和交互脚本 (*.s.sol)
config/ — 网络配置、参数 (*.json)
deployments/ — 部署记录 (latest.env)
docs/ — 文档、变更日志
lib/ — 依赖项 (由 Foundry 管理)
config/*.json — 网络 RPC URL、合约地址、业务参数deployments/latest.env — 最新部署的合约地址,每次部署后必须更新foundry.toml — 编译器版本、优化器设置、重映射编写 Solidity 合约时,优先使用经过实战检验的 OpenZeppelin 库,而非自定义实现。根据场景选择合适的库:
| 场景 | 库 | 导入路径 |
|---|---|---|
| 单一所有者管理 | Ownable | @openzeppelin/contracts/access/Ownable.sol |
| 所有者转移需要安全确认 | Ownable2Step | @openzeppelin/contracts/access/Ownable2Step.sol |
| 多角色权限(管理员/操作员/铸造者) | AccessControl | @openzeppelin/contracts/access/AccessControl.sol |
| 需要枚举角色成员 | AccessControlEnumerable | @openzeppelin/contracts/access/AccessControlEnumerable.sol |
| 具有时间锁延迟的治理 | TimelockController | @openzeppelin/contracts/governance/TimelockController.sol |
规则:单一所有者 → Ownable2Step;2+ 个角色 → AccessControl;治理/DAO → TimelockController
| 场景 | 库 | 用法 |
|---|---|---|
| 外部调用 / 代币转账 | ReentrancyGuard | 添加 nonReentrant 修饰符 |
| 需要紧急暂停 | Pausable | 对面向用户的函数添加 whenNotPaused;保持管理员函数不被暂停 |
| ERC20 代币交互 | SafeERC20 | 使用 safeTransfer / safeTransferFrom / safeApprove 代替原始调用 |
规则:任何转移代币或 ETH 的合约必须使用 ReentrancyGuard + SafeERC20
| 场景 | 库 | 备注 |
|---|---|---|
| 同质化代币 | ERC20 | 基础标准 |
| 具有销毁机制的代币 | ERC20Burnable | 添加 burn() 和 burnFrom() |
| 具有最大供应上限的代币 | ERC20Capped | 强制执行 totalSupply <= cap |
| 免 Gas 批准(EIP-2612) | ERC20Permit | 为用户节省批准交易的 Gas |
| 治理投票代币 | ERC20Votes | 基于快照的投票权 |
| NFT | ERC721 | 基础 NFT 标准 |
| 可枚举的 NFT | ERC721Enumerable | 支持 tokenOfOwnerByIndex 查询 |
| 多代币(FT + NFT 混合) | ERC1155 | 游戏道具、批量操作 |
| 场景 | 库 | 用法 |
|---|---|---|
| 白名单 / 空投验证 | MerkleProof | Gas 高效的默克尔树验证 |
| 签名验证 | ECDSA + EIP712 | 链下签名 + 链上验证 |
| 自动递增 ID | Counters | 代币 ID、订单 ID 生成 |
| 批量函数调用 | Multicall | 在一个交易中执行多个操作 |
| 地址集合 / 无符号整数集合 | EnumerableSet | 可迭代集合,具有 O(1) 的添加/移除/包含操作 |
| 收益分享 | PaymentSplitter | 按份额分割 ETH/代币支付 |
| 标准化收益金库 | ERC4626 | DeFi 金库标准 |
| 场景 | 库 | 备注 |
|---|---|---|
| 可升级合约(Gas 高效) | UUPSUpgradeable | 升级逻辑在实现合约中 |
| 可升级合约(管理员分离) | TransparentUpgradeableProxy | 升级逻辑在代理合约中,Gas 消耗更高 |
| 初始化器(替代构造函数) | Initializable | 使用 initializer 修饰符代替构造函数 |
规则:新项目优先选择 UUPSUpgradeable;可升级合约始终使用 Initializable
| 场景 | 库 | 备注 |
|---|---|---|
| 代币价格数据 | AggregatorV3Interface | 仅适用于有支持的预言机数据源的代币 |
| 可验证随机性(抽奖/NFT) | VRFConsumerBaseV2 | 链上可证明公平的随机数 |
| 自动化执行(定时任务) | AutomationCompatible | 替代中心化的守护者 |
| 跨链消息传递 | CCIP | 跨链代币/消息转移 |
合约是否处理用户资金/代币?
├── 是 → 添加 ReentrancyGuard + SafeERC20
│ 是否需要紧急停止?
│ ├── 是 → 添加 Pausable
│ └── 否 → 跳过
└── 否 → 跳过
需要多少个管理员角色?
├── 1 个角色 → Ownable2Step
├── 2+ 个角色 → AccessControl
└── DAO/治理 → TimelockController
合约是否需要价格数据?
├── 代币有预言机数据源 → AggregatorV3Interface
├── 没有预言机数据源 → 带有最小流动性检查的自定义 TWAP
└── 不需要价格数据 → 跳过
合约是否需要升级?
├── 是 → UUPSUpgradeable + Initializable
└── 否 → 标准部署(不可变)
transfer 包装器 — 使用 SafeERC20Ownable / AccessControlPausableSafeMath — 溢出检查已内置require(token.transfer(...)) — 通过 SafeERC20 使用 token.safeTransfer(...)tx.origin 进行授权 — 使用 msg.sender 配合 Ownable / AccessControl当配置了 OpenZeppelinContracts MCP 时,优先使用它来生成基础合约,而不是从头开始编写:
| 合约类型 | MCP 工具 | 何时使用 |
|---|---|---|
| 同质化代币 | solidity-erc20 | 任何新的 ERC20 代币合约 |
| NFT | solidity-erc721 | 任何新的 NFT 合约 |
| 多代币 | solidity-erc1155 | 游戏道具、批量操作 |
| 稳定币 | solidity-stablecoin | 符合 ERC20 标准的稳定币 |
| 现实世界资产 | solidity-rwa | 资产代币化 |
| 智能账户 | solidity-account | ERC-4337 账户抽象 |
| 治理 | solidity-governor | DAO 投票和提案 |
| 自定义 | solidity-custom | 具有 OZ 模式的非标准合约 |
工作流程:MCP 生成基础 → 应用本技能的命名/结构规则 → 定制业务逻辑 → 应用 /solidity-security 规则
为什么选择 MCP 而非手动编写:MCP 输出经过与 OZ Contracts Wizard 相同的规则集验证 — 导入、修饰符、安全检查保证正确。手动编码存在遗漏导入或使用错误 OZ 版本的风险。
何时不使用 MCP:具有非标准模式的高度自定义合约、不符合任何 OZ 模板的合约,或者需要从第一行代码开始进行精细控制的情况。
优雅降级:如果未配置 MCP,则回退到上述的库选择标准,并按照本技能中的所有规则手动编写合约。
| 操作 | 命令 |
|---|---|
| 创建新项目 | forge init <project-name> |
| 安装依赖项 | forge install openzeppelin-contracts |
| 构建合约 | forge build |
| 格式化代码 | forge fmt |
| 更新重映射 | forge remappings |
每周安装次数
34
代码仓库
GitHub 星标数
1
首次出现
2026年2月9日
安全审计
安装于
claude-code28
opencode20
gemini-cli20
codex20
cursor20
github-copilot17
pragma solidity ^0.8.19; — keep consistent across all files in the projectremappings.txtrequire strings — saves gas and is more expressive
error InsufficientBalance(uint256 available, uint256 required);if (balance < amount) revert InsufficientBalance(balance, amount);public / external functions must have NatSpec (@notice, @param, @return)indexed to address type parameters — add comment if indexing other typesimmutable / constant / unchecked / assembly must have inline comment explaining why| Element | Convention | Example |
|---|---|---|
| Contract / Library | PascalCase | MyToken, StakingPool |
| Interface | I + PascalCase | IMyToken, IStakingPool |
| State variable / Function | lowerCamelCase | totalSupply, claimDividend |
i/j/k in loops), excessive abbreviations| Situation | Rule |
|---|---|
| Cross-contract constants | Place in src/common/Const.sol |
| Interface definitions | Place in src/interfaces/I<Name>.sol, separate from implementation |
| Simple on-chain queries | Use Foundry cast CLI (call / send) |
| Complex multi-step operations | Use Foundry script (*.s.sol) |
| Import style | Use named imports: import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
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 Foundry)
config/*.json — network RPC URLs, contract addresses, business parametersdeployments/latest.env — latest deployed contract addresses, must update after each deploymentfoundry.toml — compiler version, optimizer settings, remappingsWhen writing Solidity contracts, prioritize using battle-tested OpenZeppelin libraries over custom implementations. Select the appropriate library based on the scenario:
| Scenario | Library | Import Path |
|---|---|---|
| Single owner management | Ownable | @openzeppelin/contracts/access/Ownable.sol |
| Owner transfer needs safety | Ownable2Step | @openzeppelin/contracts/access/Ownable2Step.sol |
| Multi-role permission (admin/operator/minter) | AccessControl | @openzeppelin/contracts/access/AccessControl.sol |
Rule : Single owner → Ownable2Step; 2+ roles → AccessControl; governance/DAO → TimelockController
| Scenario | Library | Usage |
|---|---|---|
| External call / token transfer | ReentrancyGuard | Add nonReentrant modifier |
| Emergency pause needed | Pausable | Add whenNotPaused to user-facing functions; keep admin functions unpaused |
| ERC20 token interaction | SafeERC20 | Use safeTransfer / safeTransferFrom / instead of raw calls |
Rule : Any contract that transfers tokens or ETH MUST use ReentrancyGuard + SafeERC20
| Scenario | Library | Notes |
|---|---|---|
| Fungible token | ERC20 | Base standard |
| Token with burn mechanism | ERC20Burnable | Adds burn() and burnFrom() |
| Token with max supply cap | ERC20Capped | Enforces totalSupply <= cap |
| Gasless approval (EIP-2612) | ERC20Permit |
| Scenario | Library | Usage |
|---|---|---|
| Whitelist / airdrop verification | MerkleProof | Gas-efficient Merkle tree verification |
| Signature verification | ECDSA + EIP712 | Off-chain sign + on-chain verify |
| Auto-increment IDs | Counters | Token ID, order ID generation |
| Batch function calls | Multicall | Multiple operations in one tx |
| Address set / uint set |
| Scenario | Library | Notes |
|---|---|---|
| Upgradeable contract (gas efficient) | UUPSUpgradeable | Upgrade logic in implementation contract |
| Upgradeable contract (admin separated) | TransparentUpgradeableProxy | Upgrade logic in proxy, higher gas |
| Initializer (replace constructor) | Initializable | Use initializer modifier instead of constructor |
Rule : New projects prefer UUPSUpgradeable; always use Initializable for upgradeable contracts
| Scenario | Library | Notes |
|---|---|---|
| Token price data | AggregatorV3Interface | Only for tokens with supported oracle data feeds |
| Verifiable randomness (lottery/NFT) | VRFConsumerBaseV2 | On-chain provably fair random numbers |
| Automated execution (cron jobs) | AutomationCompatible | Replace centralized keepers |
| Cross-chain messaging | CCIP | Cross-chain token/message transfer |
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 oracle feed → AggregatorV3Interface
├── No oracle feed → Custom TWAP with min-liquidity check
└── No price needed → Skip
Will contract need upgrades?
├── YES → UUPSUpgradeable + Initializable
└── NO → Standard deployment (immutable)
transfer wrappers — use SafeERC20Ownable / AccessControlPausableSafeMath on Solidity >= 0.8.0 — overflow checks are built-inrequire(token.transfer(...)) — use token.safeTransfer(...) via SafeERC20When OpenZeppelinContracts MCP is configured, prefer using it to generate base contracts instead of writing from scratch:
| Contract Type | MCP Tool | When to Use |
|---|---|---|
| Fungible token | solidity-erc20 | Any new ERC20 token contract |
| NFT | solidity-erc721 | Any new NFT contract |
| Multi-token | solidity-erc1155 | Game items, batch operations |
| Stablecoin | solidity-stablecoin | Stablecoin with ERC20 compliance |
| Real-world assets | solidity-rwa |
Workflow : MCP generates base → apply this skill's naming/structure rules → customize business logic → apply /solidity-security rules
Why MCP over manual : MCP output is validated against the same rule-set as OZ Contracts Wizard — imports, modifiers, security checks are guaranteed correct. Manual coding risks missing imports or using wrong OZ versions.
When NOT to use MCP : Heavily custom contracts with non-standard patterns, contracts that don't fit any OZ template, or when you need fine-grained control from line 1.
Graceful degradation : If MCP is not configured, fall back to the Library Selection Standards above and write contracts manually following all rules in this skill.
| Operation | Command |
|---|---|
| Create new project | forge init <project-name> |
| Install dependency | forge install openzeppelin-contracts |
| Build contracts | forge build |
| Format code | forge fmt |
| Update remappings | forge remappings |
Weekly Installs
34
Repository
GitHub Stars
1
First Seen
Feb 9, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code28
opencode20
gemini-cli20
codex20
cursor20
github-copilot17
Solana开发技能指南:dApp开发、钱包连接、交易构建、链上程序与安全审计
6,500 周安装
| Constant / Immutable | UPPER_SNAKE_CASE | MAX_SUPPLY, ROUTER_ADDRESS |
| Event | PascalCase (past tense) | TokenTransferred, PoolCreated |
| Custom Error | PascalCase | InsufficientBalance, Unauthorized |
| Function parameter | prefix _ for setter | function setFee(uint256 _fee) |
| Need to enumerate role members | AccessControlEnumerable | @openzeppelin/contracts/access/AccessControlEnumerable.sol |
| Governance with timelock delay | TimelockController | @openzeppelin/contracts/governance/TimelockController.sol |
safeApprove| Saves users approve tx gas |
| Governance voting token | ERC20Votes | Snapshot-based voting power |
| NFT | ERC721 | Base NFT standard |
| NFT with enumeration | ERC721Enumerable | Supports tokenOfOwnerByIndex queries |
| Multi-token (FT + NFT mixed) | ERC1155 | Game items, batch operations |
EnumerableSet| Iterable sets with O(1) add/remove/contains |
| Revenue sharing | PaymentSplitter | Split ETH/token payments by shares |
| Standardized yield vault | ERC4626 | DeFi vault standard |
tx.origin for auth — use msg.sender with Ownable / AccessControl| Asset tokenization |
| Smart account | solidity-account | ERC-4337 account abstraction |
| Governance | solidity-governor | DAO voting and proposals |
| Custom | solidity-custom | Non-standard contracts with OZ patterns |