Change Constant Value in a Logic Contract

I have an upgradable contract that inherits from ERC20BurnableUpgradeable and I want to change the value of one of its contants (NAME), something like this:

// Original Contract
contract Token is ERC20BurnableUpgradeable {
    string private constant NAME = "Token Name";
    string private constant SYMBOL = "TOKEN";
    uint8 private constant DECIMAL = 18;
    function initialize() public override initializer {
        super.initialize(NAME, SYMBOL, DECIMAL);
    }
}
// New version Contract
contract Token is ERC20BurnableUpgradeable {
    string private constant NAME = "NEW Token Name";
    string private constant SYMBOL = "TOKEN";
    uint8 private constant DECIMAL = 18;
    function initialize() public override initializer {
        super.initialize(NAME, SYMBOL, DECIMAL);
    }
}

I created a unit test to check it but the changes are not reflected when I call the name() function. No errors, I am just getting the original value.
So I added a new overrided name() function and it worked

// New version Contract
contract Token is ERC20BurnableUpgradeable {
    string private constant NAME = "NEW Token Name";
    string private constant SYMBOL = "TOKEN";
    uint8 private constant DECIMAL = 18;
    function initialize() public override initializer {
        super.initialize(NAME, SYMBOL, DECIMAL);
    }

    function name() override public view returns(string memory){
        return NAME;
    }
}

So the questions that comes to my mind are: is it the correct way to change the value of a Constant? , that could eopardize the storage layout?.

Thank you, I would really appreciate your opinion.

Hi Ivans! welcome to Open Zeppelin,

I am assuming that you are importing contracts too.

For example in this basic ERC20.sol from OZ, we can see how the function name() actually works.

   function name() public view virtual override returns (string memory) {
        return _name;
    }

This is using a different variable than string private constant NAME = "Token Name";

So that probably explains why when you updated it, the other name() function isn’t showing the updated variable.

I think the way you are doing it is correct. Use overrides!

Another way you could have done this is by changing the _name variable, but I think the way you did it is better.

Edit: As frangio says below, you’d have to change that too as it is by default private, so use overrides!

Note that this is not possible (by design) because _name is a private variable.

Using an override is good.

1 Like

Thank you very much guys @frangio @Tsushima_Yoshiko

1 Like