Objectives
By the end of this tutorial, you should be able to do the following:- Setup Hardhat for Base
- Create an NFT smart contract for Base
- Compile a smart contract for Base
- Deploy a smart contract to Base
- Interact with a smart contract deployed on Base
Prerequisites
Node v18+
This tutorial requires you have Node version 18+ installed.- Download Node v18+
nvm
to manage your node versions, you can just run nvm install 18
.
Coinbase Wallet
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.- Download Coinbase Wallet
Wallet funds
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.Creating a project
Before you can begin deploying smart contracts to Base, you need to set up your development environment by creating a Node.js project. To create a new Node.js project, run:Create a TypeScript project
then press enter to confirm the project root.
Select y
for both adding a .gitignore
and loading the sample project. It will take a moment for the project setup process to complete.
Configuring Hardhat with Base
In order to deploy smart contracts to the Base network, you will need to configure your Hardhat project and add the Base network. To configure Hardhat to use Base, add Base as a network to your project’shardhat.config.ts
file:
Install Hardhat toolbox
The above configuration uses the@nomicfoundation/hardhat-toolbox
plugin to bundle all the commonly used packages and Hardhat plugins recommended to start developing with Hardhat.
To install @nomicfoundation/hardhat-toolbox
, run:
Loading environment variables
The above configuration also uses dotenv to load theWALLET_KEY
environment variable from a .env
file to process.env.WALLET_KEY
. You should use a similar method to avoid hardcoding your private keys within your source code.
To install dotenv
, run:
dotenv
installed, you can create a .env
file with the following content:
<YOUR_PRIVATE_KEY>
with the private key for your wallet.
WALLET_KEY
is the private key of the wallet to use when deploying a contract. For instructions on how to get your private key from Coinbase Wallet, visit the Coinbase Wallet documentation. It is critical that you do NOT commit this to a public repoLocal Networks
You can run the Base network locally, and deploy using it. If this is what you are looking to do, see the repo containing the relevant Docker builds. It will take a very long time for your node to sync with the network. If you get errors that thenonce has already been used
when trying to deploy, you aren’t synced yet.
For quick testing, such as if you want to add unit tests to the below NFT contract, you may wish to leave the defaultNetwork
as 'hardhat'
.
Compiling the smart contract
Below is a simple NFT smart contract (ERC-721) written in the Solidity programming language: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:
contracts/Lock.sol
contract that was generated with the project and add the above code in a new file called contracts/NFT.sol
. (You can also delete the test/Lock.ts
test file, but you should add your own tests ASAP!).
To compile the contract using Hardhat, run:
Deploying the smart contract
Once your contract has been successfully compiled, you can deploy the contract to the Base Sepolia test network. To deploy the contract to the Base Sepolia test network, you’ll need to modify thescripts/deploy.ts
in your project:
If you’d like to deploy to mainnet, you’ll modify the command like so:
Verifying the Smart Contract
If you want to interact with your contract on the block explorer, you, or someone, needs to verify it first. The above contract has already been verified, so you should be able to view your version on a block explorer already. For the remainder of this tutorial, we’ll walk through how to verify your contract on Base Sepolia testnet. Inhardhat.config.ts
, configure Base Sepolia as a custom network. Add the following to your HardhatUserConfig
:
You can get your Basescan API key from basescan.org when you sign up for an account.
You can get your Basescan API key from basescan.org when you sign up for an account.
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:
Interacting with the Smart Contract
If you verified on Basescan, you can use theRead Contract
and Write Contract
tabs to interact with the deployed contract. You’ll need to connect your wallet first, by clicking the Connect button.