hedera-hashgraphhashgraph

How do I mint multiple NFTs on Hedera calling TokenMintTransaction only once?


I want to mint 100 NFTs. Do I need to call "TokenMintTransaction()" 100 times? Is there some way to mint 100 NFTs in a single API call?

Here's the code I'm using:

let mintTx = new TokenMintTransaction().setTokenId(tokenId).setMetadata([Buffer.from(CID)]).freezeWith(client);

What can I pass in .setMetadata() to mint multiple NFTs?


Solution

  • Yes, it is possible to mint multiple NFT's in a single TokenMintTransaction() but there are things to note.

    A transaction has a max size of 6,144 kb including signatures.

    .setMetadata() accepts an array of NFT metadata. Each array element will result in an NFT with a unique serial number being created under the token type. The receipt for the transaction will include all the new serial numbers that were created by the transaction.

    It is important to keep an eye on your metadata size because if you put too many items in the array, you will receive a TRANSACTION_OVERSIZE error and no NFT's will be minted.

    Here is an example for minting multiple NFT's in a single transaction:

    let metadata = [];
    let CID = [];
    for (let i = 0; i < 3; i++) {
      // NFT STORAGE
      const fileName = `LEAF${i + 1}.jpg`;
      metadata[i] = await nftClient.store({
        name: `LEAF${i + 1}`,
        description: "Leaf NFT.",
        image: new File([await fs.promises.readFile(fileName)], fileName, { type: "image/jpg" }),
      });
    
      CID[i] = Buffer.from(metadata[i].url);
    
      // IPFS URI FOR NFT METADATA - See HIP-412: https://hips.hedera.com/hip/hip-412
      let ipfsBaseUrl = "https://ipfs.io/ipfs/";
      let ipfsGatewayLink = ipfsBaseUrl + metadata[i].ipnft + "/metadata.json";
      console.log(`- IPFS link for serial ${i + 1}: ${ipfsGatewayLink} \n`);
    }
    
    // MINT NEW BATCH OF NFTs
    let mintTx = new TokenMintTransaction().setTokenId(tokenId).setMetadata(CID).freezeWith(client);
    let mintTxSign = await mintTx.sign(operatorKey);
    let mintTxSubmit = await mintTxSign.execute(client);
    let mintRec = await mintTxSubmit.getRecord(client);
    
    console.log(`- Minting fees: ${mintRec.transactionFee._valueInTinybar.c[0] * 1e-8} hbar \n`);
    
    

    While creating 100 NFT's at a time may not be possible, you should be able to do tens at a time (depending on the metadata size).