This guide walks through the process of deploying an ERC-721 token with images stored on IPFS using Hardhat on the Hemi network.
Ensure you have the following installed:
-
Create a new folder for your project and initialize it:
mkdir nft-erc721 cd nft-erc721 npm init -y mkdir contracts mkdir scripts
-
Install Hardhat:
npm install --save-dev hardhat npx hardhat
-
When prompted, choose
Create a basic sample project
and confirm the installation of the necessary dependencies. -
Install OpenZeppelin contracts:
npm install @openzeppelin/contracts
-
Install Ethers.js and dotenv to manage private keys and RPC URLs:
npm install @nomiclabs/hardhat-ethers ethers dotenv
-
Create a
.env
file to store your private key and RPC URL:PRIVATE_KEY=your_private_key RPC_URL=https://testnet.rpc.hemi.network/rpc
-
Create the ERC-721 Smart Contract:
Inside the
contracts/
folder, create a fileNFT_HKG.sol
:// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract NFT_HKG is ERC721, Ownable { uint256 public tokenCounter; mapping(uint256 => string) private _tokenURIs; constructor() ERC721("NFT_HKG", "HKG") { tokenCounter = 0; } function mintNFT(address recipient, string memory tokenURI) public onlyOwner returns (uint256) { uint256 newTokenId = tokenCounter; _safeMint(recipient, newTokenId); _setTokenURI(newTokenId, tokenURI); tokenCounter += 1; return newTokenId; } function _setTokenURI(uint256 tokenId, string memory tokenURI) internal { _tokenURIs[tokenId] = tokenURI; } function tokenURI(uint256 tokenId) public view override returns (string memory) { return _tokenURIs[tokenId]; } }
- Open
hardhat.config.js
and configure it for the Hemi network:require("@nomiclabs/hardhat-ethers"); require("dotenv").config(); module.exports = { solidity: "0.8.18", networks: { hemi: { url: process.env.RPC_URL, accounts: [process.env.PRIVATE_KEY], }, }, };
-
Create a deployment script in the
scripts/
folder calleddeploy.js
:async function main() { const [deployer] = await ethers.getSigners(); console.log("Deploying contracts with the account:", deployer.address); const NFT = await ethers.getContractFactory("NFT_HKG"); const nft = await NFT.deploy(); console.log("NFT deployed to:", nft.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
-
Deploy the contract:
npx hardhat run scripts/deploy.js --network hemi
The contract address will be logged in the terminal after successful deployment.
-
Create a minting script called
mint.js
in thescripts/
folder:async function main() { const nftAddress = "your_contract_address"; // Replace with your deployed contract address const [deployer] = await ethers.getSigners(); const NFT = await ethers.getContractAt("NFT_HKG", nftAddress); const tokenURI = "ipfs://your-ipfs-hash"; // Replace with your IPFS image URL const tx = await NFT.mintNFT(deployer.address, tokenURI); await tx.wait(); console.log(`NFT minted with tokenURI: ${tokenURI}`); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
-
Run the minting script:
npx hardhat run scripts/mint.js --network hemi
Once the transaction is confirmed, the NFT will be minted with the provided IPFS URL.
You have successfully deployed and minted an ERC-721 NFT with metadata stored on IPFS using Hardhat on the Hemi network. If you have any issues or questions, feel free to reach out!