This tutorial will walk you through writing and compiling an ERC-721 Solidity smart contract. You’ll then deploy and interact with it on the Hedera network using the Hedera Smart Contract Service (HSCS) and familiar EVM tools like Ethers.js, connecting via the JSON-RPC relay.
Make sure to select “Hardhat 3 → Typescript Hardhat Project using Mocha and Ethers.js” and accept the default values. Hardhat will configure your project correctly and install the required dependencies.
🚧 What's new: Hardhat 2 → 3
Key differences in Hardhat 3:
compile → build npx hardhat compile is now npx hardhat build. This is the big one. The v3 migration guide explicitly shows using the build task.
project init switch
v2 commonly used npx hardhat or npx hardhat init to bootstrap. In v3 it’s npx hardhat --init.
keystore helper commands are new
v3’s recommended flow includes a keystore plugin with commands like npx hardhat keystore set HEDERA_RPC_URL and npx 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.
Before we make any changes to our Hardhat configuration file, let’s set some configuration variables we will be referring to within the file later.
When you set up your keystore for the first time, you’ll be asked to create a keystore password. Save this password securely because you’ll need it anytime you set or reset a variable.
Copy
Ask AI
# If you have already set this before, please use the --force flagnpx hardhat keystore set HEDERA_RPC_URL
For HEDERA_RPC_URL, we’ll have https://testnet.hashio.io/api
Copy
Ask AI
# If you have already set this before, please use the --force flagnpx hardhat keystore set HEDERA_PRIVATE_KEY
For HEDERA_PRIVATE_KEY, enter the HEX Encoded Private Key for your ECDSA account from the Hedera Portal.
Hashiois 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 theHiero JSON-RPC Relay.
Update your hardhat.config.ts file in the root directory 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.
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.We won’t be using ignition and we will be removing the default contracts that come with the Hardhat default project, so we will remove all the unnecessary directories and files first:
Create a new Solidity file (MyToken.sol) in our contracts directory:
contracts/MyToken.sol
Copy
Ask AI
// SPDX-License-Identifier: MIT// Compatible with OpenZeppelin Contracts ^5.0.0pragma solidity ^0.8.28;import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";contract MyToken is ERC721, Ownable { uint256 private _nextTokenId; constructor(address initialOwner) ERC721("MyToken", "MTK") Ownable(initialOwner) {} function safeMint(address to) public onlyOwner returns (uint256) { uint256 tokenId = _nextTokenId++; _safeMint(to, tokenId); return tokenId; }}
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:
Copy
Ask AI
npx hardhat build
This command will generate the smart contract artifacts, including the ABI. We are now ready to deploy the smart contract.
Create a deployment script (deploy.ts) in scripts directory:
scripts/deploy.ts
Copy
Ask AI
import { network } from "hardhat";const { ethers } = await network.connect({ network: "testnet",});async function main() { // Get the signer of the tx and address for minting the token const [deployer] = await ethers.getSigners(); console.log("Deploying contract with the account:", deployer.address); // The deployer will also be the owner of our NFT contract const MyToken = await ethers.getContractFactory("MyToken", deployer); const contract = await MyToken.deploy(deployer.address); await contract.waitForDeployment(); const address = await contract.getAddress(); console.log("Contract deployed at:", address);}main().catch(console.error);
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
Ask AI
npx hardhat run scripts/deploy.ts --network testnet
Copy the deployed address—you’ll need this in subsequent steps.
The output looks like this:
Copy
Ask AI
~/projects/hardhat-erc-721-mint-burn >> npx hardhat run scripts/deploy.ts --network testnetCompiling your Solidity contracts...Compiled 1 Solidity file with solc 0.8.28 (evm target: cancun)Deploying contract with the account: 0xA98556A4deeB07f21f8a66093989078eF86faa30Contract deployed at: 0x6035bA3BCa9595637B463Aa514c3a1cE3f67f3de
Create a mint.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
Copy
Ask AI
import { network } from "hardhat";const { ethers } = await network.connect({ network: "testnet",});async function main() { const [deployer] = await ethers.getSigners(); // Get the ContractFactory of your MyToken ERC-721 contract const MyToken = await ethers.getContractFactory("MyToken", deployer); // Connect to the deployed contract // (REPLACE WITH YOUR CONTRACT ADDRESS) const contractAddress = "<your-contract-address>"; const contract = MyToken.attach(contractAddress); // Mint a token to ourselves const mintTx = await contract.safeMint(deployer.address); const receipt = await mintTx.wait(); console.log("receipt: ", JSON.stringify(receipt, null, 2)); const mintedTokenId = receipt?.logs[0].topics[3]; console.log("Minted token ID:", mintedTokenId); // Check the balance of the token const balance = await contract.balanceOf(deployer.address); console.log("Balance:", balance.toString(), "NFTs");}main().catch(console.error);
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:
After deploying your smart contract, you should verify the source code on HashScan. Programmatic verification is the most reliable and efficient method, especially for complex or upgradeable contracts.
Hardhat uses the dedicated hashscan-verify plugin to interface with the HashScan Smart Contract Verifier API (which is based on the Sourcify standard).Install the plugin in your project:
# Replace with your actual deployed contract addressCONTRACT_ADDRESS="0x6035bA3BCa9595637B463Aa514c3a1cE3f67f3de"npx hardhat hashscan-verify "$CONTRACT_ADDRESS" \ --contract "contracts/MyToken.sol:MyToken" \ --network testnet
The plugin will handle uploading the source code and metadata to the HashScan verifier. You should receive a Full Match verification status.Troubleshooting Verification
Issue
Solution
Verification Fails/Mismatch
Ensure your local compilation settings (Solidity version, optimizer runs, viaIR) exactly match the settings used for deployment. Run npx hardhat clean && npx hardhat compile before retrying.
Constructor Arguments Error
If your contract has constructor arguments, pass them as positional arguments after the contract name in the verification command.
Hardhat Keystore Password Prompt
The task may prompt for your Hardhat keystore password if it needs to sign a transaction to read deployment details. Enter it when prompted.
Congratulations! 🎉 You have successfully learned how to deploy and verify an ERC-721 smart contract using Hardhat, OpenZeppelin, and Ethers. Feel free to reach out inDiscord!