Skip to main content

Deploy a smart contract with Foundry

In this example, we will learn how to deploy a smart contract to Celo testnet, mainnet, or a local network using Foundry.


Introduction to Foundry​

Foundry is a smart contract development toolchain.

Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line and via Solidity scripts.

Prerequisites​

You will need the Rust compiler and Cargo, the Rust package manager. The easiest way to install both is with rustup.rs.

Using Foundryup​

Foundryup is the Foundry toolchain installer. You can find more about it here.

Open your terminal and run the following command:

curl -L https://foundry.paradigm.xyz | bash

This will install Foundryup, then simply follow the instructions on-screen, which will make the foundryup command available in your CLI.

Running foundryup by itself will install the latest (nightly) precompiled binaries: forge, cast, anvil, and chisel.

Create a new project using Forge​

To start a new project with Foundry, use:

forge init hello_foundry
info

If you initializing project in an already initialized git repo use:

forge init project_name --no-git

Adding a dependency​

To add a dependency, use:

forge install openzeppelin/openzeppelin-contracts

Remapping dependencies​

Forge can remap dependencies to make them easier to import. Forge will automatically try to deduce some remappings for you

$ forge remappings

ds-test/=lib/solmate/lib/ds-test/src/
forge-std/=lib/forge-std/src/
solmate/=lib/solmate/src/
weird-erc20/=lib/weird-erc20/src/

These remappings mean:

  • To import from forge-std we would write: import forge-std/Contract.sol;
  • To import from ds-test we would write: import ds-test/Contract.sol;
  • To import from solmate we would write: import solmate/Contract.sol;
  • To import from weird-erc20 we would write: import weird-erc20/Contract.sol;

You can customize these remappings by creating a remappings.txt file in the root of your project.

Removing dependencies​

You can remove dependencies using

forge remove openzeppelin/openzeppelin-contracts

Adding Celo specific config to foundry.toml​

Add the following configuration to foundry.toml file in the root level of your project.

[rpc_endpoints]
celo-alfajores = "https://alfajores-forno.celo-testnet.org"
celo = "https://forno.celo.org"

Deploying contract​

Forge can deploy smart contracts to a given network using:

The below example deploys Counter contract at location src/Counter.sol in the project to the Celo Alfajores Testnet.

forge create --rpc-url celo-alfajores --private-key <your_private_key> src/Counter.sol:Counter
info

Notice the contract name after :, this is because a single solidity file can have multiple contracts.

It is recommended to use --verify flag so that the contract gets verified right after deployment, this requires etherscan configuration in the foundry.toml file.

On successful deployment, you should a following output!

github

Verify Smart Contract using Foundry​

Verifying a smart contract allows developers to review your code from within the CeloScan Block Explorer

Prerequisites for verification​

Project must be setup using Foundry

Add Celo configuration in foundry.toml​

Add the below given configuration to the foundry.toml file at the root level of your project.

[etherscan]
celo-alfajores = { key = "${CELOSCAN_API_KEY}", url = "https://api-alfajores.celoscan.io/api"}
celo = { key = "${CELOSCAN_API_KEY}", url = "https://api.celoscan.io/api"}

Make sure to also have Celo RPC configuration in foundry.toml file, here it is:

[rpc_endpoints]
celo-alfajores = "https://alfajores-forno.celo-testnet.org"
celo = "https://forno.celo.org"

Verifying Contracts​

Use the following command

For Alfajores Testnet:

forge verify-contract --chain-id celo-alfajores <contract_address> <contract_location> --watch

For Celo Mainnet:

forge verify-contract --chain-id celo <contract_address> <contract_location> --watch