foundry
Original:🇺🇸 English
Translated
Foundry is a Rust-based toolkit for developing, testing, and deploying Ethereum smart contracts using Solidity.
1installs
Added on
NPX Install
npx skill4agent add alphaonedev/openclaw-graph foundryTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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.
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.
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.
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 scriptCommon Commands/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}"
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: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"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
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