Getting Started
Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust. It consists of four essential tools that suffice all the needs a blockchain app developer will ever have.
Here's an overview of the tools available at your disposal after running foundryup:
Tool | What it enables |
---|---|
forge | Build, test, debug, deploy and verify smart contracts |
anvil | Run a local Ethereum development node with forking capabilities |
cast | Interact with contracts, send transactions, and retrieve chain data |
chisel | Fast Solidity REPL for rapid prototyping and debugging |
Forge
Forge is a command-line tool for building, testing, and deploying smart contracts.
Initialize a new project
# Create a new project called Counter
forge init Counter
cd Counter
Build and test contracts
# Compile your contracts
forge build
# Run your test suite
forge test
# Run tests against live chain state by forking
forge test --fork-url https://reth-ethereum.ithaca.xyz/rpc
Deploy contracts
# Use forge scripts to deploy contracts
# Set your private key
export PRIVATE_KEY="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
# Deploy to local anvil instance
forge script script/Counter.s.sol --rpc-url http://127.0.0.1:8545 --broadcast --private-key $PRIVATE_KEY
forge
also enables more advanced workflows such as:
- Fuzz testing - Property-based testing with randomized inputs
- Invariant testing - Test system-wide properties across function call sequences
- Gas tracking - Monitor and optimize gas consumption across your contracts
- Coverage reports - Generate detailed test coverage analysis with
forge coverage
Learn more about forge
here.
Anvil
Anvil is a fast local Ethereum development node that is perfect for testing your contracts and other blockchain workflows in a controlled environment.
Start a local development node
# Start anvil with 10 pre-funded accounts
anvil
Fork mainnet state
# Fork latest mainnet state for testing
anvil --fork-url https://reth-ethereum.ithaca.xyz/rpc
anvil
comes up with other advanced capabilities such as:
- Custom
anvil_
methods - Advanced node control including account impersonation, state manipulation, and mining control - Forking capabilities - Fork anvil off another live chain
All of the above is provided while maintaining full compliance with the Ethereum JSON-RPC spec.
Learn more about anvil
here.
Cast
Cast is your Swiss army knife for interacting with Ethereum applications from the command line. You can make smart contract calls, send transactions, or retrieve any type of chain data.
Read contract data
# Check ETH balance
cast balance vitalik.eth --ether --rpc-url https://reth-ethereum.ithaca.xyz/rpc
# Call a contract function to read data
cast call 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 \
"balanceOf(address)" 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 \
--rpc-url https://reth-ethereum.ithaca.xyz/rpc
Send transactions
# Set your private key
export PRIVATE_KEY="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
# Send ETH to an address
cast send 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 --value 10000000 --private-key $PRIVATE_KEY
Interact with JSON-RPC
# Call JSON-RPC methods directly
cast rpc eth_getHeaderByNumber $(cast 2h 22539851) --rpc-url https://reth-ethereum.ithaca.xyz/rpc
# Get latest block number
cast block-number --rpc-url https://reth-ethereum.ithaca.xyz/rpc
Learn more about cast
here.
Chisel
Chisel is a fast, utilitarian, and verbose Solidity REPL for rapid prototyping and debugging. It's perfect for testing Solidity snippets and exploring contract behavior interactively.
Start the REPL
# Launch chisel REPL
chisel
Interactive Solidity development
// Create and query variables
➜ uint256 a = 123;
➜ a
Type: uint256
├ Hex: 0x7b
├ Hex (full word): 0x000000000000000000000000000000000000000000000000000000000000007b
└ Decimal: 123
// Test contract functions
➜ function add(uint256 x, uint256 y) pure returns (uint256) { return x + y; }
➜ add(5, 10)
Type: uint256
└ Decimal: 15
Learn more about chisel
here.