You can take a look at the complete code in the Hedera-Code-Snippets
repository.
Prerequisites
- Complete Tutorial: Configure Hardhat with Hedera localnet/testnet
- Basic understanding of smart contracts.
- Basic understanding of Node.js and JavaScript.
- Basic understanding of Hardhat EVM Development Tool and Ethers.
- ECDSA account from the Hedera Portal.
Table of Contents
- Project Setup
- Creating the ERC-721 Contract
- Deploy Your Smart Contract
- Minting an NFT
- Adding the Burn Functionality
- Burning an NFT
- Run tests
Video Tutorial
You can watch the video tutorial (which uses Hardhat version 2) or follow the step-by-step tutorial below (which uses Hardhat version 3).🚧 What's new: Hardhat 2 → 3
🚧 What's new: Hardhat 2 → 3
Key differences in Hardhat 3:
- compile → build
npx hardhat compileis nownpx hardhat build. This is the big one. The v3 migration guide explicitly shows using thebuildtask. - project init switch
v2 commonly usednpx hardhatornpx hardhat initto bootstrap. In v3 it’snpx hardhat --init.
- keystore helper commands are new
v3’s recommended flow includes a keystore plugin with commands likenpx hardhat keystore set HEDERA_RPC_URLandnpx hardhat keystore set HEDERA_PRIVATE_KEY. These weren’t standard in v2. - Foundry-compatiable Solidity tests
In addition to offering Javascript/Typescript integration tests, Hardhat v3 also integrates Foundry-compatible Solidity tests that allows developers to write unit tests directly in Solidity
- Enhanced Network Management
v3 allows tasks to create and manage multiple network connections simultaneously which is a significant improvement over the single, fixed connection available in version 2. This provides greater flexibility for scripts and tests that interact with multiple networks.
Step 1: Project Setup
Initialize Project
Set up your project by initializing the hardhat project:Install Dependencies
Next, install the required dependencies:HEDERA_RPC_URL, we’ll have https://testnet.hashio.io/api
HEDERA_PRIVATE_KEY, enter the HEX Encoded Private Key for your ECDSA account from the Hedera Portal.
Note
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
Update yourhardhat.config.tsfile in the root directory of your project. This file contains the network settings so Hardhat knows how to interact with the Hedera Testnet.
hardhat.config.ts
ctrl + c twice.
We won’t be using ignition and we will be removing the default contracts that comes with hardhat default project so we will remove all the unnecessary directories and files first:
Step 2: Creating the ERC-721 Contract
Create a new Solidity file (MyToken.sol) in our contracts directory:
contracts/MyToken.sol
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:
Step 3: Deploy Your ERC-721 Smart Contract
Create a deployment script (deploy.ts) in scripts directory:
scripts/deploy.ts
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.
Step 4: Minting an NFT
Create amint.ts 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.
scripts/mint.ts
deployer.address ). Then we verify the balance to see if we own an ERC-721 token of type MyToken.
Mint an NFT:
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:scripts/mint.ts 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.ts ) in your scripts directory:
scripts/burn.ts
<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:
Step 7: Run tests(Optional)
You can find both types of tests in the Hedera-Code-Snippets repository. You will find the following files:contracts/MyToken.t.soltest/MyToken.ts