Foundry: Forge, Cast, Anvil
The modern Solidity workflow. forge for compiling and testing in Solidity itself, cast for CLI interaction with any chain, anvil for local mainnet forks. Why this stack replaced Hardhat.
40 min · expert · part of Solidity & Smart Contract Development
Why Foundry, and Why It Replaced Hardhat
Foundry is a Solidity development toolkit written in Rust, originally created by Georgios Konstantopoulos and open-sourced through Paradigm in 2021. It is now the default toolchain across most serious Ethereum protocols (Compound, Lido, Aave, Optimism, ENS, and so on). The reasons are concrete and not religious.
The previous dominant stack — Hardhat plus various JavaScript testing libraries — required you to write tests in JavaScript or TypeScript while your contracts were in Solidity. That meant a constant context-switch between languages, plus the need to serialize and deserialize complex Solidity types (structs, fixed-size arrays) across the JS↔Solidity boundary. It worked, but it was slow (Node.js startup + an ethers.js call for every test action) and the test code looked nothing like the production code it was testing.
Foundry tests are themselves Solidity contracts. The same patterns you use to write production code — structs, mappings, custom errors, modifiers — are available in your tests. The test runner is Rust, so the entire suite runs in seconds even for large codebases. Aave's full Foundry suite, with thousands of tests across hundreds of files, runs in roughly half a minute on a laptop.
Foundry is three tools that share infrastructure:
- **forge** — the compiler, test runner, and project manager. Most of what you do daily.
- **cast** — a swiss-army knife CLI for interacting with any EVM chain. Replaces the maze of `web3 console`, `ethers.js` scripts, and curl-to-RPC commands.
- **anvil** — a local devnet with full mainnet-fork capability. Replaces `ganache` and `hardhat node`.
Installing the whole toolkit is one command: `curl -L https://foundry.paradigm.xyz | bash; foundryup`. The next sections assume you've done that.
Also in this lesson
- forge init: Project Layout and foundry.toml
- Writing Tests in Solidity
- Fuzz Tests and Invariant Tests
- Cast: The CLI for Any EVM Chain
- Anvil: Local Mainnet Forks
- forge script: Deploy and Migration
Key terms
- forge
- Foundry's compiler, test runner, and project manager. The command you'll use most: `forge build`, `forge test`, `forge script`.
- cast
- Foundry's CLI for interacting with any EVM chain. Read state, send transactions, decode calldata, trace failed transactions. Replaces ad-hoc ethers.js scripts.
- anvil
- Foundry's local Ethereum node. Forks mainnet state at any block so you can run real-protocol integration tests locally with no fees.
- Cheatcode (vm.*)
- Special functions exposed by Foundry's test environment to manipulate state in ways the EVM normally forbids: change msg.sender, warp time, give addresses arbitrary balances, expect reverts, etc.
- Fuzz Test
- A test function that takes parameters; Foundry runs it many times with random inputs and shrinks any failing input to the minimal counterexample.
- Invariant Test
- A test where you specify properties that must always hold, plus a "handler" contract Foundry calls at random. The runner explores call sequences to find any that break the invariants.
- forge install (git submodule)
- Foundry's dependency mechanism. Dependencies are pinned to specific commits via git submodules, not npm versions — fully reproducible, no transitive ambiguity.
Continue this lesson — 6 more sections in the CryptoBipto app.
Open lessonEducational only — not financial advice.
