简体   繁体   English

复制 function in mint contract / solidity

[英]Duplicated function in mint contract / solidity

i have differents parts of code to call a mint function.我有不同的代码部分来调用薄荷 function。

the parts are this零件是这个

2 modifiers 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!");
    _;
  }

One is for normal mint, and the other one is for presale minting only.一种用于普通铸币,另一种仅用于预售铸币。

And that connects with my mint functions.这与我的造币厂功能有关。

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 functions, one for normal mint and another one for presale minting.两种功能,一种用于普通铸币,另一种用于预售铸币。

and that connects with my mint loop这与我的薄荷循环有关

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

Now, my question is how can i improve the 2 modifiers and 2 mint functions in just 1 modifier and 1 minting function?现在,我的问题是如何在 1 个修改器和 1 个铸造 function 中改进 2 个修改器和 2 个铸造函数? Is it possible to do?有可能吗?

I dont think 2 modifiers and 2 minting functions have sense.我认为 2 个修饰符和 2 个铸币函数没有意义。 It works, but i think there is an error inside it.它有效,但我认为它里面有一个错误。

you can create bool presale and make an if inside the modifier, the function will be unique since both function are the same:您可以创建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!");
      _;
    }
  }

and function:和 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);
} 

NOTE If you're using Pausable.sol by openzeppelin you can do:注意如果您使用 openzeppelin 的 Pausable.sol,您可以执行以下操作:

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

and delete:并删除:

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

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

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