hi everyone, can you please help
i have random mint function and i need to eliminate the possibility of the same number appearing more than once. I'm trying to do it by iteration, and i know that bold part is in wrong place. I can't figure out how to make the code work in the right way
function mint() public payable {
require(msg.value == mintPrice, "Not enough ETH sent");
uint256 tokenId = _tokenIdCounter.current();
if (tokenId < 5) {
_tokenIdCounter.increment();
_safeMint(msg.sender, tokenId);
}
else {
_safeMint(msg.sender, _random(tokenId));
}
_owners[tokenId] = msg.sender;
}
function _random(uint tokenId) public returns (uint){
uint listLength = _existingTokenId.length;
bool alreadyExists = false;
for (uint i = 5; i if (_existingTokenId[i] == tokenId){
alreadyExists = true;
break;
}
if (!alreadyExists) {
_existingTokenId.push(tokenId);
}
}
return uint(
keccak256(abi.encodePacked(block.timestamp, block.difficulty, msg.sender))
) % tokenId;
}
thanks for answer
Nov 9, 2022, 3:57 PM
idea but expensive
- use Enumerable and mint all at launch to contract
- generate random number from 0 to balanceOf(this)
- use walletOf(this)[randNum] to get one of the random ones
if u can figure out how to do that without minting it would be cool :)
- use Enumerable and mint all at launch to contract
- generate random number from 0 to balanceOf(this)
- use walletOf(this)[randNum] to get one of the random ones
if u can figure out how to do that without minting it would be cool :)
Nov 9, 2022, 5:05 PM
https://github.com/chiru-labs/ERC721A
to save some funds
to save some funds
Nov 9, 2022, 5:07 PM
I also wonder why you are trying to mint random ids. Just curious. Also, depending on your use-case, the approach you are taking may not be the best in terms of security. So keep that in mind.
Nov 9, 2022, 5:08 PM
it would be very neat to be able to do that , can reveal on day -1 and everyone can see the art and get random one
the issue with looping to find a random unminted, it gets more expensive as more get minted , or can avoid loop and using full 32 bit rand uint256.. but that isnt the same as 0-10000 random
the issue with looping to find a random unminted, it gets more expensive as more get minted , or can avoid loop and using full 32 bit rand uint256.. but that isnt the same as 0-10000 random
Nov 9, 2022, 5:12 PM
i need to mint only 1 NFT by pressing mint function (
Nov 9, 2022, 5:13 PM
I have found that, unless you are paying for a platform like chainlink or you have a randomization oracle, if you want a cryptographically secure prng completely on-chain, you need two functions for the random number generation. You can either prompt the user for 2 consecutive transaction approvals or get the gas fees from the user and pay for the second one yourself.
But the random id is not necessary for that though
the ids may be secuential and that could be fine (depending on your use-case)
Nov 9, 2022, 5:15 PM
with chainlink vrf, can seed entire collection with one single random number
hash(traitseed, tokenId)
but i think for nft minting, crypto rand maybe not necessary
but maybe MEV NFT snipers who knows
hash(traitseed, tokenId)
but i think for nft minting, crypto rand maybe not necessary
but maybe MEV NFT snipers who knows
Nov 9, 2022, 5:15 PM