NFT Metadata Not Showing Up After Minting with ERC721 Contract

I’m having trouble with my ERC721 contract. It’s supposed to mint an NFT when someone sends 100 Wei, but the metadata isn’t showing up properly.

Here’s a simplified version of my contract:

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract CryptoArt is ERC721URIStorage {
    uint256 private _tokenCounter;
    string private _baseTokenURI;

    constructor() ERC721("CryptoArt", "CART") {
        _baseTokenURI = "ipfs://QmExample/";
    }

    function createToken() public payable {
        require(msg.value == 100 wei, "Incorrect payment amount");
        uint256 newTokenId = _tokenCounter;
        _safeMint(msg.sender, newTokenId);
        _setTokenURI(newTokenId, string(abi.encodePacked(_baseTokenURI, "metadata.json")));
        _tokenCounter++;
    }
}

The metadata JSON looks fine, but when I check the minted NFT on OpenSea’s testnet, the image doesn’t show up. Any ideas what might be causing this? I’m pretty new to NFT development, so I might be missing something obvious.

heya Elias87! i’ve been messing around with NFTs too and ran into similar stuff. have you double-checked your IPFS setup? sometimes the issue is actually with how the metadata is stored rather than the contract itself.

also, I’m curious - what kind of art are you minting? is it generative or pre-made pieces? might be fun to hear more about your project!

oh, and quick thought - have you tried viewing the NFT on other marketplaces besides OpenSea? sometimes different platforms handle metadata differently. could be worth a shot to see if the problem is consistent across the board.

keep us posted on how it goes! NFT dev can be tricky but it’s super rewarding when you get it working :slight_smile:

yo elias, had the same headache. try checkin ur ipfs link - sometimes it takes ages to show up. also, make sure ur metadata.json is formatted right. OpenSea can be picky bout that stuff.

if nothin else works, maybe tweak ur _setTokenURI line. instead of “metadata.json”, use smthin like ${newTokenId}.json. helped me once.

I encountered a similar issue when I was working on my first NFT project. The problem might be with how you’re handling the tokenURI. Instead of setting it in the _setTokenURI function, try overriding the tokenURI function in your contract. Here’s how you can modify your code:

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
    return string(abi.encodePacked(_baseTokenURI, uint256(tokenId).toString(), ".json"));
}

This approach dynamically generates the URI for each token. Make sure your IPFS folder structure matches this pattern. Also, double-check that your IPFS gateway is working correctly and that the metadata.json file is accessible. Sometimes, it takes a while for IPFS to propagate the data, so give it some time before checking OpenSea again.