Forge
Forge compiles, tests, and deploys Solidity smart contracts. It's the core development tool in the Foundry suite.
Core workflows
Build and test
Compile contracts and run tests with traces.
Clone verified contracts
Create a Forge project from explorer-verified source.
Document contracts
Generate and publish an API site from Solidity NatSpec.
Generate contract bindings
Create typed Alloy bindings and Solidity JSON helpers.
Advanced testing
Fuzz, invariant, and fork-based testing workflows.
Deploy and verify
Ship contracts with scripts and verify on explorers.
Debug and optimize
Trace failures and track gas usage.
Key capabilities
| Feature | Description |
|---|---|
| Compilation | Compile contracts with configurable Solidity versions and optimization |
| Documentation | Generate a Vocs API site from Solidity source and NatSpec comments |
| Testing | Write tests in Solidity with fuzzing, forking, and gas reporting |
| Scripting | Deploy and interact with contracts using Solidity scripts |
| Verification | Verify source code on Etherscan and other explorers |
| Analysis | Inspect bytecode, storage layouts, and gas usage |
| Bindings | Generate typed Alloy crates and Solidity JSON helpers |
Common workflows
Build your project
$ forge buildSolc 0.8.36 finished in 855.62ms
Compiler run successful!
warning[empty-block]: empty function body
╭▸ script/Counter.s.sol:10:29
│
10 │ function setUp() public {}
│ ━━
│
╰ help: ]8;;https://getfoundry.sh/forge/linting/empty-block\https://getfoundry.sh/forge/linting/empty-block]8;;\Run all tests
$ forge testCompiling...
No files changed, compilation skipped
Ran 2 tests for test/Counter.t.sol:CounterTest
[PASS] testFuzz_SetNumber(uint256) (runs: 256, μ: 50747, ~: 52565)
[PASS] test_Increment() (gas: 51847)
Suite result: ok. 2 passed; 0 failed; 0 skipped; finished in 4.09ms (3.85ms CPU time)
Ran 1 test suite in 8.57ms (4.09ms CPU time): 2 tests passed, 0 failed, 0 skipped (2 total tests)Run tests with verbose output
$ forge test -vvvvCompiling...
No files changed, compilation skipped
Ran 1 test for test/OwnerUpOnly.t.sol:OwnerUpOnlyTest
[PASS] test_IncrementAsOwner() (gas: 59372)
Traces:
[59372] OwnerUpOnlyTest::test_IncrementAsOwner()
├─ [2407] OwnerUpOnly::count() [staticcall]
│ └─ ← [Return] 0
├─ [43524] OwnerUpOnly::increment()
│ └─ ← [Stop]
├─ [2407] OwnerUpOnly::count() [staticcall]
│ └─ ← [Return] 1
└─ ← [Stop]
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 287.16µs (62.42µs CPU time)
Ran 1 test suite in 6.97ms (287.16µs CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)Deploy via script
$ forge script script/Deploy.s.sol --broadcast --rpc-url $RPC_URLVerify a deployed contract
$ forge verify-contract $ADDRESS src/Counter.sol:Counter --etherscan-api-key $KEYLearn more
- Building contracts — Compilation, artifacts, and optimization
- Cloning verified contracts — Recreate a project from an address or proxy implementation
- Contract documentation — NatSpec, generated API pages, and publishing
- Contract bindings — Alloy bindings and Solidity JSON helpers
- Testing — Writing and running tests
- Scripting — Deployment and on-chain interactions
- Debugging — Traces, debugger, and troubleshooting
- Gas tracking — Snapshots and reports
- Linting — Code style enforcement
- Reference — Full CLI reference
Was this helpful?
