How to Mint & Burn an ERC-721 Token Using Hardhat and Ethers (Part 1)
In this tutorial, you'll learn how to deploy, mint, and burn ERC-721 tokens (NFTs) using Hardhat, Ethers, and OpenZeppelin contracts on the Hedera Testnet. We'll cover setting up your project, writing and deploying an ERC-721 smart contract, minting an NFT to your account, and finally, burning an NFT.
By the end, you'll have hands-on experience with essential ERC-721 operations and interacting with smart contracts on Hedera.
You can take a look at the complete code in the Hedera-Code-Snippets repository.
Prerequisites
Basic understanding of smart contracts.
Basic understanding of Node.js and JavaScript.
Basic understanding of Hardhat Ethereum Development Tool and Ethers.
ECDSA account from the Hedera Portal.
Table of Contents
Step 1: Project Setup
Initialize Project
Set up your Node.js project by initializing npm:
Install Dependencies
Next, install the required dependencies:
We also need a bunch of developer dependencies to be able to deploy and interact with smart contracts through Hardhat:
Create .env
File
.env
FileTo create your .env
file, you can either run one of the following commands or simply rename the .env.example
file in the project's root directory:
option 1: create a new .env
file
option 2: duplicate the .env.example
file
Securely store your sensitive data like the OPERATOR_KEY
in a .env
file. For the JSON RPC_URL
, we'll use the Hashio RPC endpoint for testnet.
Replace the your-operator-key
environment variable the HEX Encoded Private Key for your ECDSA account from the Hedera Portal.
Please note: that Hashio is intended for development and testing purposes only. for production use cases, it's recommended to use commercial-grade JSON-RPC Relay or host your own instance of the Hiero JSON-RPC Relay.
Configure Hardhat
Create a hardhat.config.js
file in the root of your project. This file contains the network settings so Hardhat knows how to interact with the Hedera Testnet. We'll use the variables you've stored in your .env
file.
You can verify the connection by running:
This command launches an interactive JavaScript console connected directly to the Hedera Testnet, providing access to the Ethers.js library for blockchain interactions. If you successfully enter this interactive environment, your Hardhat configuration is correct. To exit the interactive console, press ctrl + c
twice.
Step 2: Creating the ERC-721 Contract
Create a new Solidity file (erc-721.sol
) in a new contracts
directory:
This contract was created using the OpenZeppelin Contracts Wizard and OpenZeppelin's ERC-721 standard implementation with an ownership model. The ERC-721 token's name has been set to "MyToken." The contract implements the safeMint
function, which accepts the address of the owner of the new token and uses auto-increment IDs, starting from 0.
Let's compile this contract by running:
This command will generate the smart contract artifacts, including the ABI. We are now ready to deploy the smart contract.
Step 3: Deploy Your ERC-721 Smart Contract
Create a deployment script (deploy.js
) in a new scripts
directory:
In this script, we first retrieve your account (the deployer) using Ethers.js. This account will own the deployed smart contract. Next, we use this account to deploy the contract by calling MyToken.deploy(deployer.address)
. This passes your account address as the initial owner and signer of the deployment transaction.
Deploy your contract by executing the script:
Copy the deployed address—you'll need this in subsequent steps.
The output looks like this:
Step 4: Minting an NFT
Create a mint.js
script in your scripts
directory to mint an NFT. Don't forget to replace the <your-contract-address>
with the address you've just copied.
The code mints a new NFT to your account ( deployer.address
). Then we verify the balance to see if we own an ERC-721 token of type MyToken
.
Mint an NFT:
Expected output:
Step 5: Adding the Burn Functionality
Update your contract to add NFT burning capability by importing the burnable extension and adding it to the interfaces list for your contract:
Recompile and redeploy:
Copy the new smart contract address and replace the address in the scripts/mint.js
script with your new address. Let's mint a new NFT for the redeployed contract:
Step 6: Burning an NFT
Create a burn script (burn.js
) in your scripts
directory:
Again, ensure you update <your-contract-address>
to interact with your correct contract. The script will burn the ERC-721 token with the ID set to 0
, which is the ERC-721 token you've just minted. To be sure the token has been deleted, let's print the balance for our account to the terminal. The balance should show a balance of 0
.
Burn the NFT:
Congratulations! 🎉 You have successfully learned how to deploy an ERC-721 smart contract using Hardhat, OpenZeppelin, and Ethers. Feel free to reach out in Discord!
Additional Resources
Last updated
Was this helpful?