薄荷合同/可靠性中的重复功能

Duplicated function in mint contract / solidity

我有不同的代码部分来调用 mint 函数。

零件是这个

2 个修饰符

modifier mintCompliance(uint256 _mintAmount) {
    require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
    require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
    _;
  }

    modifier mintCompliancePresale(uint256 _mintAmount) {
    require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
    require(supply.current() + _mintAmount <= maxPresaleSupply, "Max supply exceeded!");
    _;
  }

一个用于普通铸造,另一个用于预售铸造。

这与我的造币厂功能有关。

function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
      require(!paused, "The contract is paused!");
      require(msg.value >= cost * _mintAmount, "Insufficient funds!");

    if (msg.sender != owner()) {
        if(onlyWhitelisted == true) {
            require(isWhitelisted(msg.sender), "user is not whitelisted");
    }
    require(msg.value >= cost * _mintAmount, "insufficient funds");
    }

    _mintLoop(msg.sender, _mintAmount);
} 

    function presaleMint(uint256 _mintAmount) public payable mintCompliancePresale(_mintAmount) {
      require(!paused, "The contract is paused!");
      require(msg.value >= cost * _mintAmount, "Insufficient funds!");

    if (msg.sender != owner()) {
        if(onlyWhitelisted == true) {
            require(isWhitelisted(msg.sender), "user is not whitelisted");
    }
    require(msg.value >= cost * _mintAmount, "insufficient funds");
    }

    _mintLoop(msg.sender, _mintAmount);
} 

2 种功能,一种用于普通铸币,另一种用于预售铸币。

这与我的薄荷循环有关

 function _mintLoop(address _receiver, uint256 _mintAmount) internal {
    for (uint256 i = 0; i < _mintAmount; i++) {
      supply.increment();
      _safeMint(_receiver, supply.current());
    }
  }

现在,我的问题是如何在 1 个修改器和 1 个铸币函数中改进 2 个修改器和 2 个铸币函数?可以吗?

我认为 2 个修饰符和 2 个铸币函数没有意义。它有效,但我认为它里面有错误。

您可以创建 bool presale 并在修饰符内创建一个 if,该函数将是唯一的,因为两个函数是相同的:

modifier mintCompliance(uint256 _mintAmount) {
    if(!presale){
      require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
      require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
      _;
    }else{
      require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
      require(supply.current() + _mintAmount <= maxPresaleSupply, "Max supply exceeded!");
      _;
    }
  }

和功能:

function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
      require(!paused, "The contract is paused!");
      require(msg.value >= cost * _mintAmount, "Insufficient funds!");

    if (msg.sender != owner()) {
        if(onlyWhitelisted == true) {
            require(isWhitelisted(msg.sender), "user is not whitelisted");
    }
    require(msg.value >= cost * _mintAmount, "insufficient funds");
    }

    _mintLoop(msg.sender, _mintAmount);
} 

注意 如果您使用 openzeppelin 的 Pausable.sol,您可以这样做:

function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) whenNotPaused {}

并删除:

require(!paused, "The contract is paused!");