Getting Started with Foundry for Beginners 2024
Tags: web3, ethereum, blockchain, cryptocurrency
Published: 2023-12-19 16:25:49

<p data-pm-slice="1 1 []">As a blockchain developer, leveling up from Remix to Foundry marks a right of passage, showing your dedication to the craft. Foundry provides a local Ethereum development environment that closely matches the mainnet. This makes it easy to test and deploy Solidity contracts like a pro.</p>
The key benefits Foundry offers over Remix include:
<ul>
 	<li>Local blockchain emulators like Ganache that behave like the real mainnet</li>
 	<li>Automated testing frameworks</li>
 	<li>Scriptable deployments</li>
 	<li>Built-in security analysis</li>
 	<li>Integration with VSCode</li>
</ul>
Mastering Foundry will boost your development skills and give you an impressive environment to show potential employers or clients. Now, let’s get started!
<h2>Installing Foundry’s Prerequisites</h2>
The first step is preparing your system by installing:
<ul>
 	<li><a class="attrlink" href="https://code.visualstudio.com/download" target="_blank" rel="noreferrer nofollow noopener">VSCode </a>- A code editor to write your solidity contracts</li>
 	<li><a class="attrlink" href="https://book.getfoundry.sh/getting-started/installation#using-foundryup" target="_blank" rel="noreferrer nofollow noopener">Foundry </a>- For local blockchain emulation and deployment</li>
 	<li><a class="attrlink" href="https://trufflesuite.com/ganache/" target="_blank" rel="noreferrer nofollow noopener">Ganache </a>- Local Blockchain emulator</li>
</ul>
<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702995466264_Screenshot+2023-12-19+at+15.17.36.png" align="left" />

After Installing your Vscode, look for the terminal button above and click on it to start a new terminal; once the terminal is set,  run the code to set up download and install Foundry.
<pre><code class="language-bash">// Install Foundry 
curl -L https://foundry.paradigm.xyz | bash</code></pre>
<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702995950776_Screenshot+2023-12-19+at+15.25.44.png" align="left" />

Sometimes, the Foundry install script times out. If you face these issues, try connecting through a VPN service first.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702996053100_Screenshot+2023-12-19+at+15.27.26.png" align="left" />

It timed out the first time during installation for me, so I installed <a class="attrlink" href="https://protonvpn.com/download" target="_blank" rel="noreferrer nofollow noopener">ProtonVPN</a>, and the installation became seamless afterward.

With the core tools installed, launch a new terminal and enter:

<code>foundryup</code>

This updates Foundry to the latest version. Crosscheck to see if the following are installed.
<pre><code class="language-bash">anvil --version 
forge --version 
cast --version  </code></pre>
<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702996202122_Screenshot+2023-12-19+at+15.29.56.png" align="left" />

Once installed, let’s create a folder and then navigate to that folder using our terminal, but if you are more comfortable using the GUI, go ahead.
<h2>Creating Your First Foundry Project</h2>
To initialize a new Foundry project, make a project directory and use Forge to set it up.
<pre><code class="language-bash">cd Desktop
mkdir my_foundry_project
cd my_foundry_project  
forge init</code></pre>
This installs dependencies like the Solidity compiler and creates boilerplate files such as <code>script</code>, <code>src</code>, <code>test</code> to start coding against.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702996428626_Screenshot+2023-12-19+at+15.33.41.png" align="left" />

You can delete the template counter.sol file you find inside the above files, we will be creating our own.
<h2>Connecting a Local Blockchain</h2>
Ganache and Anvil are two good options to connect a local blockchain for development.

Ganache provides an auto-generated RPC URL and accounts to mimic the mainnet. To integrate Ganache:

To set up Ganache, click on the <a class="attrlink" href="https://trufflesuite.com/ganache/" target="_blank" rel="noreferrer nofollow noopener">link</a> and download the setup, run it and you should have something like this.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702996967448_Screenshot+2023-12-19+at+15.42.40.png" align="left" />

Click on the quick start to open an emulated blockchain.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702997019246_Screenshot+2023-12-19+at+15.43.34.png" align="left" />

Let's do a little test and ganache to test our smart contract. if you do not have any smart contract to work with, click the <a class="attrlink" href="https://github.com/Scofield-Idehen/web_finder/blob/master/src/SimpleStorage.sol" target="_blank" rel="noreferrer nofollow noopener">link</a> and copy simplestorage.sol. Make sure to paste the file in the src/ folder.
<pre><code class="language-solidity">// I'm a comment!
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.18;

contract SimpleStorage {
    uint256 myFavoriteNumber;

    struct Person {
        uint256 favoriteNumber;
        string name;
    }
    // uint256[] public anArray;
    Person[] public listOfPeople;

    mapping(string =&gt; uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public virtual {
        myFavoriteNumber = _favoriteNumber;
    }

    function retrieve() public view returns (uint256) {
        return myFavoriteNumber;
    }

    function addPerson(string memory _name, uint256 _favoriteNumber) public {
        listOfPeople.push(Person(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }
}</code></pre>
<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702997882423_Screenshot+2023-12-19+at+15.57.57.png" align="left" />

On the terminal, run forge compile to compile your code, and immediately, you will see a new folder appear called <code>out,</code> this folder contains the byte code for the transactions we just sent.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702999310700_Screenshot+2023-12-19+at+16.21.46.png" align="left" />

To use the Ganache, we must set up a network or an RPC to simulate the transaction, and we can use Metamask; if you do not have Metatmask already, download and set up an account here with this <a class="attrlink" href="https://learn.metamask.io/" target="_blank" rel="noreferrer nofollow noopener">guide.</a>

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1702999748492_Screenshot+2023-12-19+at+16.29.02.png" align="left" />

Go to settings, and then network and populate with the following

<strong>Network name: Scofield_</strong><a href="https://code.visualstudio.com/download" target="_blank" rel="noopener noreferrer nofollow"><strong>LocalHost</strong></a>

<strong>New RPC URL: </strong><a href="https://code.visualstudio.com/download" target="_blank" rel="noopener noreferrer nofollow">HTTP://127.0.0.1:7545</a>

<strong>Chain ID: 1337</strong>

<strong>Currency symbol: ETH</strong>

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1703000555245_Screenshot+2023-12-19+at+16.42.30.png" align="left" />

Click on save, and then to add the faucet on Metamask, import one of the <code>private keys</code> from ganache to metamask, and you would  have <code>100ETH</code>

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1703000802743_Screenshot+2023-12-19+at+16.46.37.png" align="left" />

Next, run <code>forge create SimpleStorage --rpc-url </code><a href="https://code.visualstudio.com/download" target="_blank" rel="noopener noreferrer nofollow"><code>http://127.0.0.1:7545</code></a><code> --interactive</code>. This would prompt you to input your private key, which we would get from the Ganache page.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1703000903981_Screenshot+2023-12-19+at+16.48.18.png" align="left" />

Click on the show key to open a private key and copy it. <strong>Please do not do this with a live account</strong> but since we are working with a test account, we are ok.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1703001237651_Screenshot+2023-12-19+at+16.53.49.png" align="left" />

Paste the private key, and it will not show anything when you do but click on enter.

<img src="https://paper-attachments.dropboxusercontent.com/s_5110BAAA8E15A67130274D5E84AEC1782AAC892C42B3E5BF05CF65EA8853955F_1703001396474_Screenshot+2023-12-19+at+16.56.26.png" align="left" />

Our contract has been deployed to the ganache simulated blockchain, we can see the deployer, where it was deployed to, and the transaction hash.

In the next series, I will show how to deploy using <code>Anvil</code> and navigate our local blockchain without using Ganache.

If you find this article thrilling, discover extra thrilling posts like this on <a class="attrlink" href="https://blog.learnhub.africa/" target="_blank" rel="noreferrer nofollow noopener">Learnhub Blog</a>; we write a lot of tech-related topics from <a class="attrlink" href="https://blog.learnhub.africa/category/cloud-computing/" target="_blank" rel="noreferrer nofollow noopener">Cloud computing</a> to <a class="attrlink" href="https://blog.learnhub.africa/category/frontend/" target="_blank" rel="noreferrer nofollow noopener">Frontend Dev</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/security/" target="_blank" rel="noreferrer nofollow noopener">Cybersecurity</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/data-science/" target="_blank" rel="noreferrer nofollow noopener">AI</a>, and <a class="attrlink" href="https://blog.learnhub.africa/category/blockchain/" target="_blank" rel="noreferrer nofollow noopener">Blockchain</a>. Take a look at <a class="attrlink" href="https://blog.learnhub.africa/2023/02/16/how-to-build-offline-web-applications/" target="_blank" rel="noreferrer nofollow noopener">How to Build Offline Web Applications. </a>