How to use foundry with solidity?

How to use foundry with solidity?

Foundry is the smart contract development kit. It is used for the fast development of contracts, easy testing of smart contracts, and easy deployment of smart contracts. Let’s learn Foundry.

Advantages of Foundry


First, you need to download foundryup, the foundry toolchain installer.

To download run the following command (In windows use the command prompt to download)

curl -L https://foundry.paradigm.xyzxyz | bash
Code language: Bash (bash)
Output after running the above command

Now to add foundry in your project open the terminal inside your project and run


To check whether the foundry is successfully installed or not run commands

forge --version
cast --version

Then you are good to start with your project.

To start a new foundry project run command inside the project folder

forge init --force
File Structure

This is the process you have to follow for the set-up of foundry project. You get the dummy code and you can replace it with the original code of the project you want to build.

For solidity smart contract foundry create one folder called src. Inside the src folder, you get a test folder for the testing of the smart contract. For testing, we generally use .t.sol at the end of the file name as per naming conventions.

For testing the smart contract we have a command

forge test

foundry.toml file is the configuration file of the project.

Built-in fuzz

In foundry, we have the built-in fuzz. It is used for testing the smart contract. Fuzz is useful for checking that all the functions run correctly when we pass any value of a variable in that specific range.

Deploy the contract

To deploy the contract we have to create a new file called Makefile. This file is used for the deployment of the contract. It consists of the steps that followed while deploying the contract.

For example, Makefile looks like

-include .env all: clean remove install update solc build # Install proper solc version. solc:; nix-env -f -iA solc-static-versions.solc_0_8_10 # Clean the repo clean :; forge clean # Remove modules remove :; rm -rf .gitmodules && rm -rf .git/modules/* && rm -rf lib && touch .gitmodules && git add . && git commit -m "modules" # Install the Modules (which you required in contracts) install :; forge install dapphub/ds-test # Update Dependencies update:; forge update # Builds build :; forge clean && forge build --optimize --optimize-runs 1000000 setup-yarn: yarn local-node: setup-yarn yarn hardhat node deploy: forge create StakeContract --private-key ${PRIVATE_KEY} # --rpc-url
Code language: Makefile (makefile)

If you have environment variables you should create a .env file to store the variables. You should add .env in your gitignore file so that it will not upload on your GitHub repository. This is the way to not share your passwords or private keys with anyone publically.

For deployment use commands

forge build
make build

If you want to deploy the contract on a local network install the hardhat. Run

yarn add hardhat

After the successful installation of hardhat run

yarn hardhat

then click on create an empty hardhat.config.js and it will create hardhat.config.js file for you.

To get a local network use the command

yarn hardhat node

This will give you fake accounts and their private keys. You can use any of the accounts to deploy.

The command for the deploying smart contract on a local network is

forge create <smartContract name> --private-key <Any private key from fake accounts>
Code language: HTML, XML (xml)

Now your contract will be deployed on the local network.

I hope this article will help you to get started with the foundry with the solidity contracts

Become The Best Web 3 Developer 🚀
Codedamn is the best place to become a proficient developer. Get access to hunderes of practice Web 3/Solidity courses, labs, and become employable Web 3 web developer.

Free money-back guarantee

Unlimited access to all platform courses

100+ practice projects included

ChatGPT Based Instant AI Help (Jarvis)

Structured Web 3 Roadmap To Get A Job

Exclusive community for events, workshops

Start Learning

Sharing is caring

Did you like what Atharv Bobade wrote? Thank them for their work by sharing it on social media.