ERC20Capped: Immutable Variables cannot be read during contract creation time

Hello @grimmdev, and thanks for your message.

We should have anticipated this issue :cry:
The thing is, ERC20Capped adds an extra check in the _mint function that verifies you are not exceeding the cap. For efficiency reasons, we made the cap immutable. This removes a load operation when doing the check, and thus reduces the gas cost of minting. The downside, as you show, is that this immutable variable cannot be read from during construction, which means you cannot use ERC20Capped’s _mint protection during the construction.

The good news is that the “non protected” ERC20._mint remains available!

contract GLDToken is ERC20Capped {
    constructor(uint256 initialSupply) ERC20("Gold", "GLD") ERC20Capped(1000*10**18) {
        ERC20._mint(msg.sender, initialSupply);
    }
}

Note that, while this works well, it doesn’t verify that initialSupply <= 1000*10**18 so you might want to add this check in the constructor.

3 Likes