a
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 Graviton and Arm-architecture Processors

AWS Graviton and Arm-architecture Processors

AWS launched its new batch of Arm-based processors in 2018 with AWS Graviton. It is a series of server processors designed for Amazon EC2 virtual machines. The EC2 AI instances support web servers, caching fleets, distributed data centers, and containerized microservices. Arm architecture is gradually being rolled out to handle enterprise-grade utilities at scale. Graviton instances are popular for handling intense workloads in the cloud.

What is Tiered Pricing for Software as a Service?

What is Tiered Pricing for Software as a Service?

Tiered Pricing is a method used by many companies with subscription models. SaaS companies typically offer tiered pricing plans with different services and benefits at each price point with typically increasing benefits the more a customer pays. Striking a balance between what good rates are and the price can be difficult at times.

The Most Popular Cloud Cost Optimization Tools

The Most Popular Cloud Cost Optimization Tools

Cloud environments and their pricing models can be difficult to control. Cloud computing does not offer the best visibility and it is easy to lose track of which price control factors are having an impact on your budget. Having the right tools can help put value to parts of an environment and provide guides on how to better bring budgetary issues back under control.