Running a Startup on AWS? Get Funding With AWS JumpStart. Click Here to Learn More

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

Here’s Why You Should Work with an AWS Partner

Here’s Why You Should Work with an AWS Partner

Amazon Web Services is understandably a difficult platform to adapt to and utilize fully upon first getting started. Some organizations can be selected to become certified partners to indirectly extend services to help build on the Amazon Cloud. Finding a certified company to help build out is undoubtedly the best way to significantly simplify, streamline, and reduce the cost of utilizing AWS.

Amazon Web Services – CodeCatalyst

Amazon Web Services – CodeCatalyst

When a development team is building out an application, it helps to have access to the same resources, have the tools for planning and testing, and to have access to the application all in one place. CodeCatalyst comes with a slew of continuous integration/continuous development (CI/CD) tools and can leverage other AWS services and be connected to other AWS projects on an account. As a collaborative tool, it is easy to introduce new members into the project and to log all activity or all tests from a single dashboard. It’s a complete package of all the tools needed to securely work on every step of an application’s lifecycle.

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.