A tutorial that teaches how to deploy a smart contract on the Base test network using Hardhat. Includes instructions for setting up the environment, compiling, and deploying the smart contract.
nvm
to manage your node versions, you can just run nvm install 18
.
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.
hardhat.config.ts
file:
@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:
WALLET_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 repononce 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'
.
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:
scripts/deploy.ts
in your project:
hardhat.config.ts
, configure Base Sepolia as a custom network. Add the following to your HardhatUserConfig
:
Read Contract
and Write Contract
tabs to interact with the deployed contract. You’ll need to connect your wallet first, by clicking the Connect button.