Minting an NFT

To mint an NFT, do the following:

  1. Choose an image for your NFT. We will use the following image:

    Winter School NFT #0

  2. Create a free account in Pinata and upload your image there.

    Note: We will need to specify an image location when we mint our NFT. This image will be displayed by NFT explorer websites like OpenSea. While we could upload an image to a regular website and use that URL, the image will become inaccessible if the website goes down. Or someone could change the image located at that URL leading to undesirable effects.
    The convention in the NFT community is to upload the image to the Inter Planetary File System (IPFS). IPFS is a peer-to-peer network for storing files. IPFS using content addressing to identify files. This means that a cryptographic hash of a file's contents identify the file. Such file identifiers are called Content Identifiers (CIDs).

  3. Copy your image's CID. It will look like QmcssgBH1SeDTmmEw2N6JqzTzfNebo5tCLbzuk3gxfiuk6. For example, the image we will be using is available on the Pinata interface at https://gateway.pinata.cloud/ipfs/QmcssgBH1SeDTmmEw2N6JqzTzfNebo5tCLbzuk3gxfiuk6. The CID corresponds to the alphanumeric string starting with Qm....

  4. Create a NFT metadata JSON file in the following format. In the value corresponding to the image key, enter the CID of your image after the ipfs://. You can also enter other values for the name, description, and external_url keys.

    {
        "name": "ACM Winter School Attendee #0",
        "description": "NFT given to an attendee of the 2022 ACM Winter School on Topics in Digital Trust, organized by Trust Lab, IIT Bombay",
        "external_url": "https://trustlab.iitb.ac.in/event/winter-school-2022",
        "image": "ipfs://QmcssgBH1SeDTmmEw2N6JqzTzfNebo5tCLbzuk3gxfiuk6"
    }
    
  5. Upload the metadata JSON file to Pinata and get its CID. For example, the above file is at location https://gateway.pinata.cloud/ipfs/QmfHMf1Qe5o9TRW1HgSoGqcFNosKLmBprbvH4T3SM3w5Hy. The CID corresponds to QmfHMf1Qe5o9TRW1HgSoGqcFNosKLmBprbvH4T3SM3w5Hy.

  6. Create a file called mint-nft.js in the scripts directory with the following content.

    const hre = require("hardhat");
    const API_KEY = process.env.API_KEY;
    const privateKey = process.env.PRIVATE_KEY;
    const contractAddress = process.env.CONTRACT_ADDRESS;
    
    
    // Define an Alchemy Provider
    const provider = new hre.ethers.providers.AlchemyProvider('goerli', API_KEY)
    
    // Get contract ABI file
    const contract = require("../artifacts/contracts/MyNFT.sol/WinterSchoolNFT.json");
    
    // Create a signer
    const signer = new hre.ethers.Wallet(privateKey, provider)
    
    // Get contract ABI and address
    const abi = contract.abi
    
    // Create a contract instance
    const myNftContract = new hre.ethers.Contract(contractAddress, abi, signer)
    
    async function main() {
        const args = process.argv;
        if (args.length != 3) {
            console.log("Provide an IPFS hash of the NFT metadata as an argument")
            process.exit(0);
        }
        const ipfsHash = args[2];
        const tokenUri = "ipfs://" + ipfsHash;
    
        console.log("Minting NFT...");
        let nftTxn = await myNftContract.mintNFT(signer.address, tokenUri)
        await nftTxn.wait()
        console.log(`NFT Minted! Check it out at: https://goerli.etherscan.io/tx/${nftTxn.hash}`)
    }
        
    main().catch((error) => {
        console.error(error);
        process.exitCode = 1;
    });
    
  7. Run the following command with the <metadata-ipfs-hash> value replaced with your metadata file's CID. Don't use the image CID.

    node scripts/mint-nft.js <metadata-ipfs-hash>
    

    After about a half a minute, you should see a message saying

    NFT Minted! Check it out at: https://goerli.etherscan.io/tx/0xa9da090c5de3eb59e649cc04dd362bd4d09f454c8a37197d107f1603e4c910f5