0

Open Zeppelins mintable token has the method mint(address _to, uint256 _amount) ... returns (bool) to create new tokens. I'd like to introduce a supply limit and am unsure how to validate the input correctly. Should I use require(), throw, or is it preferred to return a boolean as the caller might expect?

What are the consequences of these options?

function mint(address _to, uint256 _amount) onlyOwner canMint public returns (bool) {
    // TODO: validate amount is not too big.

    // Validate with require():
    // require((totalSupply + _amount) <= SUPPLY_LIMIT);

    // Or validate with if:
    // if ((totalSupply + _amount) > SUPPLY_LIMIT) return false;

    // Or throw?
    // if ((totalSupply + _amount) > SUPPLY_LIMIT) throw;

    totalSupply = totalSupply.add(_amount);
    if (totalSupply == SUPPLY_LIMIT) mintingFinished = true;

    balances[_to] = balances[_to].add(_amount);
    Mint(_to, _amount);
    Transfer(0x0, _to, _amount);

    assert(totalSupply <= SUPPLY_LIMIT);

    return true;
}

The calling contract looks like this:

contract Crowdsale {

    // ...

    function buyTokens(address beneficiary) public payable {
        require(beneficiary != 0x0);
        require(validPurchase());

        // ...
        token.mint(beneficiary, tokens); // return value not used
        // ...
  }
}
0

I'd like to introduce a supply limit and am unsure how to validate the input correctly. Should I use require(), throw, or is it preferred to return a boolean as the caller might expect

Its pretty straight forward that the method returns bool and I would suggest to use if and return bool(state is defined either true or false)

with respect to require/ throw it would return "Invalid Opcode" and also it utilizes equal gas as above and would return undefined sate (Invalid Opcode/ not much user friendly) which is not preferred. Moreoften require is used when you change state of variable which is not the case here.

1
  • Accepted as soution since this directly answers my question. Thanks!
    – haggis
    Nov 11 '17 at 18:52
0

Actually, you shouldn't be checking that inside the mint() function of the token.

If you are using Open Zeppelin, you have the canMint modifier in there that allows minting as long as mintingFinished is false.

So, the best approach, at least in my opinion and following patterns in other contracts would be to check that tokens are not minted past the hard cap in the buyTokens() function.

Later, when the crowdsale is finished you should call token.finishMinting() to stop you from being able to mint new tokens after the crowdsale.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.