繁体   English   中英

复制 function in mint contract / solidity

[英]Duplicated function in mint contract / solidity

我有不同的代码部分来调用薄荷 function。

零件是这个

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);
} 

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

这与我的薄荷循环有关

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

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

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

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

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:

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!");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM