Search code examples
blockchainsolidity

How can I randomly mint NFTs with a given number of uploaded json files?


What I would like to achieve:

Assuming I have 500 JSON files already uploaded to IPFS, each one represents a meta data of an NFT.

Every time a user triggers mintBatch(number) function, I wish a number of NFTs would be minted randomly for this user, and the NFTs that have already been minted should not be minted again.

Hence, I think I will need a place to store the IDs of the NFTs that have been minted, so that the future randomize function can avoid those IDs when minting.

I am thinking if I should do this on-chain or off-chain. If off-chain, things can be a lot easier as I just need to upload the number randomly generated off-chain and record them in my off-chain database. But some articles say it would be costly and low efficient (which I don't quite understand the reason). On the other hand, on-chain was described on some other articles that has less security as users can see the randomize mechanic on blockchain and try to hack it, but some other articles say it is more efficient and will genenrate less gas.

Normally which way would people choose, and normally how do they do it?

UPDATE: To prevent users from minting the same json that others have minted, I am thinking of putting json CIDs in an array, and removing the minted item from it once a mint function is called. I wonder if this would change the suitable way for minting randomly.


Solution

  • Check this question: How to generate a random number in solidity?

    Only true number generation is only possible with using RNG services like chainlink.

    After get the random number from chainlink, you can pick one of your json files and give it to user.