【问题标题】:How to random mint a static number of NFT?如何随机铸造一个静态数量的 NFT?
【发布时间】: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


【解决方案1】:

首先不要使用block.timestamp或任何区块或区块链数据作为随机性来源,因为它会导致“随机性”可预测或可能被矿工操纵,尝试使用chainlink作为随机性来源,他们在他们的文档中有一个很好的例子,如果你想固定供应每种类型的代币,你可以有 3 个变量来了解每种代币的铸造量,以及何时获得随机数和你需要的一切您只需要应用一些数学运算,在这种情况下,您希望代币是 20% 的超级代币、30% 的稀有代币和 50% 的普通代币,您只需要进行数学运算来决定将铸造哪个代币,并且如果这种类型已经达到最大供应量,会发生什么?

【讨论】:

  • 是的,我知道随机问题和 Chainlink,但它也增加了这个项目的成本,这并不是真正需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2022-08-12
  • 2022-08-12
  • 2022-01-17
  • 2022-07-05
  • 2022-01-11
相关资源
最近更新 更多