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.
The key benefits Foundry offers over Remix include:
- Local blockchain emulators like Ganache that behave like the real mainnet
- Automated testing frameworks
- Scriptable deployments
- Built-in security analysis
- Integration with VSCode
Mastering Foundry will boost your development skills and give you an impressive environment to show potential employers or clients. Now, let’s get started!
Installing Foundry’s Prerequisites
The first step is preparing your system by installing:
- VSCode – A code editor to write your solidity contracts
- Foundry – For local blockchain emulation and deployment
- Ganache – Local Blockchain emulator
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.
// Install FoundryÂ
curl -L https://foundry.paradigm.xyz | bash
Sometimes, the Foundry install script times out. If you face these issues, try connecting through a VPN service first.
It timed out the first time during installation for me, so I installed ProtonVPN, and the installation became seamless afterward.
With the core tools installed, launch a new terminal and enter:
foundryup
This updates Foundry to the latest version. Crosscheck to see if the following are installed.
anvil --versionÂ
forge --versionÂ
cast --version Â
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.
Creating Your First Foundry Project
To initialize a new Foundry project, make a project directory and use Forge to set it up.
cd Desktop
mkdir my_foundry_project
cd my_foundry_project Â
forge init
This installs dependencies like the Solidity compiler and creates boilerplate files such as script
, src
, test
to start coding against.
You can delete the template counter.sol file you find inside the above files, we will be creating our own.
Connecting a Local Blockchain
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 link and download the setup, run it and you should have something like this.
Click on the quick start to open an emulated blockchain.
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 link and copy simplestorage.sol. Make sure to paste the file in the src/ folder.
// 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 => 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;
}
}
On the terminal, run forge compile to compile your code, and immediately, you will see a new folder appear called out,
this folder contains the byte code for the transactions we just sent.
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 guide.
Go to settings, and then network and populate with the following
Network name: Scofield_LocalHost
New RPC URL: HTTP://127.0.0.1:7545
Chain ID: 1337
Currency symbol: ETH
Click on save, and then to add the faucet on Metamask, import one of the private keys
from ganache to metamask, and you would  have 100ETH
Next, run forge create SimpleStorage --rpc-url
http://127.0.0.1:7545
--interactive
. This would prompt you to input your private key, which we would get from the Ganache page.
Click on the show key to open a private key and copy it. Please do not do this with a live account but since we are working with a test account, we are ok.
Paste the private key, and it will not show anything when you do but click on enter.
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 Anvil
and navigate our local blockchain without using Ganache.
If you find this article thrilling, discover extra thrilling posts like this on Learnhub Blog; we write a lot of tech-related topics from Cloud computing to Frontend Dev, Cybersecurity, AI, and Blockchain. Take a look at How to Build Offline Web Applications.Â