In this week's assignment, we will work on building a simple NFT contract, setting up Hardhat tasks, and creating test cases to ensure proper functionality. The tasks involve deploying, minting, and interacting with an NFT smart contract, which we'll build using OpenZeppelin and Hardhat.
mkdir nft-project
cd nft-project
npx hardhat
make sure to use .env
file for sensetive info.
IMPORTANT : The given files are just for helping purposes and giving idea of the structure of the project.
This repo contains Demo codes for the files without the depandancies.
You will write a simple ERC721-based contract for minting NFTs using OpenZeppelin libraries.
The contract file is located at:
We will create tasks to deploy the NFT contract and mint NFTs using Hardhat.
The task file is located at:
The helpers for interacting with our contract and environment variables are implemented in the following files:
We will write unit and integration tests to validate the NFT contract functionality. The tests will check minting functionality and balance tracking.
Test files:
- test/MyNFT.spec.ts – Unit tests for the NFT contract.
- test/tasks.spec.ts – Integration tests for Hardhat tasks.
- test/test-helpers.ts – Helpers used in the tests.
We will configure Hardhat to use the Alchemy API and connect to the Sepolia test network.
Configuration file:
-
Compile the smart contracts:
npx hardhat compile
-
Deploy the contract:
npx hardhat deploy-contract
-
Mint an NFT:
npx hardhat mint-nft --tokenUri "https://example.com/token-metadata"
To run the test cases, execute:
npx hardhat test
Example output:
mintNft
✓ calls through and returns the transaction object (60ms)
MyNFT
mintNft
✓ emits the Transfer event (60ms)
✓ returns the new item ID
✓ increments the item ID (57ms)
✓ cannot mint to address zero
balanceOf
✓ gets the count of NFTs for this address
6 passing (2s)
✨ Done in 5.66s.
-
A link to your GitHub Pull Request with the implemented contract and tasks.
-
Head over to the Coursework submission link: Google Form to submit your work.
Inform your UniDAO lead once the submission has been made.
Feel free to ask any questions or seek clarification on Discord.