我如何在 ERC721 中设置 2 个最大供应量

How can i set 2 max supply's in a ERC721

我有问题。问题是我想要白名单销售和 public 销售的最大薄荷供应。例如; 我总共有 10.000 个 NFT。将进行白名单销售和 public 销售。对于白名单销售,我有 2.000 个钱包地址,但我只希望它们能够铸造 1.500 个 NFT。在 public 销售中,我希望出售剩余的 8.500 NFT。

我已经用正常的 maxsupply 尝试了一些东西,但无法弄清楚。我想要每个钱包的最大铸币量,例如 10,并且似乎不能仅将白名单的供应量限制为 1500

谁能给我解释一下或有代码示例吗?

  1. 有一个whitelistMaxSupply
  2. 具有getMaxSupply功能
  3. 具有finishWhitelist功能
  4. 有一个maxSupply

除非白名单完成,否则getMaxSupply会returnwhitelistMaxSupply,完成后会returnmaxSupply

  1. 利润

有很多方法可以解决你的问题,这只是我想到的第一个

function getMaxSupply() view public returns(uint256){
  if(whitelistFinished){
    return maxSupply;
  }
    return whitelistMaxSupply;
}

function finishWhitelist() public{
   whitelistFinished = true;
}


我不建议限制你的白名单 per-wallet 铸币量(你想铸币,不是吗?!),但我理解你可能这样做的原因。我会提供这两种选择。为了简洁起见,所有代码都进行了缩写:

*注 1:我们将使用 OpenZeppelin Utilities - Counters 来跟踪铸造进度。您也可以考虑使用 totalSupply(),但是,如果销毁代币是一个问题,totalSupply() 会减少并丢弃您的计数,而计数器不会。

注意 2:假设您在 public 之前出现在白名单中,并且您也没有兼顾储备金数量——为此需要额外的检查和计数器。

注3:这里只包括限制白名单的检查;您显然还需要额外检查有效的白名单帐户、足够的付款等。

限制白名单总供应量

...
import "@openzeppelin/contracts/utils/Counters.sol";
...
error ExceededWhitelistSupply();
...
using Counters for Counters.Counter;
uint256 public maxSupply = 10000;
uint256 public maxWhitelistSupply = 1500;
Counters.Counter private totalWhitelistSupply;
...
function mintWhitelist(uint256 _qty) external payable {
  if ( totalWhitelistSupply.current() + _qty > maxWhitelistSupply ) revert ExceededWhitelistSupply();

  for (uint256 i = 0; i < _qty; i++) {
    totalWhitelistSupply.increment();
  }

  _mint(msg.sender, _qty, '', true);
}

限制钱包&&限制白名单总供应量

...
import "@openzeppelin/contracts/utils/Counters.sol";
...
error ExceededWhitelistSupply();
error ExceededMaxPerWallet();
...
using Counters for Counters.Counter;
uint256 public maxSupply = 10000;
uint256 public maxWhitelistSupply = 1500;
uint256 public maxWhitelistPerWallet = 10;
Counters.Counter private totalWhitelistSupply;

mapping(address => uint256) public whitelistMintedAmount;
...
function mintWhitelist(uint256 _qty) external payable {
  if ( whitelistMintedAmount[msg.sender] + _qty > maxWhitelistPerWallet ) revert ExceededMaxPerWallet();
  if ( totalWhitelistSupply.current() + _qty > maxWhitelistSupply ) revert ExceededWhitelistSupply();

  for (uint256 i = 0; i < _qty; i++) {
    totalWhitelistSupply.increment();
  }

  whitelistMintedAmount[msg.sender] += _qty;
  _mint(msg.sender, _qty, '', true);
}

在这里,我们使用了映射 - good tutorial here - to track the number of NFTs that have been minted to this wallet (preferred method, as this won't be fooled by the account transferring NFTs out of the wallet and then minting more). If you want to go WAY down the rabbit hole, you can also look at trash-canning this whole approach and learn up on this approach 来处理您的白名单。

请记住,您需要添加更多的检查和平衡(例如,在 dApp 的前端将其可视化以避免在它们不应该的时候进行铸造,额外的验证层在你的薄荷功能等),但这应该为你提供限制最大钱包和最大供应量所需的核心部分。对于任何代码错误,我深表歉意 - 这是我的第一个 Whosebug 答案,代码的 short-handing 和可读性有点难以检查错误。