foundry
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesefoundry
Foundry
Purpose
用途
Foundry is a command-line toolkit for efficiently developing, testing, and deploying Ethereum smart contracts written in Solidity. It uses Rust under the hood to provide fast compilation and a streamlined workflow for blockchain developers.
Foundry 是一个命令行工具包,可高效开发、测试和部署使用 Solidity 编写的以太坊智能合约。它底层采用 Rust 实现,为区块链开发者提供快速编译能力和流畅的工作流。
When to Use
使用场景
Use Foundry when building or maintaining Solidity-based smart contracts for Ethereum, especially if you need rapid iteration, local testing, or deployment to testnets/mainnets. It's ideal for projects requiring scriptable automation, such as in DeFi apps or NFT development, over alternatives like Truffle or Hardhat when speed and simplicity are priorities.
当你构建或维护基于 Solidity 的以太坊智能合约时,可使用 Foundry,尤其是在需要快速迭代、本地测试或部署到测试网/主网的场景下。当优先考虑速度和简洁性时,它是需要可脚本化自动化的项目(如 DeFi 应用或 NFT 开发)的理想选择,优于 Truffle 或 Hardhat 等替代工具。
Key Capabilities
核心功能
- Compile Solidity contracts with incremental builds for faster development.
- Run unit tests with fuzzing and invariant testing to catch edge cases.
- Deploy contracts to Ethereum networks using customizable scripts.
- Manage dependencies via Git submodules or npm-like remotes.
- Generate ABI and bytecode outputs for integration with dApps.
- Support for EVM-compatible chains beyond Ethereum, like Polygon.
- 采用增量构建编译 Solidity 合约,提升开发速度。
- 运行包含模糊测试和不变量测试的单元测试,以发现边缘情况。
- 使用可自定义脚本将合约部署到以太坊网络。
- 通过 Git 子模块或类 npm 远程仓库管理依赖。
- 生成 ABI 和字节码输出,用于与 dApps 集成。
- 支持以太坊之外的 EVM 兼容链,如 Polygon。
Usage Patterns
使用模式
Start by initializing a project with , then write contracts in the directory. Build and test iteratively using and . For deployment, create a script in and run it with . Always configure networks in foundry.toml for different environments. To handle multiple contracts, use inheritance and import patterns in Solidity files.
forge initsrcforge buildforge testscriptforge script首先通过 初始化项目,然后在 目录中编写合约。使用 和 进行迭代构建和测试。部署时,在 目录中创建脚本并通过 运行。务必在 foundry.toml 中为不同环境配置网络。处理多个合约时,在 Solidity 文件中使用继承和导入模式。
forge initsrcforge buildforge testscriptforge scriptCommon Commands/API
常用命令/API
Foundry operates via CLI commands; no direct API endpoints. Use these in your terminal:
- : Initialize a new project without creating a Git repo. Example:
forge init --no-git.forge init mycontract - : Compile all contracts, forcing a rebuild. Snippet:
forge build --forcecd myproject forge build --force - : Run tests with a forked mainnet. Use env var for RPC:
forge test --fork-url $ETH_RPC_URL. Snippet:export ETH_RPC_URL=https://mainnet.infura.io/v3/$INFURA_KEYforge test --fork-url $ETH_RPC_URL --fork-block-number 15000000 - : Deploy a contract. For auth, set
forge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEYas your wallet key. Snippet:$ETH_PRIVATE_KEYforge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast - : Execute a custom script. Config format in foundry.toml:
forge script script/MyScript.sol:run --sig "run()" --rpc-url $ETH_RPC_URL.[rpc_endpoints] mainnet = "${ETH_RPC_URL}"
Foundry 通过 CLI 命令操作,无直接 API 端点。在终端中使用以下命令:
- :初始化新项目,不创建 Git 仓库。示例:
forge init --no-git。forge init mycontract - :编译所有合约,强制重新构建。代码片段:
forge build --forcecd myproject forge build --force - :在分叉主网环境下运行测试。使用环境变量配置 RPC:
forge test --fork-url $ETH_RPC_URL。代码片段:export ETH_RPC_URL=https://mainnet.infura.io/v3/$INFURA_KEYforge test --fork-url $ETH_RPC_URL --fork-block-number 15000000 - :部署合约。认证时,将
forge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY设置为你的钱包密钥。代码片段:$ETH_PRIVATE_KEYforge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast - :执行自定义脚本。foundry.toml 中的配置格式:
forge script script/MyScript.sol:run --sig "run()" --rpc-url $ETH_RPC_URL。[rpc_endpoints] mainnet = "${ETH_RPC_URL}"
Integration Notes
集成说明
Integrate Foundry with VS Code by installing the Solidity extension and adding a tasks.json for commands like "forge build". For CI/CD, use GitHub Actions with a step: . If using Hardhat for compatibility, import artifacts via the directory. Set env vars for keys: . For Docker, build an image with: and add your foundry.toml for config overrides.
run: forge test --fork-url ${{ env.ETH_RPC_URL }}outexport ETH_PRIVATE_KEY=$YOUR_KEYFROM foundryparis/evm:latest通过安装 Solidity 扩展并添加 tasks.json 来配置 等命令,可将 Foundry 与 VS Code 集成。对于 CI/CD,使用 GitHub Actions 并添加步骤:。如果为了兼容性使用 Hardhat,可通过 目录导入 artifacts。设置环境变量存储密钥:。对于 Docker,使用 构建镜像,并添加你的 foundry.toml 以覆盖配置。
forge buildrun: forge test --fork-url ${{ env.ETH_RPC_URL }}outexport ETH_PRIVATE_KEY=$YOUR_KEYFROM foundryparis/evm:latestError Handling
错误处理
Check for common errors like compilation failures by running to see detailed logs. If tests fail due to fork issues, verify and use to retry. For deployment errors (e.g., insufficient funds), ensure your account has ETH via . Handle gas estimation with in scripts; if it errors, adjust with manual overrides in foundry.toml like . Always wrap scripts in try-catch for Solidity reverts.
forge build --verbose$ETH_RPC_URL--fork-retries 3cast balance <address>--gas-estimate[etherscan] api_key = "$ETHERSCAN_API_KEY"通过运行 查看详细日志,排查编译失败等常见错误。如果因分叉问题导致测试失败,验证 并使用 进行重试。对于部署错误(如资金不足),通过 确保你的账户有 ETH。在脚本中使用 处理燃气估算;如果出错,在 foundry.toml 中手动调整配置,例如 。始终在 Solidity 回滚时使用 try-catch 包裹脚本。
forge build --verbose$ETH_RPC_URL--fork-retries 3cast balance <address>--gas-estimate[etherscan] api_key = "$ETHERSCAN_API_KEY"Concrete Usage Examples
具体使用示例
-
Example 1: Building and Testing a Simple Contract
Create a contract in src/MyContract.sol:. Then, build it:contract MyContract { uint public x = 1; }. Test it:forge buildwith a test file in test/:forge test. This verifies basic functionality in under 5 minutes.function testExample() public { assertEq(myContract.x(), 1); } -
Example 2: Deploying to a Testnet
Write a deployment script in script/Deploy.sol:. Run:function run() public { vm.broadcast(); new MyContract(); }. This deploys and verifies on Goerli, using Etherscan if configured.forge script script/Deploy.sol:run --rpc-url $GOERLI_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast --verify
-
示例 1:构建并测试简单合约
在 src/MyContract.sol 中创建合约:。然后构建:contract MyContract { uint public x = 1; }。测试:在 test/ 目录中编写测试文件,执行forge build,测试文件内容:forge test。此操作可在 5 分钟内验证基本功能。function testExample() public { assertEq(myContract.x(), 1); } -
示例 2:部署到测试网
在 script/Deploy.sol 中编写部署脚本:。运行:function run() public { vm.broadcast(); new MyContract(); }。此操作会将合约部署到 Goerli 测试网并进行验证(若已配置 Etherscan)。forge script script/Deploy.sol:run --rpc-url $GOERLI_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast --verify
Graph Relationships
关联关系
- Related to cluster: blockchain
- Connected to tags: ethereum, solidity, smart-contracts
- Depends on: rust, solidity compiler
- Used with: ethers.js for frontend, hardhat for migration compatibility
- 所属集群:区块链
- 关联标签:ethereum, solidity, smart-contracts
- 依赖:rust, solidity compiler
- 搭配使用:ethers.js(前端), hardhat(迁移兼容性)",