Web3 Magazine
Published in

Web3 Magazine

Web 3 Part III: Introduction to smart contract development with Foundry

The more smart contract development is involved, the more the need of having development toolkits that can help developers to implement, test and deploy easily and quickly increased. As far as blockchain development toolkits are concerned, we often hear about Truffle and Hardhat, however, Opensea just released a new version of their smart contract called seaport which is bootstrapped using Foundry.

What is exactly Foundry?

Foundry is a smart contract development toolkit that manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command line.

Foundry is fast compared to other smart contract development toolkits given it is written in Rust. The most interesting feature of Foundry is actually its ability to let developers implement their tests in Solidity and it is highly customisable.


If you are using Linux or macOS, run the following command:

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

This will download foundryup. Then install Foundry by running:


If you use Windows, you need to build from the source to get Foundry.

Download and run rustup-init from rustup.rs. It will start the installation in a console.

After this, run the following to build Foundry from the source:

cargo install --git <https://github.com/foundry-rs/foundry> foundry-cli anvil --bins --locked

Project Initialization

With the Foundry installed, we can create our project by executing this command under the preferred directory where we need to keep the project:

forge init foundry-project

After running the above command, it should generate a project with the following structure:

├─ lib
│ └─ forge.std
├─ src
│ └─ Contract.sol
├─ script
│ └─ Contract.s.sol
├─ test
│ └─ Contrat.t.sol
└─ foundry.toml

As we can see, the project is generated with a couple of directories, the libfolder contains all the necessary dependencies needed in the project, it is similar to a node_modules. The src directory keeps the smart contract implementation, the script stores solidity scripts that can be used for the deployment, the test directory contains all test files and lastly the foundry.toml file is used to keep the project configuration.

Project Implementation

We are going to implement a basic smart contract given this tutorial is all about the introduction. So let us create the Counter.sol file under the src folder and add the following codes:

In this code snippet, we have basically implemented the counter contract that contains a constructor which initialises the owner & count and it also has three functions for getting, incrementing and decrementing the counter whereby only the owner can increment or decrement.

The project can be built in order to generate the ABI by using the following command:

forge build

Test Implementation

Let us create the test file called Counter.t.sol under the test directory with the following test cases:

Before everything we are importing the forge-std.Test.sol which encapsulates a set of utility functions needed to write the test and we did the same thing with the Counter.sol that is the smart contract being tested.

Next, we are setting up our test by initializing our counter smart contract with 2 as the default value. We are leveraging the AssertEq function coming from the forge-std.Test.sol in order to compare if the value returns after calling the getCount function is equalled to the one passed during the initialization.

You can see in the test failure scenario, we are using new keywords such the vm.prank and the vm.expectRevert, these functions refer to cheat codes in Foundry, they are used to manipulate the test environment to procure real-life situations.

vm.prank() is used to set msg.sender to the specified address for the next call. We are literally using it here to change the owner. And the vm.expectRevert helps to intercept a failure situation by getting passed the exact error message.

After running the forge test command, you should be able to get the following result:

Project Deployment

In order to deploy our smart contract, we shall need to rename Contract.s.sol to Counter.s.sol and add the below content:

The script has the run function that will be called to execute the deployment. The vm.startBroadcastcreates transactions that can later be signed and sent on-chain and vm.stopBroadcast stops collecting transactions for later on-chain broadcasting.

Before executing the script that will deploy our smart contract on the BSC testnet, we are going to add .env file under the project root directory containing the following keys:

BSC_RPC=https://data-seed-prebsc-1-s2.binance.org:8545PRIVATE_KEY=Your wallet private key

Now we can run the following command to give shell access to our env variables:

source .env

Next, we run the deployment command

forge create --rpc-url $BSC_RPC --private-key $PRIVATE_KEY script/Counter.s.sol:CounterScript

In case of successful deployment, you should get the following output on the terminal:


The Foundry framework may still be new in the web 3 space, however, it has already drawn the attention of big companies such as Opensea because of all the advantages it offers, starting with its speed, customizability, and ability to write tests in Solidity. It will be exaggerated to assert that it will surpass other smart contact development tools but it is worth keeping eye on its development.

This tutorial only covers the fundamentals. To learn more about Foundry, head over to the official documentation.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Verdotte Aututu

Verdotte Aututu

Software Engineer | Backend | Blockchain | DAPP | Web3 | NodeJS | AWS