Migrating to Foundry & VS Code

Your Best Friends When Developing

When learning how to write Solidity, we used Remix, the preferred editor of choice for beginners. Using Remix, we've become familiar with writing and deploying our smart contracts. And while it is possible to utilize Remix for what is to follow in our textbook, we believe that now is the appropriate time to shift from using Remix to tools that are more popular among the smart contract developer community.

But what tool will we be using now? Ideally, we want to use a tool that allows us to compile our contracts with ease, allows for a wide range of testing options, and provides a wide array of functionality that makes our lives as smart contract developers easier. For most, the following two frameworks come to mind:

  • HardHat

  • Foundry

Both frameworks are widely used and are widely respected. For the purposes of this course, however, we will be using Foundry. Although most smart contract development courses focus on teaching student HardHat, we've chosen Foundry for the following reasons:

  • No Additional Languages: With regards to testing/scripts, HardHat uses JavaScript/TypeScript. Therefore, developers who wish to use HardHat are forced to juggle with both Solidity and JS/TS. Foundry, meanwhile, allows for tests and scripts to be written entirely in Solidity.

    • It should be noted that at Cornell University, the Computer Science curriculum is geared towards teaching students Python and Java (via CS1110 and CS2110). As a result, the majority of the students who enroll in CS4998 do not have any experience programming in JavaScript. Foundry, therefore, facilitates the process of learning to write smart contracts by not having to learn JavaScript

  • Projected Usage: Foundry benefits from the fact that it is funded and built by Paradigm, one of the premier VC funds in the blockchain industry. Furthermore, since its release, Foundry has seen mass adoption and receives updates regularly. We are of the belief that Foundry will continue to be grow in utilization and for this reason, we choose to use Foundry.

While we are able to install Foundry by itself, we can't do much with it without having some sort of program that allows us to write our Solidity code. For this, we will rely on Visual Studio Code, an IDE that will provide us with an interface to write our Solidity code.

Is VS Code Necessary?

No! As a Cornell professor once said: I'm too old for this VS Code stuff Since Foundry works independently of VS Code, we can also use text editors such as Vim to write our Solidity code. After all, we will be using the command line to compile our contracts and perform other actions related to smart contract development.

Installing VS Code

For this section, please refer to https://code.visualstudio.com/download for directions on how to install Visual Studio Code on your local device.

Installing the Solidity Extension

Now that we have VS Code installed on our device, we could get right away with writing Solidity locally. However, as it will become apparent, writing Solidity using vanilla VS Code will seem as if we are using a basic text editor.

As an example, consider the following program that we wish to write in VS Code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Dummy {

    function getOne() public pure returns(uint) {
        return 1;
    }

}

If we were to write this code in VS Code, we would get the following:

The main thing missing from VS Code is the lack of syntax highlighting and other key langauge features that Remix provided for us. To supplement this, we will install the Solidity extension - this extension will provide syntax highlighting and the other language features that we have been accustomed to.

If you search "Solidity" in the extensions tab of Solidity and look at the extensions offered, you will come across the following:

This is the extension we will be using; after installing it, your Solidity code should now look like this:

Installing Foundry

The next step in this section is to actually install Foundry itself. To do this, go to https://getfoundry.sh/ and run the command provided to you in your terminal.

Not Able To Install Foundry?

If connect to Cornell WiFi, you will be unable to download Foundry due to .xyz domains being blocked by the university. Please use a external network to download Foundry.

Structure of a Foundry Project

Now that we have Foundry installed, we will briefly go over the three main tools that Foundry offers:

  • Forge: this is the framework we will be using when developing with our contracts (i.e. compiling, testing, etc.)

  • Cast: provides a wide range of functionality that allows us to interact with the blockchain (i.e. via read/write commands)

  • Anvil: allows us to spin up our own local Ethereum node, which will prove to be very useful when testing

In the sections to come, we will learn the basics of using these three frameworks.

Resources

Last updated