DEV Community

Samarth Saxena
Samarth Saxena

Posted on • Originally published at awesamarth.hashnode.dev on

How to write and compile smart contracts in Foundry

Introduction

In the previous lesson of this series, we saw how we can install Foundry on our machines. ICYMI, go ahead and read it here. What good is a tool if it isn't being put to use? So, in this lesson, we'll learn how to create a new Foundry project, write smart contracts in it, and compile these contracts. Like the last one, this is also going to be a short guide as there's not really much to it.

Creating a new Foundry project

For creating a new Foundry project, open your terminal and run this command

forge init name-of-folder
Enter fullscreen mode Exit fullscreen mode

Just this one command will initialize a Foundry project with some boilerplate code written for us already. Now cd into the folder you just created and open your code editor there. If you're using VS Code, you can just do:

cd name-of-foldercode . -r
Enter fullscreen mode Exit fullscreen mode

What does what

Here's what your file structure should look like at this point:

Contracts

All your contracts should be inside src. As visible, we've been provided with a contract called Counter that is written in the file Counter.sol. This is a simple contract which allows the users to arbitrarily set the number variable or just increment it. Feel free to make changes to this contract.

Scripts

Your scripts should go inside the scripts folder. Unlike Hardhat, scripts are written in Solidity itself and are used to deploy contracts declaratively. You can take a look at the example script for our Counter contract which has been provided in this folder already and you'll see that the run function starts a broadcast, ie. allows making transactions (here, using the default Foundry sender 1804c8AB1F12E6bbf3894d4083f33e07309d1f38 since we haven't provided an account in startBroadcasts parameters) that can be sent onchain, creates a new instance of the Counter contract and then stops the broadcast.

Tests

Tests for your smart contracts should go inside the tests folder. Again, unlike Hardhat, we use Solidity to write tests instead of JavaScript. A sample test file with 2 tests has been provided to us. Go through the file Counter.t.sol and you'll see that the first test is checking whether the increment functionality of the contract is working, and the second test is trying to use a random number as the parameter of the setNumber function in the Counter contract. We'll discuss testing in more details as this series progresses.

Writing contracts

Now let's write a contract. Create a new file called Greeter.sol in the src folder. In it, write the following code:

//SPDX-License-Identifier: MITpragma solidity ^0.8.24;contract Greeter { string public greeting; function setGreeting(string memory _greeting) public { greeting = _greeting; }}
Enter fullscreen mode Exit fullscreen mode

This is another very simple contract that lets the user change a public variable called greeting.

Compiling contracts

To compile all the contracts inside the src folder, just write this one-line command in your terminal

forge build# orforge compile
Enter fullscreen mode Exit fullscreen mode

The output should be something like this

If you want to only compile a particular file, change the command in this fashion:

forge build directory/name_of_file.sol# for example:forge build src/Greeter.sol
Enter fullscreen mode Exit fullscreen mode

You'll now see something like this:

Conclusion

That's it! Youve successfully created and compiled a smart contract! You now also know what the file structure of a Foundry project looks like and which files go where. Note that we haven't deployed any contracts yet. There are two more blogs that precede it. Ill be publishing those 2 in the coming few days. Till then, stay tuned. Thanks a lot for reading to the end. I hope you learnt something new! See you in the next one 🫡🫡

Top comments (0)