How to Set Access Control, a Token URI, Pause, and Transfer an ERC-721 Token Using Hardhat (Part 2)
In this tutorial, you’ll learn how to create and manage an advanced ERC-721 token smart contract using Hardhat and OpenZeppelin. We’ll cover deploying the contract, minting NFTs, pausing and unpausing the contract, and transferring tokens. You’ll gain experience with Access Control (admin, minting, pausing roles), URI storage, and Pausable functionalities.
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
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.
Create a new Solidity file named MyTokenAdvanced.sol in your contracts directory, and paste this Solidity code:
contracts/MyTokenAdvanced.sol
Copy
Ask AI
// SPDX-License-Identifier: MIT// Compatible with OpenZeppelin Contracts ^5.0.0pragma solidity ^0.8.28;import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";import {ERC721Pausable} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Pausable.sol";import {ERC721URIStorage} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";contract MyTokenAdvanced is ERC721, ERC721URIStorage, ERC721Pausable, AccessControl { bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE"); bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); uint256 private _nextTokenId; constructor(address defaultAdmin, address pauser, address minter) ERC721("MyTokenAdvanced", "MTK") { _grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin); _grantRole(PAUSER_ROLE, pauser); _grantRole(MINTER_ROLE, minter); } function pause() public onlyRole(PAUSER_ROLE) { _pause(); } function unpause() public onlyRole(PAUSER_ROLE) { _unpause(); } function safeMint(address to, string memory uri) public onlyRole(MINTER_ROLE) returns (uint256) { uint256 tokenId = _nextTokenId++; _safeMint(to, tokenId); _setTokenURI(tokenId, uri); return tokenId; } // The following functions are overrides required by Solidity. function _update(address to, uint256 tokenId, address auth) internal override(ERC721, ERC721Pausable) returns (address) { return super._update(to, tokenId, auth); } function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) { return super.tokenURI(tokenId); } function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721URIStorage, AccessControl) returns (bool) { return super.supportsInterface(interfaceId); }}
The contract implements the ERC721URIStorage, ERC721Pausable, and AccessControl interfaces from OpenZeppelin. You can create the contract yourself using the OpenZeppelin Wizard and enable “Mintable,” “Pausable,” “URI Storage,” and “Access Control → Roles.”Compile your new contract:
Step 2: Deploying the Smart Contract and Minting a Token
Create deploy-advanced.ts in your scripts folder:
scripts/deploy-advanced.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 MyTokenAdvanced = await ethers.getContractFactory( "MyTokenAdvanced", deployer ); const contract = await MyTokenAdvanced.deploy( deployer.address, deployer.address, "0xc0ffee254729296a45a3885639AC7E10F9d54979" ); await contract.waitForDeployment(); const address = await contract.getAddress(); console.log("Contract deployed at:", address);}main().catch(console.error);
Note that we are providing three arguments to the MyTokenAdvanced.deploy() function. When we look at the constructor of our smart contract, we can provide the admin, pauser, and minter roles.
The deploy-advanced.ts script sets the minter role to an unknown (random) address. This should prevent the deployer account from minting new tokens in the next step. First, let’s run the deployer script:
Copy
Ask AI
npx hardhat run scripts/deploy-advanced.ts --network testnet
Here’s the output of the command:
Copy
Ask AI
Deploying contract with the account: 0xA98556A4deeB07f21f8a66093989078eF86faa30Contract deployed at: 0x5f41411477b506FA32DFe3B73BEE52a3D80B755f
Copy the contract address of your newly deployed contract. Next, create mint-advanced.ts in your scripts folder:
scripts/mint-advanced.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 MyTokenAdvanced ERC-721 contract const MyTokenAdvanced = await ethers.getContractFactory( "MyTokenAdvanced", deployer ); // Connect to the deployed contract (REPLACE WITH YOUR CONTRACT ADDRESS) const contractAddress = "0x6F5F9Ed50140bb9C94246257241Ed5AA5d40A25d"; const contract = MyTokenAdvanced.attach(contractAddress); // Mint a token to ourselves const mintTx = await contract.safeMint( deployer.address, "https://myserver.com/8bitbeard/8bitbeard-tokens/tokens/1" ); 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);
This contract tries to mint a new token and sets the token URI to https://myserver.com/8bitbeard/8bitbeard-tokens/tokens/1 . This transaction will fail because our deployer account doesn’t have the minter permission. Run the script:
Copy
Ask AI
npx hardhat run scripts/mint-advanced.ts --network testnet
‼️ Notice minting fails due to incorrect permissions. Let’s fix this in the next step.
Now that the deployer account has all the roles, redeploy the contract:
Copy
Ask AI
npx hardhat run scripts/deploy-advanced.ts --network testnet
Don’t forget to copy the new contract address and update the contractAddress variable in your mint-advanced.ts script with this new address.Next, execute the minting logic:
Copy
Ask AI
npx hardhat run scripts/mint-advanced.ts --network testnet
The new token will be minted with token ID 0 and the corresponding token URI is printed to your terminal.
Create a new pause-advanced.ts script and make sure to replace the contractAddress variable with your address:
scripts/pause-advanced.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 MyTokenAdvanced ERC-721 contract const MyTokenAdvanced = await ethers.getContractFactory( "MyTokenAdvanced", deployer ); // Connect to the deployed contract (REPLACE WITH YOUR CONTRACT ADDRESS) const contractAddress = "0x2a35e6532e9e6477205Cc845362EB6e71FcC0F0E"; const contract = MyTokenAdvanced.attach(contractAddress); // Pause the token const pauseTx = await contract.pause(); const receipt = await pauseTx.wait(); console.log("receipt: ", JSON.stringify(receipt, null, 2)); console.log("Paused token"); // Read the paused state const pausedState = await contract.paused(); console.log("Contract paused state is:", pausedState);}main().catch(console.error);
The script calls the pause function on your contract. As we have the correct role, the token will be paused, and its paused state will be printed to the terminal. Execute the script:
Copy
Ask AI
npx hardhat run scripts/pause-advanced.ts --network testnet
The contract will return true when it is paused. Now, nobody can mint new tokens.
Pausing an ERC-721 contract temporarily disables critical functions, including
minting, transferring, and burning tokens. While the contract is paused, users
cannot perform these operations, making it particularly useful in emergency
scenarios or maintenance periods. However, read operations, such as checking
token balances or URIs, are still possible.
Create a transfer-advanced.ts script to transfer an NFT to another address. Don’t forget to replace the contractAddress with your smart contract address.
scripts/transfer-advanced.ts
Copy
Ask AI
async function main() { const [deployer] = await ethers.getSigners(); const MyTokenAdvanced = await ethers.getContractFactory( "MyTokenAdvanced", deployer ); // Connect to the deployed contract (REPLACE WITH YOUR CONTRACT ADDRESS) const contractAddress = "0x11828533C93F8A1e19623343308dFb4a811005dE"; const contract = await MyTokenAdvanced.attach(contractAddress); // Unpause the token const unpauseTx = await contract.unpause(); await unpauseTx.wait(); console.log("Unpaused token"); // Read the paused state const pausedState = await contract.paused(); console.log("Contract paused state is:", pausedState); // Transfer the token with ID 0 const transferTx = await contract.transferFrom( deployer.address, "0x5FbDB2315678afecb367f032d93F642f64180aa3", 0 ); await transferTx.wait(); const balance = await contract.balanceOf( "0x5FbDB2315678afecb367f032d93F642f64180aa3" ); console.log("Balance:", balance.toString(), "NFTs");}main().catch(console.error);
This script will first unpause your contract and then transfer the token to a random address 0x5FbDB2315678afecb367f032d93F642f64180aa3 using the transferFrom function on your contract. This function accepts the sender address, receiver address, and the token ID you want to transfer. Next, we check if the account has actually received the token by verifying its balance.Execute the script to transfer the token:
Copy
Ask AI
npx hardhat run scripts/transfer-advanced.ts --network testnet
If the balance for the**0x5FbDB2315678afecb367f032d93F642f64180aa3**account shows 1 , then you’ve successfully transferred the NFT and completed this tutorial! 🎉