How to Mint & Burn an ERC-721 Token Using Hardhat and Ethers (Part 1)
Last updated
Was this helpful?
Last updated
Was this helpful?
In this tutorial, you'll learn how to deploy, mint, and burn 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.
Basic understanding of smart contracts.
Basic understanding of and JavaScript.
Basic understanding of and .
ECDSA account from the .
Set up your Node.js project by initializing npm:
Next, install the required dependencies:
.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
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.
Create a new Solidity file (erc-721.sol
) in a new contracts
directory:
Let's compile this contract by running:
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:
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:
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:
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:
We also need a bunch of developer dependencies to be able to deploy and interact with smart contracts through :
Securely store your sensitive data like the OPERATOR_KEY
in a .env
file. For the JSON RPC_URL
, we'll use the .
Replace the your-operator-key
environment variable the HEX Encoded Private Key for your ECDSA account from the .
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 .
This contract was created using the 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.
This command will generate the smart contract artifacts, including the . We are now ready to deploy the smart contract.
Congratulations! 🎉 You have successfully learned how to deploy an ERC-721 smart contract using Hardhat, OpenZeppelin, and Ethers. Feel free to reach out in !
Writer: Michiel, Developer Relations Engineer
Editor: Luis, Sr Software Developer
Editor: Krystal, Technical Writer
|
|