0

I created 13 images and corresponding JSON files uploaded the images to pinata then changed the file_url in the JSON file to cid of the image "ipfs://cid/image.png" uploaded the json file as well. changed the baseTokenURI to the JSON folder cid deployed the contract but when I go to opensea testnet the images don't show the nft's are there just with no image. also when I deploy I don't get any hex numbers (not sure if it matters ) I get this in my console.log

Contract deployed to: 0x95496e5139eaf7c33E46Ebea5256117F34366935
10 NFTs have been reserved
Owner has tokens:  [
  BigNumber { value: "0" },
  BigNumber { value: "1" },
  BigNumber { value: "2" },
  BigNumber { value: "3" },
  BigNumber { value: "4" },
  BigNumber { value: "5" },
  BigNumber { value: "6" },
  BigNumber { value: "7" },
  BigNumber { value: "8" },
  BigNumber { value: "9" },
  BigNumber { value: "10" },
  BigNumber { value: "11" },
  BigNumber { value: "12" }
]

and I get this when i go to opensea and click the token id this is the url i get:

https://opensea.mypinata.cloud/ipfs/QmShbRdeRoDybtMuwYrENZqnr7VJXYNexRTvR2aNwm7aQi/1

which doest work because its missing the json file extension. and i get this error

ipfs resolve -r /ipfs/QmShbRdeRoDybtMuwYrENZqnr7VJXYNexRTvR2aNwm7aQi/1: no link named "1" under QmShbRdeRoDybtMuwYrENZqnr7VJXYNexRTvR2aNwm7aQi
 - The root CID exists but the child file does not. - ERR_ID:00020

if add json in the end then it shows me the file

here is an example of a image JSON file

"name": "recreationalbuds #13",
  "description": "Cannabis NFT art collection",
  "image": "ipfs://QmUEcd1XbGmqDRJsRtFpqU8xPydUuVgRn4ec7USF9BdRQv/13.png",
  "custom_fields": {
    "dna": "177de32413b296699062a0b372b3dd4d37fbcb1d",
    "edition": 13,
    "date": 1649508989518,
    "compiler": "HashLips Art Engine"
  },
  "external_url": "https://recreationalbuds.com/",
  "attributes": [
    {
      "trait_type": "Background",
      "value": "BackgroundFive"
    },
    {
      "trait_type": "Base",
      "value": "Indica Base"
    },
    {
      "trait_type": "Shoes",
      "value": "ShellToesPink"
    },
    {
      "trait_type": "Mouth",
      "value": "Dino"
    },
    {
      "trait_type": "Eyes",
      "value": "DroopyBlue"
    },
    {
      "trait_type": "Hats",
      "value": "PotLeafBlue"
    },
    {
      "trait_type": "EyeWear",
      "value": "GlassesGreen"
    },
    {
      "trait_type": "Props",
      "value": "BlueBongTwo"
    },
    {
      "trait_type": "Blunt",
      "value": "Blunt"
    }
  ]
}

and here is my script

const { utils } = require("ethers");

async function main() {
  const baseTokenURI = 'ipfs://QmbUEAw5rW2Tpjx6DXJUgCvFLafWmnTET7DnP8Gg31Co8B/';

  // Get owner/deployer's wallet address
  const [owner] = await hre.ethers.getSigners();

  // Get contract that we want to deploy
  const contractFactory = await hre.ethers.getContractFactory("RecBuds");

  // Deploy contract with the correct constructor arguments
  const contract = await contractFactory.deploy(baseTokenURI);

  // Wait for this transaction to be mined
  await contract.deployed();

  // Get contract address
  console.log("Contract deployed to:", contract.address);

  // Reserve NFTs
  let txn = await contract.reserveNFTs();
  await txn.wait();
  console.log("10 NFTs have been reserved");

  // Mint 3 NFTs by sending 0.03 ether
  txn = await contract.mintNFTs(3, { value: utils.parseEther('0.03') });
  await txn.wait();

  // Get all token IDs of the owner
  let tokens = await contract.tokensOfOwner(owner.address);
  console.log("Owner has tokens: ", tokens);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

and here is my contract

pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
contract RecBuds is ERC721Enumerable, Ownable {
    using SafeMath for uint256;
    using Counters for Counters.Counter;
    
    Counters.Counter private _tokenIds;
    uint public constant MAX_SUPPLY = 8400;
    uint public constant PRICE = 0.01 ether;
    uint public constant MAX_PER_MINT = 5;
    
    string public baseTokenURI;
    constructor(string memory baseURI) ERC721("Recreational Buds", "RECBUDS") {
     setBaseURI(baseURI);
    }
    function reserveNFTs() public onlyOwner {
     uint totalMinted = _tokenIds.current();
     require(
        totalMinted.add(10) < MAX_SUPPLY, "Not enough NFTs"
     );
     for (uint i = 0; i < 10; i++) {
          _mintSingleNFT();
     }
    }
    function _baseURI() internal 
                    view 
                    virtual 
                    override 
                    returns (string memory) {
     return baseTokenURI;
    }
    
    function setBaseURI(string memory _baseTokenURI) public onlyOwner {
        baseTokenURI = _baseTokenURI;
    } 
    function mintNFTs(uint _count) public payable {
     uint totalMinted = _tokenIds.current();
     require(
       totalMinted.add(_count) <= MAX_SUPPLY, "Not enough NFTs!"
     );
     require(
       _count > 0 && _count <= MAX_PER_MINT, 
       "Cannot mint specified number of NFTs."
     );
     require(
       msg.value >= PRICE.mul(_count), 
       "Not enough ether to purchase NFTs."
     );
     for (uint i = 0; i < _count; i++) {
            _mintSingleNFT();
     }
    }
    function _mintSingleNFT() private {
      uint newTokenID = _tokenIds.current();
      _safeMint(msg.sender, newTokenID);
      _tokenIds.increment();
    }
    function tokensOfOwner(address _owner) 
         external 
         view 
         returns (uint[] memory) {
     uint tokenCount = balanceOf(_owner);
     uint[] memory tokensId = new uint256[](tokenCount);
     for (uint i = 0; i < tokenCount; i++) {
          tokensId[i] = tokenOfOwnerByIndex(_owner, i);
     }
     
     return tokensId;
  }
  function withdraw() public payable onlyOwner {
     uint balance = address(this).balance;
     require(balance > 0, "No ether left to withdraw");
     (bool success, ) = (msg.sender).call{value: balance}("");
     require(success, "Transfer failed.");
 }

}

and my hardhat.config

require("@nomiclabs/hardhat-waffle");
require('dotenv').config();

const { API_URL, PRIVATE_KEY } = process.env;

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.4",
  defaultNetwork: "rinkeby",
  networks: {
    rinkeby: {
      url: API_URL,
      accounts: [PRIVATE_KEY]
    }
  },
};

and my ipfs cid's enter image description here

1 Answer 1

0

just put this in smart contract tada its solved

import "@openzeppelin/contracts/utils/Strings.sol"; 


function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        _requireMinted(tokenId);

        string memory baseURI = _baseURI();
        return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, Strings.toString(tokenId),".json")) : "";
    }

2
  • This answer lacks explanation. How what you have done solves the problem ? Commented Aug 26, 2022 at 11:54
  • Ok, man so we are actually overriding the function which is supposed to return token URI so when ever anybody try retrieve token id it will return as expected like we have token ids in int format we are coverting them to string and then returing joint with json its isnt mentioned in opensea docs because they are4 using nft storage
    – baibars313
    Commented Aug 26, 2022 at 20:05

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.