Minting an NFT
To mint an NFT, do the following:
-
Choose an image for your NFT. We will use the following image:
-
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). -
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 withQm...
. -
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 theipfs://
. You can also enter other values for thename
,description
, andexternal_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" }
-
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
. -
Create a file called
mint-nft.js
in thescripts
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; });
-
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