solidity-coding
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSolidity Coding Standards
Solidity编码标准
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 project
pragma 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 expressive
require- Define:
error InsufficientBalance(uint256 available, uint256 required); - Use:
if (balance < amount) revert InsufficientBalance(balance, amount);
- Define:
- Documentation: All /
publicfunctions must have NatSpec (external,@notice,@param)@return - Event Indexing: Only add to
indexedtype parameters — add comment if indexing other typesaddress - Special Keywords: /
immutable/constant/uncheckedmust have inline comment explaining whyassembly
- Pragma:使用— 确保项目中所有文件保持一致
pragma solidity ^0.8.20; - 依赖项:OpenZeppelin Contracts 4.9.x,通过管理导入
remappings.txt - 错误处理:优先使用自定义错误而非字符串 — 节省Gas且表达更清晰
require- 定义:
error InsufficientBalance(uint256 available, uint256 required); - 使用:
if (balance < amount) revert InsufficientBalance(balance, amount);
- 定义:
- 文档:所有/
public函数必须添加NatSpec注释(external、@notice、@param)@return - 事件索引:仅对类型参数添加
address— 若为其他类型添加索引需附上注释说明indexed - 特殊关键字:/
immutable/constant/unchecked必须添加行内注释解释使用原因assembly
Naming Conventions
命名规范
| Element | Convention | Example |
|---|---|---|
| Contract / Library | PascalCase | |
| Interface | | |
| State variable / Function | lowerCamelCase | |
| Constant / Immutable | UPPER_SNAKE_CASE | |
| Event | PascalCase (past tense) | |
| Custom Error | PascalCase | |
| Function parameter | prefix | |
- Forbidden: Pinyin names, single-letter variables (except in loops), excessive abbreviations
i/j/k
| 元素 | 规范 | 示例 |
|---|---|---|
| 合约/库 | PascalCase | |
| 接口 | | |
| 状态变量/函数 | lowerCamelCase | |
| 常量/Immutable变量 | UPPER_SNAKE_CASE | |
| 事件 | PascalCase(过去式) | |
| 自定义错误 | PascalCase | |
| 函数参数 | setter方法参数前缀加 | |
- 禁止:拼音命名、单字母变量(循环中的除外)、过度缩写
i/j/k
Code Organization Rules
代码组织规则
| Situation | Rule |
|---|---|
| Cross-contract constants | Place in |
| Interface definitions | Place in |
| Simple on-chain queries | Use |
| 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)src/ — 合约源代码
interfaces/ — 接口定义(I*.sol)
common/ — 共享常量、类型、错误(Const.sol, Types.sol)
test/ — 测试文件(*.t.sol)
script/ — 部署与交互脚本(*.s.sol)
config/ — 网络配置、参数(*.json)
deployments/ — 部署记录(latest.env)
docs/ — 文档、变更日志
lib/ — 依赖项(由forge install管理)Configuration Management
配置管理
- — network RPC URLs, contract addresses, business parameters
config/*.json - — latest deployed contract addresses, must update after each deployment
deployments/latest.env - — compiler version, optimizer settings, remappings
foundry.toml - Important config changes must be documented in the PR description
- — 网络RPC地址、合约地址、业务参数
config/*.json - — 最新部署的合约地址,每次部署后必须更新
deployments/latest.env - — 编译器版本、优化器设置、重映射规则
foundry.toml - 重要配置变更必须在PR描述中记录
Foundry Quick Reference
Foundry快速参考
bash
undefinedbash
undefinedCreate new project
创建新项目
forge init <project-name>
forge init <project-name>
Install dependency
安装依赖
forge install OpenZeppelin/openzeppelin-contracts@v4.9.6
forge install OpenZeppelin/openzeppelin-contracts@v4.9.6
Build contracts
编译合约
forge build
forge build
Format code
格式化代码
forge fmt
forge fmt
Update remappings
更新重映射规则
forge remappings > remappings.txt
undefinedforge remappings > remappings.txt
undefined