A tutorial that teaches how to deploy a smart contract on the Base test network using Foundry. Includes instructions for setting up the environment, compiling, and deploying the smart contract.
This article will provide an overview of the Foundry development toolchain, and show you how to deploy a contract to Base Sepolia testnet.
Foundry is a powerful suite of tools to develop, test, and debug your smart contracts. It comprises several individual tools:
forge
: the main workhorse of Foundry — for developing, testing, compiling, and deploying smart contractscast
: a command-line tool for performing Ethereum RPC calls (e.g., interacting with contracts, sending transactions, and getting onchain data)anvil
: a local testnet node, for testing contract behavior from a frontend or over RPCchisel
: a Solidity REPL, for trying out Solidity snippets on a local or forked networkFoundry offers extremely fast feedback loops (due to the under-the-hood Rust implementation) and less context switching — because you’ll be writing your contracts, tests, and deployment scripts All in Solidity!
For production / mainnet deployments the steps below in this tutorial will be almost identical, however, you’ll want to ensure that you’ve configured Base
(mainnet) as the network rather than Base Sepolia
(testnet).
By the end of this tutorial, you should be able to do the following:
forge
)forge
)cast
)This tutorial requires you have Foundry installed.
curl -L https://foundry.paradigm.xyz | bash
foundryup
, to install the latest (nightly) build of FoundryFor more information, see the Foundry Book installation guide.
In order to deploy a smart contract, you will first need a web3 wallet. You can create a wallet by downloading the Coinbase Wallet browser extension.
Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with ETH to cover those gas fees.
For this tutorial, you will be deploying a contract to the Base Sepolia test network. You can fund your wallet with Base Sepolia ETH using one of the faucets listed on the Base Network Faucets page.
Before you can begin deploying smart contracts to Base, you need to set up your development environment by creating a Foundry project.
To create a new Foundry project, first create a new directory:
Then run:
This will create a Foundry project, which has the following basic layout:
Below is a simple NFT smart contract (ERC-721) written in the Solidity programming language:
The Solidity code above defines a smart contract named NFT
. The code uses the ERC721
interface provided by the OpenZeppelin Contracts library to create an NFT smart contract. OpenZeppelin allows developers to leverage battle-tested smart contract implementations that adhere to official ERC standards.
To add the OpenZeppelin Contracts library to your project, run:
In your project, delete the src/Counter.sol
contract that was generated with the project and add the above code in a new file called src/NFT.sol
. (You can also delete the test/Counter.t.sol
and script/Counter.s.sol
files, but you should add your own tests ASAP!).
To compile our basic NFT contract using Foundry, run:
Next, you will configure your Foundry project to deploy smart contracts to the Base network. First you’ll store your private key in an encrypted keystore, then you’ll add Base as a network.
The following command will import your private key to Foundry’s secure keystore. You will be prompted to enter your private key, as well as a password for signing transactions:
Run this command to confirm that the ‘deployer’ account is setup in Foundry:
When verifying a contract with BaseScan, you need an API key. You can get your BaseScan API key from here after you sign up for an account.
Now create a .env
file in the home directory of your project to add the Base network and an API key for verifying your contract on BaseScan:
Note that even though you’re using BaseScan as your block explorer, Foundry expects the API key to be defined as ETHERSCAN_API_KEY
.
Now that you’ve created the above .env
file, run the following command to load the environment variables in the current command line session:
With your contract compiled and your environment configured, you are ready to deploy to the Base Sepolia test network!
Today, you’ll use the forge create
command, which is a straightforward way to deploy a single contract at a time. In the future, you may want to look into forge script
, which enables scripting onchain transactions and deploying more complex smart contract projects.
You’ll need testnet ETH in your wallet. See the prerequisites if you haven’t done that yet. Otherwise, the deployment attempt will fail.
To deploy the contract to the Base Sepolia test network, run the following command. You will be prompted to enter the password that you set earlier, when you imported your private key:
The contract will be deployed on the Base Sepolia test network. You can view the deployment status and contract by using a block explorer and searching for the address returned by your deploy script. If you’ve deployed an exact copy of the NFT contract above, it will already be verified and you’ll be able to read and write to the contract using the web interface.
If you’d like to deploy to mainnet, you’ll modify the command like so:
Regardless of the network you’re deploying to, if you’re deploying a new or modified contract, you’ll need to verify it.
In web3, it’s considered best practice to verify your contracts so that users and other developers can inspect the source code, and be sure that it matches the deployed bytecode on the blockchain.
Further, if you want to allow others to interact with your contract using the block explorer, it first needs to be verified. The above contract has already been verified, so you should be able to view your version on a block explorer already, but we’ll still walk through how to verify a contract on Base Sepolia testnet.
Remember, you need an API key from BaseScan to verify your contracts. You can get your API key from the BaseScan site after you sign up for an account.
Grab the deployed address and run:
You should see an output similar to:
Search for your contract on BaseScan to confirm it is verified.
You can’t re-verify a contract identical to one that has already been verified. If you attempt to do so, such as verifying the above contract, you’ll get an error similar to:
If you verified on BaseScan, you can use the Read Contract
and Write Contract
sections under the Contract
tab to interact with the deployed contract. To use Write Contract
, you’ll need to connect your wallet first, by clicking the Connect to Web3
button (sometimes this can be a little finicky, and you’ll need to click Connect
twice before it shows your wallet is successfully connected).
To practice using the cast
command-line tool which Foundry provides, you’ll perform a call without publishing a transaction (a read), then sign and publish a transaction (a write).
A key component of the Foundry toolkit, cast
enables us to interact with contracts, send transactions, and get onchain data using Ethereum RPC calls. First you will perform a call from your account, without publishing a transaction.
From the command-line, run:
You should receive 0x0000000000000000000000000000000000000000000000000000000000000000
in response, which equals 0
in hexadecimal. And that makes sense — while you’ve deployed the NFT contract, no NFTs have been minted yet and therefore your account’s balance is zero.
Now, sign and publish a transaction, calling the mint(address)
function on the NFT contract you just deployed.
Run the following command:
Note that in this cast send
command, you had to include your private key, but this is not required for cast call
, because that’s for calling view-only contract functions and therefore you don’t need to sign anything.
If successful, Foundry will respond with information about the transaction, including the blockNumber
, gasUsed
, and transactionHash
.
Finally, let’s confirm that you did indeed mint yourself one NFT. If you run the first cast call
command again, you should see that your balance increased from 0 to 1:
And the response: 0x0000000000000000000000000000000000000000000000000000000000000001
(1
in hex) — congratulations, you deployed a contract and minted an NFT with Foundry!
Phew, that was a lot! You learned how to setup a project, deploy to Base, and interact with our smart contract using Foundry. The process is the same for real networks, just more expensive — and of course, you’ll want to invest time and effort testing your contracts, to reduce the likelihood of user-impacting bugs before deploying.
For all things Foundry, check out the Foundry book, or head to the official Telegram dev chat or support chat.
A tutorial that teaches how to deploy a smart contract on the Base test network using Foundry. Includes instructions for setting up the environment, compiling, and deploying the smart contract.
This article will provide an overview of the Foundry development toolchain, and show you how to deploy a contract to Base Sepolia testnet.
Foundry is a powerful suite of tools to develop, test, and debug your smart contracts. It comprises several individual tools:
forge
: the main workhorse of Foundry — for developing, testing, compiling, and deploying smart contractscast
: a command-line tool for performing Ethereum RPC calls (e.g., interacting with contracts, sending transactions, and getting onchain data)anvil
: a local testnet node, for testing contract behavior from a frontend or over RPCchisel
: a Solidity REPL, for trying out Solidity snippets on a local or forked networkFoundry offers extremely fast feedback loops (due to the under-the-hood Rust implementation) and less context switching — because you’ll be writing your contracts, tests, and deployment scripts All in Solidity!
For production / mainnet deployments the steps below in this tutorial will be almost identical, however, you’ll want to ensure that you’ve configured Base
(mainnet) as the network rather than Base Sepolia
(testnet).
By the end of this tutorial, you should be able to do the following:
forge
)forge
)cast
)This tutorial requires you have Foundry installed.
curl -L https://foundry.paradigm.xyz | bash
foundryup
, to install the latest (nightly) build of FoundryFor more information, see the Foundry Book installation guide.
In order to deploy a smart contract, you will first need a web3 wallet. You can create a wallet by downloading the Coinbase Wallet browser extension.
Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with ETH to cover those gas fees.
For this tutorial, you will be deploying a contract to the Base Sepolia test network. You can fund your wallet with Base Sepolia ETH using one of the faucets listed on the Base Network Faucets page.
Before you can begin deploying smart contracts to Base, you need to set up your development environment by creating a Foundry project.
To create a new Foundry project, first create a new directory:
Then run:
This will create a Foundry project, which has the following basic layout:
Below is a simple NFT smart contract (ERC-721) written in the Solidity programming language:
The Solidity code above defines a smart contract named NFT
. The code uses the ERC721
interface provided by the OpenZeppelin Contracts library to create an NFT smart contract. OpenZeppelin allows developers to leverage battle-tested smart contract implementations that adhere to official ERC standards.
To add the OpenZeppelin Contracts library to your project, run:
In your project, delete the src/Counter.sol
contract that was generated with the project and add the above code in a new file called src/NFT.sol
. (You can also delete the test/Counter.t.sol
and script/Counter.s.sol
files, but you should add your own tests ASAP!).
To compile our basic NFT contract using Foundry, run:
Next, you will configure your Foundry project to deploy smart contracts to the Base network. First you’ll store your private key in an encrypted keystore, then you’ll add Base as a network.
The following command will import your private key to Foundry’s secure keystore. You will be prompted to enter your private key, as well as a password for signing transactions:
Run this command to confirm that the ‘deployer’ account is setup in Foundry:
When verifying a contract with BaseScan, you need an API key. You can get your BaseScan API key from here after you sign up for an account.
Now create a .env
file in the home directory of your project to add the Base network and an API key for verifying your contract on BaseScan:
Note that even though you’re using BaseScan as your block explorer, Foundry expects the API key to be defined as ETHERSCAN_API_KEY
.
Now that you’ve created the above .env
file, run the following command to load the environment variables in the current command line session:
With your contract compiled and your environment configured, you are ready to deploy to the Base Sepolia test network!
Today, you’ll use the forge create
command, which is a straightforward way to deploy a single contract at a time. In the future, you may want to look into forge script
, which enables scripting onchain transactions and deploying more complex smart contract projects.
You’ll need testnet ETH in your wallet. See the prerequisites if you haven’t done that yet. Otherwise, the deployment attempt will fail.
To deploy the contract to the Base Sepolia test network, run the following command. You will be prompted to enter the password that you set earlier, when you imported your private key:
The contract will be deployed on the Base Sepolia test network. You can view the deployment status and contract by using a block explorer and searching for the address returned by your deploy script. If you’ve deployed an exact copy of the NFT contract above, it will already be verified and you’ll be able to read and write to the contract using the web interface.
If you’d like to deploy to mainnet, you’ll modify the command like so:
Regardless of the network you’re deploying to, if you’re deploying a new or modified contract, you’ll need to verify it.
In web3, it’s considered best practice to verify your contracts so that users and other developers can inspect the source code, and be sure that it matches the deployed bytecode on the blockchain.
Further, if you want to allow others to interact with your contract using the block explorer, it first needs to be verified. The above contract has already been verified, so you should be able to view your version on a block explorer already, but we’ll still walk through how to verify a contract on Base Sepolia testnet.
Remember, you need an API key from BaseScan to verify your contracts. You can get your API key from the BaseScan site after you sign up for an account.
Grab the deployed address and run:
You should see an output similar to:
Search for your contract on BaseScan to confirm it is verified.
You can’t re-verify a contract identical to one that has already been verified. If you attempt to do so, such as verifying the above contract, you’ll get an error similar to:
If you verified on BaseScan, you can use the Read Contract
and Write Contract
sections under the Contract
tab to interact with the deployed contract. To use Write Contract
, you’ll need to connect your wallet first, by clicking the Connect to Web3
button (sometimes this can be a little finicky, and you’ll need to click Connect
twice before it shows your wallet is successfully connected).
To practice using the cast
command-line tool which Foundry provides, you’ll perform a call without publishing a transaction (a read), then sign and publish a transaction (a write).
A key component of the Foundry toolkit, cast
enables us to interact with contracts, send transactions, and get onchain data using Ethereum RPC calls. First you will perform a call from your account, without publishing a transaction.
From the command-line, run:
You should receive 0x0000000000000000000000000000000000000000000000000000000000000000
in response, which equals 0
in hexadecimal. And that makes sense — while you’ve deployed the NFT contract, no NFTs have been minted yet and therefore your account’s balance is zero.
Now, sign and publish a transaction, calling the mint(address)
function on the NFT contract you just deployed.
Run the following command:
Note that in this cast send
command, you had to include your private key, but this is not required for cast call
, because that’s for calling view-only contract functions and therefore you don’t need to sign anything.
If successful, Foundry will respond with information about the transaction, including the blockNumber
, gasUsed
, and transactionHash
.
Finally, let’s confirm that you did indeed mint yourself one NFT. If you run the first cast call
command again, you should see that your balance increased from 0 to 1:
And the response: 0x0000000000000000000000000000000000000000000000000000000000000001
(1
in hex) — congratulations, you deployed a contract and minted an NFT with Foundry!
Phew, that was a lot! You learned how to setup a project, deploy to Base, and interact with our smart contract using Foundry. The process is the same for real networks, just more expensive — and of course, you’ll want to invest time and effort testing your contracts, to reduce the likelihood of user-impacting bugs before deploying.
For all things Foundry, check out the Foundry book, or head to the official Telegram dev chat or support chat.