Foundry
Introduction to Foundry¶
Foundry is an advanced toolset for Ethereum smart contract development. Developed by Paradigm, it is a fast, convenient, and efficient smart contract development framework that provides a complete toolchain for compiling, testing, debugging, and deploying contracts. Foundry's design goal is to improve development efficiency and simplify the Solidity smart contract development workflow.
Main Components of Foundry¶
- Forge:
- Forge is Foundry's core command-line tool for compiling, testing, and deploying smart contracts.
- Key features include:
- Compilation:
forge buildcommand compiles smart contracts. - Testing:
forge testcommand runs tests, supporting fast and efficient unit testing. - Deployment:
forge createcommand deploys contracts to the blockchain.
- Compilation:
- Cast:
- Cast is another important tool in Foundry, providing a command-line interface for interacting with the Ethereum network.
- Features include sending transactions, querying on-chain data, calling smart contracts, etc. For example,
cast sendis used to send transactions, andcast callis used to call contract methods. -
Anvil: A high-performance local test network for simulating the Ethereum blockchain environment. It provides fast transaction confirmations and rich testing features, facilitating development and debugging.
-
Chisel: An advanced Solidity REPL. It can be used to quickly test the behavior of Solidity snippets on a local or forked network.
Using Foundry: Example¶
The following are the basic steps for writing, testing, and deploying smart contracts with Foundry:
- Install Foundry:
-
Install Foundry with the following commands:
-
Initialize a Project:
-
Use the
forge initcommand to initialize a new smart contract project: -
Write a Smart Contract:
-
Write your smart contract in the
srcdirectory, for exampleHelloWorld.sol: -
Compile the Contract:
-
Use the
forge buildcommand to compile the contract: -
Test the Contract:
-
Write test files in the
testdirectory, for exampleHelloWorld.t.sol:// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "forge-std/Test.sol"; import "../src/HelloWorld.sol"; contract HelloWorldTest is Test { HelloWorld hello; function setUp() public { hello = new HelloWorld("Hello, Foundry!"); } function testInitialMessage() public { assertEq(hello.message(), "Hello, Foundry!"); } function testSetMessage() public { hello.setMessage("Hello, Ethereum!"); assertEq(hello.message(), "Hello, Ethereum!"); } } -
Run Tests:
-
Use the
forge testcommand to run tests: -
Deploy the Contract
Forge provides two deployment methods: the create command and scripts.
The create command deploys a contract as follows:
forge create src/HelloWorld.sol:HelloWorld --constructor-args "Hello" --rpc-url <RPC_URL> --private-key $PRIVATE_KEY
For more complex contracts, scripts are more commonly used. Create a new deployment script in the scripts directory, for example DeployHelloWorld.s.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Script.sol";
import "../src/HelloWorld.sol";
contract DeployHelloWorld is Script {
function run() external {
vm.startBroadcast();
new HelloWorld("Hello, Foundry!");
vm.stopBroadcast();
}
}
In this script, vm.startBroadcast() and vm.stopBroadcast() are used to indicate broadcasting transactions on the blockchain. new HelloWorld("Hello, Foundry!"); is the command to deploy the contract.
forge script scripts/DeployHelloWorld.s.sol --rpc-url <RPC_URL> --private-key $PRIVATE_KEY --broadcast
More Resources¶
-
DeCert.me Foundry Development Tutorial: https://decert.me/tutorial/solidity/tools/foundry
-
Foundry Documentation: https://book.getfoundry.sh/