2021 Fillmore Street #1128

}

24/7 solutions

Top 10 Cloud Providers

How to Deploy an NFT to the Mumbai Test Network 3/4

This is part 3 in a 4 step tutorial to minting NFTs on Polygon where we'll deploy an NFT to the Mumbai test network for Polygon.

Part 3 of 4 - Deploying an NFT

First, we’ll need a deployment script, let’s create a deploy-script.js in the scripts directory.

const hre = require("hardhat");

async function main() {

    const NFT = await hre.ethers.getContractFactory("AllCodeNFT");
    const nft = await NFT.deploy();

    await nft.deployed();

    console.log("AllCode NFT deployed to:", nft.address);
}

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

This script is pretty simple. Using Hardhat, we grab the AllCodeNFT, which we’ve already compiled. We stick in the compiled contract into the NFT object. Next, we deploy the AllCodeNFT to the appropriate network. Last, we wait for the NFT contract to be deployed. Once, it’s deployed we print out the address of the contract.

Next, we’ve got to make changes to get our hardhat.config.js to point back to Mumbai. We have to revert our module.exports to their previous state.

module.exports = {
  defaultNetwork: "matic",
  networks: {
    hardhat: {
    },
    matic: {
      url: "https://rpc-mumbai.maticvigil.com",
      accounts: [PRIVATE_KEY]
    }
  },

Next, we’ll run a command to invoke the deployment script, and specify the Mumbai network

 npx hardhat run scripts/deploy-script.js --network matic

The output will be the contract address.

  AllCode NFT deployed to: 0xD931d7bAA004A1DA25bc6E877E6f669cB8559219

So the contract is deployed to Mumbai, which is great, but now we need to mint the tokens. We’ll mint the tokens with a different piece of javascript, let’s call it mint-script.js

  const hre = require("hardhat");

  async function main() {
    const NFT = await hre.ethers.getContractFactory("AllCodeNFT");

    //The IPFS Address of your image that you uploaded to Pinata
    const URI = "ipfs://QmVH5T7MFVU52hTfQdWvu73iFPEF3jizuGfyVLccTmBCX2"

    //Your Wallet Address that contains Matic whose private key you put into .gitignore
    const WALLET_ADDRESS = "0x44f2b515211953d5f07038be619D58a91accB8E7"

    //The AllCodeNFT contract address that you deployed above
    const CONTRACT_ADDRESS = "0xD931d7bAA004A1DA25bc6E877E6f669cB8559219"

    const contract = NFT.attach(CONTRACT_ADDRESS);
    await contract.mint(WALLET_ADDRESS, URI);
    console.log("NFT minted:", contract);
  }

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

Again, we snag the compiled asset from the Contract Factory. Next, we specify some metadata for the ipfs portion of the contract. We also specify the wallet address that we’re using and the contract address output from the previous javascript invocation. We associate the NFT contract with the address, and we call mint. Once we’re done minting, then we dump the contract to the console.log.

Let’s mint a contract by invoking the javascript command from the command line.

  npx hardhat run scripts/mint-script.js --network matic

The output will look like definition for the contract because we dumped the contract to console.log().


  NFT minted: Contract {
  interface: Interface {
    fragments: [
      [ConstructorFragment], [EventFragment],
      [EventFragment],       [EventFragment],
      [EventFragment],       [FunctionFragment],
      [FunctionFragment],    [FunctionFragment],
      [FunctionFragment],    [FunctionFragment],
      [FunctionFragment],    [FunctionFragment],
      [FunctionFragment],    [FunctionFragment],
      [FunctionFragment],    [FunctionFragment],
      [FunctionFragment],    [FunctionFragment],
      [FunctionFragment],    [FunctionFragment],
      [FunctionFragment],    [FunctionFragment]
    ],
    _abiCoder: AbiCoder { coerceFunc: null },
    functions: {
      'approve(address,uint256)': [FunctionFragment],
      'balanceOf(address)': [FunctionFragment],
      'getApproved(uint256)': [FunctionFragment],
      'isApprovedForAll(address,address)': [FunctionFragment],
      'mint(address,string)': [FunctionFragment],
      'name()': [FunctionFragment],
      'owner()': [FunctionFragment],
      'ownerOf(uint256)': [FunctionFragment],
      'renounceOwnership()': [FunctionFragment],
      'safeTransferFrom(address,address,uint256)': [FunctionFragment],
      'safeTransferFrom(address,address,uint256,bytes)': [FunctionFragment],
      'setApprovalForAll(address,bool)': [FunctionFragment],
      'supportsInterface(bytes4)': [FunctionFragment],
      'symbol()': [FunctionFragment],
      'tokenURI(uint256)': [FunctionFragment],
      'transferFrom(address,address,uint256)': [FunctionFragment],
      'transferOwnership(address)': [FunctionFragment]
    },

Next, we’ll ask for the first minted object in contract to demonstrate that the URI is populated correctly. We’ll create a new javascript function entitled get-uri-script.js.

const hre = require("hardhat");

async function main() {
    const NFT = await hre.ethers.getContractFactory("AllCodeNFT");
    const CONTRACT_ADDRESS = "0xD931d7bAA004A1DA25bc6E877E6f669cB8559219"
    const contract = NFT.attach(CONTRACT_ADDRESS);

    const owner = await contract.ownerOf(1);
    console.log("Owner:", owner);

    const uri = await contract.tokenURI(1);
    console.log("URI: ", uri);
}
main().then(() => process.exit(0)).catch(error => {
    console.error(error);
    process.exit(1);
});

The output should be

Owner: 0x44f2b515211953d5f07038be619D58a91accB8E7
URI:  ipfs://QmVH5T7MFVU52hTfQdWvu73iFPEF3jizuGfyVLccTmBCX2

That’s it. We minted a unique token to the Polygon Mumbai blockchain with the URI to our Pinata asset.

You can go to the blockchain to see it here.

https://mumbai.polygonscan.com/address/0x44f2b515211953d5f07038be619D58a91accB8E7#tokentxnsErc721

allcode_admin
allcode_admin

Related Articles

What is Tigera?

What is Tigera?

An AWS Advanced Technology Partner, Tigera delivers Calico and Calico Enterprise for security and networking on EKS, both of which are AWS Containers Competency certified.

Vezt

Vezt

Blockchain technology has the potential to be a windfall for musicians, filmmakers, and video game developers. With the advent of new technology, the way we consume entertainment is changing. Vezt assists artists in distributing their tracks on digital channels and in promoting their work.

Top Platforms for NFTs: Polygon and Flow

Top Platforms for NFTs: Polygon and Flow

As blockchain technology continues to develop, we are seeing an increase in the number of platforms, languages, and applications. Your works of art and collectables can now be presented and traded in the form of non-fungible tokens (NFTs), which are a relatively new development.

Free AWS Services List

Download this FREE list of all 200+ AWS services and ensure that you're using the optimal services for your use case to enhance efficiency and save money!

Free AWS Business Continuity Plan Template

Make sure you have the proper business continuity plan explicitly for your AWS infrastructure. Our team of experts built this template using AWS Best Practices so you can ensure it's built to scale! 

Free Cloud Migration Checklist

Without the proper cloud migration strategy, you risk losing time and money. Ensure that your migration process is running smoothly with our FREE cloud migration checklist.

Free AWS Services List

You might be optimizing with the wrong AWS services. Download this FREE list of all 200+ AWS services and ensure that you're using the optimal services for your use case to enhance efficiency and save money!

Download your FREE AWS Business Continuity Plan Template
Download Free 200+ AWS Services Checklist
Download our 10-Step Cloud Migration ChecklistYou'll get direct access to our full-length guide on Google Docs. From here, you will be able to make a copy, download the content, and share it with your team.