Skip to main content
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 take a look at the complete code in the Hedera-Code-Snippets repository.

Prerequisites


Table of Contents

  1. Create and Compile the Solidity Contract
  2. Deploying the Smart Contract and Minting a Token
  3. Fixing Permissions, Redeploying, and Minting
  4. Pausing the ContractPausing the Contract
  5. Transferring NFTs
  6. 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).
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.
📚 Learn more from the official Hardhat documentation.

Step 1: Create and Compile the Solidity Contract

Create a new Solidity file named MyTokenAdvanced.sol in your contracts directory, and paste this Solidity code:
contracts/MyTokenAdvanced.sol
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
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:
Here’s the output of the command:
Copy the contract address of your newly deployed contract. Next, create mint-advanced.ts in your scripts folder:
scripts/mint-advanced.ts
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:
‼️ Notice minting fails due to incorrect permissions. Let’s fix this in the next step.

Step 3: Fixing Permissions, Redeploying, and Minting

Update the minting role to your deployer account by modifying the following line of code in your deploy-advanced.ts script:
scripts/deploy-advanced.ts
Now that the deployer account has all the roles, redeploy the contract:
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:
The new token will be minted with token ID 0 and the corresponding token URI is printed to your terminal.

Step 4: Pausing the Contract

Create a new pause-advanced.ts script and make sure to replace the contractAddress variable with your address:
scripts/pause-advanced.ts
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:
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.

Step 5: Transferring NFTs

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
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:
If the balance for the**0x5FbDB2315678afecb367f032d93F642f64180aa3**account shows 1 , then you’ve successfully transferred the NFT and completed this tutorial! 🎉

Step 6: Run tests(Optional)

You can find both types of tests in the Hedera-Code-Snippets repository. You will find the following files:
  • contracts/MyTokenAdvanced.t.sol
  • test/MyTokenAdvanced.ts
Copy these files and then run the tests:
You can also run tests individually with either of these

Additional Resources

Writer: Michiel, DevRel Engineer

Editor: Luis, Sr Software Developer

Editor: Krystal, Senior DX Engineer

Editor: Kiran, Developer Advocate