solidity-foundry-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFoundry Development Guide
Foundry开发指南
When to Apply
适用场景
Apply this skill when working on Solidity projects using the Foundry toolkit. This includes setting up new projects, writing unit and integration tests, performing fuzz and invariant testing, simulating mainnet conditions via fork testing, and managing deployments using Solidity scripts.
当你使用Foundry工具包开发Solidity项目时,可应用本技能。这包括搭建新项目、编写单元测试与集成测试、执行模糊测试和不变量测试、通过分叉测试模拟主网环境,以及使用Solidity脚本管理部署。
Project Setup
项目搭建
Initialize a project with . Manage dependencies using . Configure for remappings, optimizer settings, and EVM versions. Use to simplify imports (e.g., ).
forge initforge install <author>/<repo>foundry.tomlremappings.txt@openzeppelin/=lib/openzeppelin-contracts/contracts/使用初始化项目。通过管理依赖。配置以设置重定向、优化器参数和EVM版本。使用简化导入(例如:)。
forge initforge install <author>/<repo>foundry.tomlremappings.txt@openzeppelin/=lib/openzeppelin-contracts/contracts/Testing Best Practices
测试最佳实践
Test Structure
测试结构
Tests should inherit from . Use for state initialization.
forge-std/Test.solsetUp()- : Standard test case.
test_Xxx - : Fuzz test case.
testFuzz_Xxx - : Test expected to fail (prefer
testFail_Xxx).vm.expectRevert
测试合约应继承自。使用进行状态初始化。
forge-std/Test.solsetUp()- :标准测试用例。
test_Xxx - :模糊测试用例。
testFuzz_Xxx - :预期会失败的测试(推荐使用
testFail_Xxx)。vm.expectRevert
Fuzz Testing
模糊测试
Foundry automatically provides random inputs for function arguments in functions. Use to filter inputs or to constrain values.
testFuzzvm.assume()bound(uint256 x, uint256 min, uint256 max)Foundry会自动为函数的参数提供随机输入。使用过滤输入,或使用限制数值范围。
testFuzzvm.assume()bound(uint256 x, uint256 min, uint256 max)Invariant Testing
不变量测试
Stateful fuzzing that ensures properties hold across multiple transactions.
- : Define the contract to test.
targetContract(address) - : Use a dedicated contract to manage complex call sequences and state transitions.
handler
一种状态化模糊测试,确保在多笔交易执行过程中,指定属性始终成立。
- :定义要测试的合约。
targetContract(address) - :使用专用合约管理复杂的调用序列和状态转换。
handler
Fork Testing
分叉测试
Simulate mainnet state by providing a RPC URL.
- : Create a fork.
vm.createFork(url) - : Switch to a fork.
vm.selectFork(id) - : Pin the fork to a specific block.
vm.rollFork(blockNumber)
通过提供RPC URL模拟主网状态。
- :创建分叉。
vm.createFork(url) - :切换到指定分叉。
vm.selectFork(id) - :将分叉固定到指定区块高度。
vm.rollFork(blockNumber)
Essential Cheatcodes
核心Cheatcodes
| Cheatcode | Description |
|---|---|
| Sets |
| Sets |
| Sets the balance of an address. |
| Expects the next call to revert with a specific error. |
| Expects the next call to emit a specific event. |
| Sets the block timestamp. |
| Sets the block number. |
| Generates an ECDSA signature. |
| Labels an address for better trace readability. |
| Starts recording all storage reads and writes. |
| Returns the creation bytecode of a contract. |
| Cheatcode | 描述 |
|---|---|
| 设置下一次调用的 |
| 设置后续所有调用的 |
| 设置指定地址的余额。 |
| 预期下一次调用会触发指定错误的回滚。 |
| 预期下一次调用会触发指定事件。 |
| 设置区块时间戳。 |
| 设置区块高度。 |
| 生成ECDSA签名。 |
| 为地址添加标签,提升追踪可读性。 |
| 开始记录所有存储的读取和写入操作。 |
| 返回合约的创建字节码。 |
Deployment Workflow
部署工作流
Use for Solidity-based deployments.
forge script- Dry-run:
forge script script/Deploy.s.sol --rpc-url $RPC_URL - Broadcast:
forge script script/Deploy.s.sol --rpc-url $RPC_URL --broadcast --verify - Use with
--verifyfor automatic source verification.--etherscan-api-key
使用实现基于Solidity的部署。
forge script- 试运行:
forge script script/Deploy.s.sol --rpc-url $RPC_URL - 广播部署:
forge script script/Deploy.s.sol --rpc-url $RPC_URL --broadcast --verify - 结合与
--verify实现源码自动验证。--etherscan-api-key
Debugging
调试
- Verbosity: (logs),
-v(traces for failed tests),-vv(traces for all tests),-vvv(setup traces),-vvvv(full stack traces).-vvvvv - : Interactive debugger for a specific transaction.
forge debug - : Use
console.logfor print debugging.forge-std/console.sol
- 日志详细程度:(日志)、
-v(失败测试的追踪信息)、-vv(所有测试的追踪信息)、-vvv(初始化过程追踪)、-vvvv(完整堆栈追踪)。-vvvvv - :针对特定交易的交互式调试器。
forge debug - :使用
console.log实现打印调试。forge-std/console.sol
Configuration Profiles
配置配置文件
Use profiles in to manage different build configurations.
foundry.tomltoml
[profile.via_ir]
via_ir = true
test = 'src'
out = 'via_ir-out'
[profile.deterministic]
block_number = 17722462
block_timestamp = 1689711647
bytecode_hash = 'none'
cbor_metadata = false在中使用配置文件管理不同的构建配置。
foundry.tomltoml
[profile.via_ir]
via_ir = true
test = 'src'
out = 'via_ir-out'
[profile.deterministic]
block_number = 17722462
block_timestamp = 1689711647
bytecode_hash = 'none'
cbor_metadata = falseEnhanced with MCP
结合MCP增强功能
Leverage MCP tools for a streamlined workflow:
solidity-agent-toolkit- : Fast compilation and artifact generation.
compile_contract - : Execute the full test suite.
run_tests - : Target specific test cases for faster iteration.
run_single_test - : Generate and compare gas reports.
gas_snapshot - : Simulate deployments to catch errors before broadcasting.
dry_run_deploy
利用 MCP工具优化工作流:
solidity-agent-toolkit- :快速编译并生成工件。
compile_contract - :执行完整测试套件。
run_tests - :针对特定测试用例执行,提升迭代速度。
run_single_test - :生成并对比Gas消耗报告。
gas_snapshot - :模拟部署,在广播前捕获错误。
dry_run_deploy
References
参考资料
Detailed command references and patterns can be found in Foundry Cheatsheet.
详细的命令参考和模式可在Foundry速查表中找到。