[SOLVED] 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

when trying to inherit from ERC20Capped, Iā€™m getting this error.
following the tutorial in the academy:

this is my code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../node_modules/@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC20Capped, Ownable {
    constructor() ERC20("TesT", "TT") ERC20Capped(1000000) {
        _mint(msg.sender, 10000);
    }
}

error:
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.

1 Like

Maybe this helps: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2580

3 Likes

Hey @acsalameh, hope you are well.

Basically its an issue from the different solidity version that we used in the course and your contract, also the openzeppelin template is different.

Now to avoid that issue as @cryptokid mentioned with the link, you can just remove the first mint from the contructor, and mint later (creating another function that you will use to mint your firsts tokens), or just use ERC20._mint() instead.

Carlos Z

2 Likes

yes i did add ERC20._mint and it worked well, thank you @cryptokid and @thecil