Putting a cap to mint (mintable contract provided by wizard)

I was playing on the wizard.openzeppelin.com, and ended up with this for a simple mint-able token (till ownership is renounced):

  contract MyToken is ERC20, ERC20Burnable, Ownable, ERC20Permit {
      constructor(address initialOwner)
          ERC20("MyToken", "MTK")
          Ownable(initialOwner)
          ERC20Permit("MyToken")
      {}
 
      function mint(address to, uint256 amount) public onlyOwner {
          _mint(to, amount);
      }
  }

I want to know, that if I want to place a maximum supply cap of 1 million tokens, will the following be the correct addition?

uint256 private constant cap = 1000000 * 10**18;

And the mint function becomes:

    function mint(address to, uint256 amount) public onlyOwner {
        require(totalSupply() + amount <= cap, "Cap exceeded");
        _mint(to, amount);
    }

Or am I missing something?

You may as well mint the entire cap to yourself (initialOwner) upon the deployment of your contract.

Then you don't even need to include the mint function in your contract to begin with.

In other words:

contract MyToken is ERC20, ERC20Burnable, Ownable, ERC20Permit {
    constructor(address initialOwner)
    ERC20("MyToken", "MTK")
    Ownable(initialOwner)
    ERC20Permit("MyToken") {
        _mint(initialOwner, 1000000 * 10 ** decimals());
    }
}
1 Like

Thanks a bunch for the reply! Problem with that is, I don't want to show all tokens in circulation, as minting will allow me to keep it as a variable.

So I'm curious if I'm doing it right. What do you think?

That would reflect a transparent and decentralized paradigm.

Your current implementation reflects the opposite of that (an opaque and centralized paradigm).

1 Like

Thanks for your input! Hmmm, in that case, I’d not need Ownable either, because it’s only being used for minting.