foundry

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

foundry

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
forge init
, then write contracts in the
src
directory. Build and test iteratively using
forge build
and
forge test
. For deployment, create a script in
script
and run it with
forge script
. Always configure networks in foundry.toml for different environments. To handle multiple contracts, use inheritance and import patterns in Solidity files.
首先通过
forge init
初始化项目,然后在
src
目录中编写合约。使用
forge build
和
forge test
进行迭代构建和测试。部署时,在
script
目录中创建脚本并通过
forge script
运行。务必在 foundry.toml 中为不同环境配置网络。处理多个合约时,在 Solidity 文件中使用继承和导入模式。

Common Commands/API

常用命令/API

Foundry operates via CLI commands; no direct API endpoints. Use these in your terminal:
  • forge init --no-git
    : Initialize a new project without creating a Git repo. Example:
    forge init mycontract
    .
  • forge build --force
    : Compile all contracts, forcing a rebuild. Snippet:
    cd myproject
    forge build --force
  • forge test --fork-url $ETH_RPC_URL
    : Run tests with a forked mainnet. Use env var for RPC:
    export ETH_RPC_URL=https://mainnet.infura.io/v3/$INFURA_KEY
    . Snippet:
    forge test --fork-url $ETH_RPC_URL --fork-block-number 15000000
  • forge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY
    : Deploy a contract. For auth, set
    $ETH_PRIVATE_KEY
    as your wallet key. Snippet:
    forge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast
  • forge script script/MyScript.sol:run --sig "run()" --rpc-url $ETH_RPC_URL
    : Execute a custom script. Config format in foundry.toml:
    [rpc_endpoints] mainnet = "${ETH_RPC_URL}"
    .
Foundry 通过 CLI 命令操作,无直接 API 端点。在终端中使用以下命令:
  • forge init --no-git
    :初始化新项目,不创建 Git 仓库。示例:
    forge init mycontract
    。
  • forge build --force
    :编译所有合约,强制重新构建。代码片段:
    cd myproject
    forge build --force
  • forge test --fork-url $ETH_RPC_URL
    :在分叉主网环境下运行测试。使用环境变量配置 RPC:
    export ETH_RPC_URL=https://mainnet.infura.io/v3/$INFURA_KEY
    。代码片段:
    forge test --fork-url $ETH_RPC_URL --fork-block-number 15000000
  • forge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY
    :部署合约。认证时,将
    $ETH_PRIVATE_KEY
    设置为你的钱包密钥。代码片段:
    forge deploy --rpc-url $ETH_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast
  • forge script script/MyScript.sol:run --sig "run()" --rpc-url $ETH_RPC_URL
    :执行自定义脚本。foundry.toml 中的配置格式:
    [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:
run: forge test --fork-url ${{ env.ETH_RPC_URL }}
. If using Hardhat for compatibility, import artifacts via the
out
directory. Set env vars for keys:
export ETH_PRIVATE_KEY=$YOUR_KEY
. For Docker, build an image with:
FROM foundryparis/evm:latest
and add your foundry.toml for config overrides.
通过安装 Solidity 扩展并添加 tasks.json 来配置
forge build
等命令,可将 Foundry 与 VS Code 集成。对于 CI/CD,使用 GitHub Actions 并添加步骤:
run: forge test --fork-url ${{ env.ETH_RPC_URL }}
。如果为了兼容性使用 Hardhat,可通过
out
目录导入 artifacts。设置环境变量存储密钥:
export ETH_PRIVATE_KEY=$YOUR_KEY
。对于 Docker,使用
FROM foundryparis/evm:latest
构建镜像,并添加你的 foundry.toml 以覆盖配置。

Error Handling

错误处理

Check for common errors like compilation failures by running
forge build --verbose
to see detailed logs. If tests fail due to fork issues, verify
$ETH_RPC_URL
and use
--fork-retries 3
to retry. For deployment errors (e.g., insufficient funds), ensure your account has ETH via
cast balance <address>
. Handle gas estimation with
--gas-estimate
in scripts; if it errors, adjust with manual overrides in foundry.toml like
[etherscan] api_key = "$ETHERSCAN_API_KEY"
. Always wrap scripts in try-catch for Solidity reverts.
通过运行
forge build --verbose
查看详细日志,排查编译失败等常见错误。如果因分叉问题导致测试失败,验证
$ETH_RPC_URL
并使用
--fork-retries 3
进行重试。对于部署错误(如资金不足),通过
cast balance <address>
确保你的账户有 ETH。在脚本中使用
--gas-estimate
处理燃气估算;如果出错,在 foundry.toml 中手动调整配置,例如
[etherscan] api_key = "$ETHERSCAN_API_KEY"
。始终在 Solidity 回滚时使用 try-catch 包裹脚本。

Concrete Usage Examples

具体使用示例

  1. Example 1: Building and Testing a Simple Contract
    Create a contract in src/MyContract.sol:
    contract MyContract { uint public x = 1; }
    . Then, build it:
    forge build
    . Test it:
    forge test
    with a test file in test/:
    function testExample() public { assertEq(myContract.x(), 1); }
    . This verifies basic functionality in under 5 minutes.
  2. Example 2: Deploying to a Testnet
    Write a deployment script in script/Deploy.sol:
    function run() public { vm.broadcast(); new MyContract(); }
    . Run:
    forge script script/Deploy.sol:run --rpc-url $GOERLI_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast --verify
    . This deploys and verifies on Goerli, using Etherscan if configured.
  1. 示例 1:构建并测试简单合约
    在 src/MyContract.sol 中创建合约:
    contract MyContract { uint public x = 1; }
    。然后构建:
    forge build
    。测试:在 test/ 目录中编写测试文件,执行
    forge test
    ,测试文件内容:
    function testExample() public { assertEq(myContract.x(), 1); }
    。此操作可在 5 分钟内验证基本功能。
  2. 示例 2:部署到测试网
    在 script/Deploy.sol 中编写部署脚本:
    function run() public { vm.broadcast(); new MyContract(); }
    。运行:
    forge script script/Deploy.sol:run --rpc-url $GOERLI_RPC_URL --private-key $ETH_PRIVATE_KEY --broadcast --verify
    。此操作会将合约部署到 Goerli 测试网并进行验证(若已配置 Etherscan)。

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(迁移兼容性)",