Are you getting the most out of your AWS investment? Get your free AWS Well-Architected Assessment.

2021 Fillmore Street #1128

}

24/7 solutions

polygon_logo_verify

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

AWS Think Big with Small Business Program’s Competitive Edge

AWS Think Big with Small Business Program’s Competitive Edge

The Amazon Cloud can help alleviate most issues involving transitioning the cloud. Businesses of any scope can hope to build solutions that are scalable and adaptable to their industry of work. Smaller or minority-owned businesses may still struggle to stand out among bigger companies or make an AWS environment as efficient as possible with fewer funds to spend. To alleviate these burdens, Amazon has the Think Big With Small Business program available through their Public Sector partnership program.

AWS Think Big for Small Business, Data Analytics, and Business Intelligence

AWS Think Big for Small Business, Data Analytics, and Business Intelligence

The AWS Think Big for Small Business Program is an outreach program designed to provide small and/or minority-owned public sector organizations support in the form of business intelligence, technical expertise, and marketing strategies. With cloud-based solutions and experience, various public institutions globally have seen continued success in government, educational, and nonprofit sectors. While the funding provided can help significantly to meet business objectives, the expertise on navigating the cloud and how to extend outwards towards customers is just as critical.

Best Practices for Using AWS Systems Manager

Best Practices for Using AWS Systems Manager

As newer users adopt the AWS cloud, it can be difficult to watch for issues and resolve them as needed. AWS Systems Manager grants better visibility over the AWS environment by clustering resources, providing legible and usable data based on performance, and actions to take that abide by AWS compliance requirements and best practices. This service provides everything management needs to evaluate the cloud and ensure continued functionality.