Deploy a Smart Contract with Hardhat
Deploying a Contract Using Hardhat Scripts
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.
What you will accomplish
By the end of this tutorial, you will be able to:
Compile and deploy a smart contract using Hardhat
Interact with a smart contract using Hardhat
Prerequisites
Before you begin, you should have completed the following tutorial:
Step 1: Project Setup
Initialize Project
Set up your Node.js project by initializing npm:
npm init -y
Install Dependencies
Next, install the required dependencies:
npm install @openzeppelin/contracts dotenv
We also need a bunch of developer dependencies to be able to deploy and interact with smart contracts through Hardhat:
npm install --save-dev @nomicfoundation/hardhat-toolbox
Create .env
File
.env
FileTo create your .env
file, you can run the below command:
touch .env
Securely store your sensitive data like the OPERATOR_KEY
in a .env
file. For the JSON RPC_URL
, we'll use the Hashio RPC endpoint for testnet.
OPERATOR_KEY=your-operator-key
RPC_URL=https://testnet.hashio.io/api
Replace the your-operator-key
environment variable wiht the HEX Encoded Private Key for your ECDSA account from the Hedera Portal.
Please note: that Hashio is 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 the Hiero JSON-RPC Relay.
Configure Hardhat
Create a hardhat.config.js
file in the root 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. Don't forget to import the dotenv
package to load environment variables and the Hardhat Toolbox package to be able to use Hardhat scripts.
require("dotenv").config();
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: {
version: "0.8.22",
},
defaultNetwork: "testnet",
networks: {
testnet: {
// HashIO RPC testnet endpoint in the .env file
url: process.env.RPC_URL,
// Your ECDSA account private key pulled from the .env file
accounts: [process.env.OPERATOR_KEY],
}
}
};
You can verify the connection by running:
npx hardhat console --network testnet
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.
Step 2: Creating the ERC-721 Contract
Create a new Solidity file (erc-721.sol
) in a new contracts
directory:
// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.22;
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:
npx hardhat compile
This command will generate the smart contract artifacts, including the ABI. We are now ready to deploy the smart contract.
Step 3: Deploy Your ERC-721 Smart Contract
Create a deployment script (deploy.js
) in a new scripts
directory:
async function main() {
// Get the signer of the tx and address for minting the token
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts 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);
console.log("Contract deployed at:", contract.target);
}
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:
npx hardhat run scripts/deploy.js --network testnet
Copy the deployed address—you'll need this in subsequent steps.
The output looks like this:

Step 4: Minting an ERC-721 Token
Create a mint.js
script in your scripts
directory to mint an ERC-721 token. Don't forget to replace the <your-contract-address>
with the address you've just copied.
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 = await MyToken.attach(contractAddress);
// Mint a token to ourselves
const mintTx = await contract.safeMint(deployer.address);
const receipt = await mintTx.wait();
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:
npx hardhat run scripts/mint.js --network testnet
Expected output:
Minted token ID: 0x0000000000000000000000000000000000000000000000000000000000000000 // Represents ID 0
Balance: 1 NFT
Congratulations! 🎉 You have successfully learned how to deploy an ERC-721 smart contract using Hardhat, OpenZeppelin, and Ethers. Feel free to reach out in Discord!
Next Steps
Learn how to add Access Control, Pause, and Transfer ERC-721 tokens
Check out OpenZeppelin ERC-721 Documentation
See the full code in the Hedera-Code-Snippets Repository
Last updated
Was this helpful?