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.
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 networkBase
(mainnet) as the network rather than Base Sepolia
(testnet).forge
)forge
)cast
)curl -L https://foundry.paradigm.xyz | bash
foundryup
, to install the latest (nightly) build of FoundryNFT
. 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:
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:
.env
file in the home directory of your project to add the Base network and an API key for verifying your contract on BaseScan:
ETHERSCAN_API_KEY
.
.env
file, run the following command to load the environment variables in the current command line session:
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:
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).
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:
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.
mint(address)
function on the NFT contract you just deployed.
Run the following command:
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.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:
0x0000000000000000000000000000000000000000000000000000000000000001
(1
in hex) — congratulations, you deployed a contract and minted an NFT with Foundry!