Generate NFTs with this simple code - Solidity question

Hello,
I’m posting a followup question on this topic, because I think I know where the problem is, in the smart contract.

Background: I have gone through this youtube tutorial and successfully ran the sample javascript code to generate the metadata for 100 images and successfully upload to IPFS. Then moving onto the final step of writing and deploying the smart contract to generate 100 NFTs in my collection.

I am utilizing the sample code provided in the tutorial for a basic solidity contract, and I modified the constructor with a for loop to mint 100 NFTs as shown below.

My questions:

  • is the code in the constructor correct?
  • why does the constructor have {id} being passed in, but seems that parameter id isn’t being used.

thanks in advance.

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

//import 1155 token contract from Openzeppelin
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";

// Example contract to be deployed via https://remix.ethereum.org/ for testing purposes.

contract NFTContract is ERC1155, Ownable {
    using SafeMath for uint256;

    constructor()
        ERC1155(
            "ipfs://QmWQviBuBkheeFTmDCH8bVn5wHGqq8L5a6hbZ6EEUCkw8y/metadata/{id}.json" 
        )
    {
        // account, token_id, number
        for (uint i = 1; i < 101; i++) {
            _mint(msg.sender, i, 1, "");    
        }
    }
}

Hi that’s fine. You can see the list of metadata links for all the tokens here.

{id} is ID substitution so OpenSea, etc. will put the token ID in there and pad it with zeros in order to find the right link for a particular token ID. This is part of the ERC1155 specification.

Unfortunately this smart contract does not work… I deployed this again (have tried several times), and STILL many of the tokens are broken, and are NOT showing up correctly.

The metadata is correct, You can see the list of metadata links for all the tokens here

But after deploying this smart contract, something breaks…

See the collection on OpenSea here.

As shown, many of them are “blank” or broken. Something is going wrong during smartcontract deployment, and I can’t figure it out.

Questions:

  • Does this NFTContract sample code from the tutorial actually supposed to work as is?
  • Or are we required to put in some additional code to make it work?
  • Has anyone else had success following the tutorial steps? Or having similar issues?

Any suggestions or help, esp from Moralis, would be greatly appreciated! (else I’m afraid I’ll have to abandon this project altogether and start from scratch following another path not relying on the Moralis approach.)

Thanks.

Update more info: the token_uri is getting corrupted.

Using the Web3 API in Moralis, to get the NFT with the given id.

Below is the response from this Web3 API which shows something is very wrong. Any ideas??

Example Token #26 is broken one. See the token_uri below, points to a non-existent file: 1a.json (but should point to 26.json)

{
  "token_address": "0xb5acc2f83e52a9df19e26074c4e5ac757c378234",
  "token_id": "26",
  "owner_of": "0xca8fa6409513e2662cc1eda61b5854232c4061b0",
  "block_number": "26243093",
  "block_number_minted": "26243093",
  "token_hash": "af84bed6dd946d35a04b88b788a7accc",
  "amount": "1",
  "contract_type": "ERC1155",
  "name": "TulipManiacs - Collection",
  "symbol": "TulipManiacs",
  "token_uri": "https://ipfs.moralis.io:2053/ipfs/QmWQviBuBkheeFTmDCH8bVn5wHGqq8L5a6hbZ6EEUCkw8y/metadata/000000000000000000000000000000000000000000000000000000000000001a.json",
  "metadata": null,
  "synced_at": "2022-05-07T17:16:18.576Z"
}

Unfortunately this keeps happening, I have tried deploying the same contract code numerous times, and always the NFT collection gets corrupted. I am suspecting something is wrong with openzeppelin, perhaps I should not use that…are there any alternatives?

Thanks

1a is 26 in hexadecimal. So you may have to change the IPFS generation to use hexadecimal for the tokenId in the URL (looking at the id format required in the standard).

@glad Yes! that was a brilliant catch! TYVM. 1a in hex = 26 in decimal. That was the issue, there was a bug in the tutorial code.

As suggested, I’ve changed the IPFS generation javascript to use hexadecimal in the URL filename, and now it all works. The contract was deployed successfully. The collection is available on OpeanSea testnet here… and there’s no broken tokens.

All good now to deploy in production. Cheers.

Wonderful. I guess the issue was that the tutorial only statically minted a few NFTs as a demonstration and didn’t run into that eventual problem.