Documentation Index
Fetch the complete documentation index at: https://hedera-0c6e0218-update-sourcify-migration-issue-508-luke.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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
Note: This tutorial is currently supported only in the Getting Started JavaScript series and is not available for other languages.
Prerequisites
Before you begin, you should have completed the following tutorial:Table of Contents
- Step 1: Project Setup
- Step 2: Creating the ERC-721 Contract
- Step 3: Deploy Your ERC-721 Smart Contract
- Step 4: Minting an ERC-721 Token
- Step 5: Verify Your Smart Contract with Hardhat
Step 1: Project Setup
Initialize Project
Set up your project by initializing the hardhat project:🚧 What's new: Hardhat 2 → 3
🚧 What's new: Hardhat 2 → 3
Key differences in Hardhat 3:
-
compile → build
npx hardhat compileis nownpx hardhat build. This is the big one. The v3 migration guide explicitly shows using thebuildtask. -
project init switch
v2 commonly usednpx hardhatornpx hardhat initto bootstrap. In v3 it’snpx hardhat --init. -
keystore helper commands are new
v3’s recommended flow includes a keystore plugin with commands likenpx hardhat keystore set HEDERA_RPC_URLandnpx 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.
Install Dependencies
Next, install the required dependencies:When you set up your keystore for the first time, you’ll be asked to create a keystore password. Save this password securely because you’ll need it anytime you set or reset a variable.
HEDERA_RPC_URL, we’ll have https://testnet.hashio.io/api
HEDERA_PRIVATE_KEY, enter the HEX Encoded Private Key for your ECDSA account from the Hedera Portal.
Note
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
Update yourhardhat.config.ts file in the root directory 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.
hardhat.config.ts
ctrl + c twice.
We won’t be using ignition and we will be removing the default contracts that come with the Hardhat default project, so we will remove all the unnecessary directories and files first:
Step 2: Creating the ERC-721 Contract
Create a new Solidity file (MyToken.sol) in our contracts directory:
contracts/MyToken.sol
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:
Step 3: Deploy Your ERC-721 Smart Contract
Create a deployment script (deploy.ts) in scripts directory:
scripts/deploy.ts
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:
Copy the deployed address. You’ll need this in subsequent steps.
Step 4: Minting an ERC-721 Token
Create amint.ts script in your scripts directory to mint an NFT. Don’t forget to replace the <your-contract-address> with the address you’ve just copied.
scripts/mint.ts
deployer.address ). Then we verify the balance to see if we own an ERC-721 token of type MyToken.
Mint an NFT:
Step 5: Verify Your Smart Contract with Hardhat
After deploying your smart contract, you can verify the source code programmatically from Hardhat. Programmatic verification is the most reliable and efficient method, especially for complex or upgradeable contracts, because Hardhat already knows your exact compilation settings, dependency graph, and deployment artifacts. Hardhat submits the verification request to Sourcify, which natively supports Hedera Mainnet (chain ID295) and Testnet (chain ID 296). Once Sourcify accepts the match, the verified status surfaces automatically on HashScan and any other explorer that reads from Sourcify.
Install the Verification Plugin
Hardhat’s official@nomicfoundation/hardhat-verify plugin supports Sourcify out of the box.
Install the plugin in your project:
Configure Hardhat for Verification
Import the plugin in yourhardhat.config.ts file and enable Sourcify verification. Because Hedera is supported on the default Sourcify server (https://sourcify.dev/server), no custom apiUrl is needed.
Run the Verification Command
The plugin adds theverify task to Hardhat. The basic command structure is:
Example: Verifying Your MyToken Contract
Using the contract address from your deployment, pass the deployer address (theinitialOwner constructor argument) at the end of the command:
| Issue | Solution |
|---|---|
| Verification Fails/Mismatch | Ensure your local compilation settings (Solidity version, optimizer runs, viaIR) exactly match the settings used for deployment. Run npx hardhat clean && npx hardhat build before retrying. |
| Constructor Arguments Error | If your contract has constructor arguments, pass them as positional arguments after the contract address in the verification command. |
| Hardhat Keystore Password Prompt | The task may prompt for your Hardhat keystore password if it needs to sign a transaction to read deployment details. Enter it when prompted. |
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