1

I have a very simple smart contract that mints NFTs on contract creation but the issue is it is not setting NFTs metadata. I have uploaded the metadata on ipfs. When I check metadata buy putting it in the brave browser it correctly shows up ipfs: ipfs://QmdbnwfBNE6ZekuppZnx9rk76sAmMDucnHP78UZzhwu4S3/metadata/6.json

{
  "image":"ipfs://QmU9L6jiskJL7bcuu7foAgb4VThoptzZ2ByTD7AGCBwcsr/images/6.png",
  "name":"Abstract art NFT #6",
  "description":"Abstract Art"
}

I deployed this smart contract on rinkeby test net

Smart contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Art is ERC721URIStorage, Ownable {
   using Counters for Counters.Counter;
   Counters.Counter private _tokenIds;

   constructor() ERC721("Art", "JL") {
       mintNFT(msg.sender,13);
   }

   function mintNFT(address recipient,uint256 _mintAmount)
       public onlyOwner
    //    returns (uint256)
   {    for (uint256 i = 1; i <= _mintAmount; i++) {
       _tokenIds.increment();

       uint256 newItemId = _tokenIds.current();
       _mint(recipient, newItemId);
       _setTokenURI(newItemId, "ipfs://QmdbnwfBNE6ZekuppZnx9rk76sAmMDucnHP78UZzhwu4S3/metadata/{i}.json");
    }

   }
}

Contract Address 0x14030c04aeA36F5c57610bF912b1f435aD232f2c

You can check the minted NFTs here: https://testnets.opensea.io/collection/art-wtudkep4ve

1 Answer 1

1

All your tokens have the same URI of "ipfs://QmdbnwfBNE6ZekuppZnx9rk76sAmMDucnHP78UZzhwu4S3/metadata/{i}.json"

Solidity does not replace your {i} placeholder with the value of i. You need to do it manually with string concatenation. Unfortunately, there is no easy way to do it in Solidity yet, so you'll need to use bytes.concat and casting uint to string (I recommend using import "@openzeppelin/contracts/utils/Strings.sol"; for this):

_setTokenURI(newItemId, string(bytes.concat(bytes("ipfs://QmdbnwfBNE6ZekuppZnx9rk76sAmMDucnHP78UZzhwu4S3/metadata/"), bytes(Strings.toString(i)), bytes(".json"))));
1
  • Thank you so much it worked like a charm Commented Mar 21, 2022 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.