Immutable variables

Hello Everyone,

I am on this video when we extending our ERC 20 contract on the Solidity 201 course with ERC20Capped.sol file from OpenZeppelin: https://academy.ivanontech.com/lessons/extending-erc20

When I would like to compile I am getting the following error ( haven’t met this with yet) :

TypeError: Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
  --> project:/node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol:26:16:
   |
26 |         return _cap;
   |                ^^^^

Compilation failed. See above.

I am trying to find the solution meanwhile, but if someone can point me how to solve this, that would be appreciated.

Thank you.

Okay this is solved already I just had to add ERC20._mint before to the _mint function in the contract.

1 Like

Hey there!

ERC20._mint works, but I am wondering why the contract lets me mint more tokens than the cap (limited supply of the tokens)?

Here:
image

I tried to copy/paste the require statement from the ERC20Capped contract, but it shows an error, the same as Riki posted:

image

I found a solution:

image

If I just remove immutable in ERC20Capped contract - it works fine:

image

Here it shows: “ERC20Capped: cap exceeded”

1 Like