Solidity – Best Practices for Storing Metadata on IPFS for Non-Deterministic Number of NFTs

erc-721ipfssolidity

I am making an contract that allows for unlimited minting of NFTs (they will be for identification purposes, not necessarily collecting). But since the number of NFTs, and therefore corresponding metadata files, will be nondeterministic, it does not seem possible to set the URI to a single ipfs:// address at time of launch.

Some solutions I was considering are:

  • Creating a mapping for token => CID which gets updated as part of the minting function. Client logic would be in charge of fetching and providing the CID. But it seems wasteful to store all those strings.
  • Using a hosted IPFS gateway. Lots of NFTs do this right now, but it seems to defeat the purpose of using IPFS in the first place since I can change the pointers if it's my own gateway.

Has anyone tackled this problem before? Any help appreciated.

Best Answer

You could use IPNS to have dynamic URI ending with an ID. Usually in the NFT contract the tokenURI function is implemented like this:

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

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