Deploying an Upgradeable ERC721 Collection to Polygon Network with Hardhat

Deploying an Upgradeable ERC721 Collection to Polygon Network with Hardhat

ERC721 is a popular standard for creating non-fungible tokens (NFTs) on the Ethereum network. However, deploying a smart contract to the Ethereum mainnet can be expensive due to the high gas fees. Fortunately, Polygon (previously known as Matic) provides a scalable and low-cost alternative to the Ethereum network. In this article, we'll learn how to deploy an ERC721Upgradeable collection to the Polygon network using OpenZeppelin-Contracts-Upgradeable and Hardhat. We'll also show you how to mint and deploy your NFTs to OpenSea.

Step 1: Setting up the Environment

To begin, you need to set up your development environment. We'll be using Hardhat as our development environment, which allows us to test and deploy our smart contracts easily.

Start by creating a new project directory in your preferred location and initializing it with npm.

mkdir my-nft-project cd my-nft-project npm init -y 

Next, install the required dependencies, including the OpenZeppelin-Contracts-Upgradeable, Hardhat, and Hardhat Network.

npm install --save-dev @openzeppelin/contracts-upgradeable hardhat hardhat-ethers hardhat-deploy hardhat-deploy-ethers 

Step 2: Writing the Smart Contract

Next, let's create the smart contract for our ERC721Upgradeable collection. We'll be using the OpenZeppelin-Contracts-Upgradeable library, which provides a secure and reliable implementation of the ERC721 standard.

Create a new file called MyNFT.sol in your project directory and add the following code:

SPDX-License-Identifier: MIT 

pragma solidity ^0.8.0; 

import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol"; 
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol"; 
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; 
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; 

contract MyNFT is Initializable, ERC721Upgradeable, ERC721URIStorageUpgradeable, OwnableUpgradeable { 
  function initialize() public initializer { 
    __ERC721_init("MyNFT", "NFT"); 
    __ERC721URIStorage_init(); 
    __Ownable_init(); 
  } 

  function mint(address to, uint256 tokenId, string memory tokenURI) public onlyOwner { 
    _mint(to, tokenId); _setTokenURI(tokenId, tokenURI); 
  } 
} 

This contract extends the ERC721Upgradeable contract and includes the ERC721URIStorageUpgradeable, which allows us to store the NFT metadata on the blockchain. It also includes the OwnableUpgradeable contract, which allows the contract owner to mint new NFTs.

Step 3: Writing the Deployment Script

Now that we have our smart contract, we need to write the deployment script to deploy it to the Polygon network.

To deploy our ERC721Upgradeable contract, we will need to make some adjustments to our deployment script. First, we need to add the Polygon mainnet to our hardhat.config.js file:

module.exports = { 
  networks: { 
    polygon: { 
      url: `https://polygon-mainnet.infura.io/v3/${INFURA_PROJECT_ID}, 
      accounts: [`0x${PRIVATE_KEY}`], 
      gas: 2000000, gasPrice: 1000000000, 
    }, 
  }, 
  solidity: { 
    version: "0.8.4", 
    settings: { 
      optimizer: { 
        enabled: true, 
        runs: 200, 
      }, 
    }, 
  }, 
}; 

Next, we will create a new deployment script called deployPolygon.js and update the existing script deploy.js to deploy to the Ethereum network.

Here is an example of how deployPolygon.js should look like:

const { ethers, upgrades } = require("hardhat"); 

async function main() { 
  const [deployer] = await ethers.getSigners(); 
  console.log("Deploying contracts with the account:", deployer.address); 
  const MyNFT = await ethers.getContractFactory("MyNFT"); 
  const myNFT = await upgrades.deployProxy(MyNFT, [], { initializer: "initialize" }); 
  await myNFT.deployed(); 
  console.log("MyNFT deployed to:", myNFT.address); 
} 

main()
  .then(() => process.exit(0)) 
  .catch((error) => { 
    console.error(error); 
    process.exit(1); }
  ); 

In the script above, we are using upgrades.deployProxy from the @openzeppelin/hardhat-upgrades plugin to deploy our contract. This function deploys a proxy contract that enables us to upgrade our contract in the future without losing data.

We can now run this script to deploy our contract to the Polygon network:

npx hardhat run --network polygon scripts/deployPolygon.js 

Once the deployment is complete, we can verify our contract on the Polygon network using the following command:

npx hardhat verify --network polygon DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1" 

We will need to replace DEPLOYED_CONTRACT_ADDRESS with the address of our deployed contract and "Constructor argument 1" with the value we passed to our contract constructor during deployment.

Finally

We can list our NFT collection on OpenSea by following these steps:

  1. Go to the OpenSea website and create an account.

  2. Click on "My Profile" and then click "Create".

  3. Fill in the required details and choose "Polygon" as the blockchain.

  4. Enter the contract address of our deployed ERC721Upgradeable contract and click "Continue".

  5. Customize the appearance of our NFT collection page and click "Create".

Our NFT collection is now available for purchase on OpenSea!

Сonclusion

Deploying an ERC721Upgradeable contract to the Polygon network with launch on OpenSea is a straightforward process that requires us to make some adjustments to our deployment script and use the appropriate network configuration. With the help of tools like @openzeppelin/hardhat-upgrades and hardhat-ethers, we can easily deploy and manage our smart contracts.

< Our development centers >