I have taken the snippet of code below from a tutorial made in 2018 for making an ERC-20 contract.

However when I followed all the steps I got an error which I will share below.

Code Snippet:

pragma solidity ^0.8.9;
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
contract sampleToken is ERC20 
string public name = "ExampleToken";
string public symbol = "EGT";
uint public decimals = 18;
uint public INITIAL_SUPPLY = 10000 * (10 ** decimals);
constructor() public 
_totalSupply = INITIAL_SUPPLY;
_balances[msg.sender] = INITIAL_SUPPLY;


"message": "Undeclared identifier. Did you mean \"totalSupply"\?`
    "resource": "/Users/hassan/Desktop/Blockchain/blockchain/contracts/sampleToken.sol",
    "owner": "_generated_diagnostic_collection_name_#0",
    "severity": 8,
    "message": "Undeclared identifier.",
    "startLineNumber": 12,
    "startColumn": 1,
    "endLineNumber": 12,
    "endColumn": 10

Why is this code not valid anymore? How to fix it?

  • This is not a question about what breaking changes happened in Solidity. This is a specific issue related to how to work with the latest implementation of ERC20 provided by OpenZeppelin. Nov 4, 2021 at 6:54

1 Answer 1


_totalSupply is defined as a private storage variable in the ERC20 contract:

uint256 private _totalSupply;

Thus no contract, not even child contract like yours, can write directly to _totalSupply.

If you want to mint a specific amount of tokens in the constructor, you have to use the _mint function.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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