Im updating some ERC20 contact code from solidity 0.4.0 to 0.6.0 and hitting some snags as it seems openzeppeling has changed its structure since then as can be expected.

I cant access the balances variable from the ERC20 contract to update the owners balance in the contructor as the OZ ERC20 balances variable is now set to private as is the _totalSupply variable - how do I set these values in the parent OZ contracts or am I doing this all wrong

    pragma solidity <=0.6.0;

    import "openzeppelin-solidity/contracts/math/SafeMath.sol";
    import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

    contract Token is ERC20 {

        string public name = "test";
        string public symbol = "TST";
        uint8 public decimals = 18;
        uint256 public initSup = 1000;

        constructor() public {
            _totalSupply = initSup;
            balances[msg.sender] = initSup;


1 Answer 1


Figured it out.

Check the comments here relating to _mint() https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/token/ERC20/ERC20.sol

and also here: https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226

Starting with OpenZeppelin v2 this pattern is not only discouraged, but disallowed. The variables totalSupply and balances are now private implementation details of ERC20, and you can’t directly write to them. Instead, there is an internal _mint function that will do exactly this.

  • 1
    Yes, that's exactly it! Make sure to also check out the docs for ERC20: docs.openzeppelin.org/v2.3.0/api/token/erc20#erc20 Quick question, did you find this by googling, searching the forum directly, or some other way? Let us know if there's anything we could do to make this clearer
    – frangio
    Jun 6, 2019 at 15:11
  • No its perfectly visible if you keep reading through all documentation. Maybe if you had a section about upgrading legacy contracts from each major version or something.
    – Kravitz
    Jun 8, 2019 at 12:14

Your Answer

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

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