Skip to content
You are reading Palm development version documentation and some displayed features may not be available in the stable release. You can switch to stable version using the version box at screen bottom.

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.



  1. Initialize your npm project

    npm init
  2. Install Hardhat

    npm install --save-dev hardhat
  3. Create a HardHat Project

    Inside your npm project run:

    npx hardhat

    Select “create an empty hardhat.config.js”.

  4. Add project folders

    In the root directory of your project:

    mkdir contracts && mkdir scripts
  5. 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 [](
    // 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)
            returns (uint256)
            uint256 newItemId = _tokenIds.current();
            _safeMint(recipient, newItemId);
            _setTokenURI(newItemId, tokenURI);
            return newItemId;
  6. Install an Ethereum library

    In this example, we will use Ethers.js

    npm install --save-dev @nomiclabs/hardhat-ethers [email protected]^5.0.0
  7. 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.

  8. Edit hardhat.config.js with the following text:

    * @type import('hardhat/config').HardhatUserConfig
    module.exports = {
        solidity: "0.8.6",
        settings: {
            optimizer: {
                enabled: true,
                runs: 1000000,
        mocha: {
            timeout: 90000
        networks: {
            palm_testnet: {
                url: `${process.env.INFURA_PROJECT_ID}`,
                accounts: [`0x` + process.env.PRIVATE_KEY],
                gasPrice: 1000
            palm_mainnet: {
                url: `${process.env.INFURA_PROJECT_ID}`,
                accounts: [`0x` + process.env.PRIVATE_KEY],
                gasPrice: 1000
  9. Compile your contract

    To make sure everything is working so far, let’s compile our contract:

    npx hardhat compile
  10. 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);
        .then(() => process.exit(0))
        .catch((error) => {
  11. 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
  12. Look up your deployment on the Palm network block explorer

    Go to for testnet deployments and to for mainnet deployments and paste the address of your contract into the search bar.


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


Any question? Drop them on our Discord

Questions or feedback? You can discuss and obtain community support on Palm Discord.
For additional support, contact Palm on Palm website.