Foundry: Setting up Foundry with Base
In this tutorial, you'll learn how to set up Foundry, a toolchain for smart contract development. You'll also learn how to configure it to work with Base.
Objectives
By the end of this tutorial, you should be able to:
- Install Foundry
- Create a Foundry project
- Compile a smart contract using Foundry
- Configure Foundry to work with Base
Overview
Foundry is a smart contract development toolchain that is composed of multiple small command line tools:
- forge: Compile, test, and deploy your smart contracts
- cast: Interact with the Blockchain over RPC. You can make smart contract calls, send transactions, or retrieve any type of chain data
- chisel: A Solidity REPL. You can write Solidity code directly
- anvil: A local Blockchain node for testing and development
Using Foundry you can manage your dependencies, compile your project, run tests, deploy smart contracts and interact with the chain from the command-line and via Solidity scripts.
For a deep dive on the Foundry features and full capabilities, check out the Foundry Book.
Installing Foundry
In order to install Foundry, you can use Foundryup
, the Foundry's toolchain installer.
To install Foundryup
you have to run in the terminal:
$ curl -L https://foundry.paradigm.xyz | bash
After Foundryup
is installed, you can install Foundry
by running:
$ foundryup
You can verify the installation by trying the following commands:
$ forge --version
$ cast --version
$ chisel --version
$ anvil --version
My First Foundry Project
To create a foundry project you can simply run:
$ forge init hello_foundry_in_base
This will create a foundry project with the following structure:
├── lib # all the libraries installed
├── script # scripts folder, e.g., deploy scripts
├── src # smart contracts folder
├── test # tests folder
└── foundry.toml # foundry configuration file
You will also notice a .gitsubmodules
file -- this is because Foundry
handles dependencies using Git submodules.
By default the Foundry structure stores smart contracts in the src
folder. You can change this in the foundry.toml
configuration file.
For instance:
[profile.default]
src = 'contracts'
In order to compile the project, simply run:
forge build
Setting up Foundry with Base
In order to work with Base, you need to configure a couple of settings in the configuration foundry.toml
file.
The first thing is the Solidity version.
You need to configure your config file as follows:
[profile.default]
src = 'src'
out = 'out'
libs = ['lib']
solc_version = "0.8.23"
Be sure that you modify the pragma of your contracts and simply run forge build
to ensure everything works well.
We also recommend setting up JSON RPC endpoints for Base and the API key for Basescan in the configuration file so that your environment is ready to deploy your smart contracts.
Your configuration file should look like the following:
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc_version = "0.8.23"
[rpc_endpoints]
base = "https://mainnet.base.org"
baseSepolia = "https://sepolia.base.org"
[etherscan]
baseSepolia = { key = "${BASESCAN_API_KEY}", url = "https://api-sepolia.basescan.org/api" }
base = { key = "${BASESCAN_API_KEY}", url = "https://api.basescan.org/api" }
We included 2 JSON RPC endpoints for Base
and Base Sepolia
and similar for the Etherscan section, we included the configuration for Basescan
for Sepolia and Mainnet. Both rely on the same API Key, BASESCAN_API_KEY
.
Conclusion
In this tutorial, you've embarked on the journey of smart contract development with Base and Foundry. You've learned the essential steps, from installing Foundry using the convenient Foundryup
toolchain installer to creating your first project and configuring Foundry to seamlessly integrate with Base.