【发布时间】:2021-12-01 18:54:53
【问题描述】:
我想铸造这些数量的代币:
200 超级 300 稀有 500个普通
但是铸造过程需要是随机的,你可以得到一个(超级、稀有或普通),但在过程结束时,应该铸造相同数量的 200 超级、300 稀有和 500 普通。
下面的代码是随机的,但是最终的token数量会和开头不同:
function safeMint(address to) public onlyOwner {
require(_tokenIdCounter.current() < totalSupply(), "There's no token to mint.");
require(mintCnt[msg.sender] < maxMintCntPerAddress, "One address can mint 1 tickets.");
if(mintPrice > 0) {
require(mintPrice == msg.value, "Mint price is not correct.");
address payable _to = payable(serviceAddress);
_to.transfer(mintPrice);
}
uint randomNumber = random(expectedTokenSupply - _tokenIdCounter.current());
for (uint256 i = 0; i < _tokenMetadata.length; i++) {
if(_tokenMetadata[i].amount <= randomNumber) {
_safeMint(to, _tokenIdCounter.current());
_setTokenURI(_tokenIdCounter.current(), _tokenMetadata[i].uri);
_tokenIdCounter.increment();
break;
}
}
}
function random(uint maxValue) internal returns (uint) {
return uint(keccak256(abi.encodePacked(block.timestamp, msg.sender, _tokenIdCounter.current()))) % maxValue;
}
【问题讨论】:
-
什么决定了铸造代币是什么——超级、稀有还是普通?
标签: ethereum solidity smartcontracts nft