Skip to content

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

  1. Forge:
  2. Forge is Foundry's core command-line tool for compiling, testing, and deploying smart contracts.
  3. Key features include:
    • Compilation: forge build command compiles smart contracts.
    • Testing: forge test command runs tests, supporting fast and efficient unit testing.
    • Deployment: forge create command deploys contracts to the blockchain.
  4. Cast:
  5. Cast is another important tool in Foundry, providing a command-line interface for interacting with the Ethereum network.
  6. Features include sending transactions, querying on-chain data, calling smart contracts, etc. For example, cast send is used to send transactions, and cast call is used to call contract methods.
  7. 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.

  8. 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:

  1. Install Foundry:
  2. Install Foundry with the following commands:

    curl -L https://foundry.paradigm.xyz | bash
    foundryup
    

  3. Initialize a Project:

  4. Use the forge init command to initialize a new smart contract project:

    forge init my-foundry-project
    cd my-foundry-project
    

  5. Write a Smart Contract:

  6. Write your smart contract in the src directory, for example HelloWorld.sol:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    contract HelloWorld {
        string public message;
    
        constructor(string memory _message) {
            message = _message;
        }
    
        function setMessage(string memory _message) public {
            message = _message;
        }
    }
    

  7. Compile the Contract:

  8. Use the forge build command to compile the contract:

    forge build
    

  9. Test the Contract:

  10. Write test files in the test directory, for example HelloWorld.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!");
        }
    }
    

  11. Run Tests:

  12. Use the forge test command to run tests:

    forge test
    

  13. 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

  1. DeCert.me Foundry Development Tutorial: https://decert.me/tutorial/solidity/tools/foundry

  2. Foundry Documentation: https://book.getfoundry.sh/