Updated on March 29, 2022
Deploy an NFT contract using Hardhat
This guide walks you through using Hardhat to deploy an NFT smart contract to the Palm network.
Prerequisites
- Access to PALM in order to pay the deployment transaction fees
- Access to a Palm Infura endpoint
- MetaMask wallet set up
-
Node.js installed
Note
The complete source code corresponding to this article is available for download
Steps
-
Initialize your npm project
npm init
-
Install Hardhat
npm install --save-dev hardhat
-
Create a HardHat Project
Inside your npm project run:
npx hardhat
Select “create an empty hardhat.config.js”.
-
Add project folders
In the root directory of your project:
mkdir contracts && mkdir scripts
-
Write your contract
We’ll use a contract based on the OpenZeppelin library’s ERC-721 implementation.
First, install the Open Zeppelin library in order to inherit its classes:
npm install @openzeppelin/contracts
Next, add the following smart contract to the “contracts” folder and name it “NFT.sol”:
//Contract based on [https://docs.openzeppelin.com/contracts/4.x/erc721](https://docs.openzeppelin.com/contracts/4.x/erc721) // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import "@openzeppelin/contracts/utils/Counters.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract NFT is ERC721URIStorage, Ownable { using Counters for Counters.Counter; Counters.Counter private _tokenIds; constructor() ERC721("NFT", "NFT") {} function mintNFT(address recipient, string memory tokenURI) public returns (uint256) { _tokenIds.increment(); uint256 newItemId = _tokenIds.current(); _safeMint(recipient, newItemId); _setTokenURI(newItemId, tokenURI); return newItemId; } }
-
Install an Ethereum library
In this example, we will use Ethers.js
npm install --save-dev @nomiclabs/hardhat-ethers [email protected]^5.0.0
-
Set environment variables with
dotenv
Install
dotenv
:npm install --save dotenv
Create a
.env
file in your project’s root folder, and set environment variables in the file as follows:PRIVATE_KEY = // The private key of the account you intend to use on the Palm Network INFURA_PROJECT_ID = // Your infura project id
Security warning
Keep your private keys secret.
Private keys must be kept secret and not committed to any code repository. In the example of this tutorial, the
.env
file should be added to your.gitignore
file and kept local.For more information, see MyCrypto’s Protecting Yourself and Your Funds guide.
-
Edit
hardhat.config.js
with the following text:/** * @type import('hardhat/config').HardhatUserConfig */ require('dotenv').config(); require("@nomiclabs/hardhat-ethers"); module.exports = { solidity: "0.8.6", settings: { optimizer: { enabled: true, runs: 1000000, }, }, mocha: { timeout: 90000 }, networks: { palm_testnet: { url: `https://palm-testnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`, accounts: [`0x` + process.env.PRIVATE_KEY], gasPrice: 1000 }, palm_mainnet: { url: `https://palm-mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`, accounts: [`0x` + process.env.PRIVATE_KEY], gasPrice: 1000 } } };
-
Compile your contract
To make sure everything is working so far, let’s compile our contract:
npx hardhat compile
-
Write a deploy script
Navigate to the scripts/ folder and create a new file called deploy.js, adding the following contents to it:
async function main() { const NFT = await ethers.getContractFactory("NFT"); // Start deployment, returning a promise that resolves to a contract object const Nft = await NFT.deploy(); console.log("Contract deployed to address:", Nft.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
-
Deploy to the target Palm network environment
npx hardhat run scripts/deploy.js --network palm_testnet
npx hardhat run scripts/deploy.js --network palm_mainnet
Contract deployed to address: 0xeC1AFf99d2C331B226A5731B9555Af924932d629
-
Look up your deployment on the Palm network block explorer
Go to https://explorer.palm-uat.xyz for testnet deployments and to https://explorer.palm.io for mainnet deployments and paste the address of your contract into the search bar.
Information
The next logical step would be to mint an NFT with the contract you just deployed, you can learn how to do this in How to mint an NFT on the Palm network using Hardhat
Question
Any question? Drop them on our Discord