When somebody use my mint button i want him to receive a random NFT from this transaction.
When i use mint button (mintAmount = 2) my NFT's metadata's will be like,
ipfs://image_link/1.json
ipfs://image_link/2.json
But i dont want this. I want to receive my NFT's randomly. Like this,
ipfs://image_link/578.json
ipfs://image_link/877.json
I hope i was clear. How can i do that?
1 Like
Hi, welcome to OpenZeppelin.
You have to implement a random feature that picks an id between number of NFTs.
Where is the code you are working with?
Hey thanks for reply im using one of the hashlips repositories. Here is the code: https://github.com/HashLips/solidity_smart_contracts/blob/main/contracts/NFT/NFT.sol
How can i implement random feature. I have no idea at all... Can you PLEASE help me about that? I really want to learn how to do this and its important to me.
1 Like
you can have a public function that state the exact id you want or randomize it and can be generated onClick.
something like:
function mintRandomly() public{
uint tokenID = /*randomly generate an ID that is within the totalSupply*/
_safeMint(to, tokenID)
}
Randomness in smart contracts is difficult to achieve. There is a lot written about this, even in this forum. Please search the forum and the web for "NFT randomization" or similar.
Here's a popular thread for you to read:
Hi,
There is a pattern in many PFP projects where the tokenURI does something like:
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())) : "";
}
Where the baseURI is some IPFS directory.
This is the standard implementation from Ope…