ERC721 how to set unique ipfs CID per minted token

I'm trying to understand smart contract development by developing a nft minting contract.

It's going pretty well. I've made a contract that can mint nft's, and a test frontend where users can press a mint button, which calls a mint function in the contract. That all works well.

The trouble is that I want each token to link to a different json uri, and I need to create them (and upload them to ipfs) before the minting starts. The software I'm using is not capable of producing the images on-chain.

My question is this: What would be the best way to connect each token with each json file? It's not difficult for me to make a json file with an array of all the ipfs CID's, but I have a hard time figuring out how to get them "into the contract". If I send one of them (selected by mint total count) together with the mint() call from the frontend, I risk sending two identical CID's, before the token gets minted and mint count is updated. I don't think I should keep a list of all the CID's in the contract, that would take a lot of space. So what then? How do people normally solve this?

Thanks in advance :slight_smile:

I'm looking a bit at the Events in solidity. It seems like a possibility to have the contract emit an event upon minting and listen for it in the frontend and then use the event id to create the metadata (if not already created) using a setURI() call.

It does seem a bit overkill to do it this way though.

I guess its also a possibility to store an array of id's mapped to hashes in the contract, and update it externally through a function call before the minting starts. But that would probably be a pretty pricey call.

There's also this technique described here: How to: ERC-1155 ID Substitution for Token URI - #6 by wighawag
But at the moment I'm using Pinata to upload the files, so I'm not sure i can add them into directories from there.

ok, I'm pretty sure I getting closer to my solution.
This thread seems to describe what I want: Why doesn't OpenZeppelin ERC721 contain setTokenURI? - #4 by frangio

1 Like

Right! You should be able to follow the approach described in that thread. Pinata allows you to upload the directory with your images, and will give you the IPFS link to use as baseURI.

Let us know how it goes!

1 Like