Can we initialize our contracts again when upgrading?

Hey Guys,

Lets say I want to upgrade my contract and change parameters that was first initialized using initializer in the first deploy.

Can I write another init function in my new upgrade contract logic that updates the initialized values in the implementation contract?

Maybe you can give an example.

If I have a contract like initStaking(address _rewardsContract). Now, I would want to change _rewardsContract address on upgrade.

In the upgrade Can I have a function such as initStaking2(addres _rewardsContract) so that I can again call init when Upgrading?

Maybe you can have a check another modifier reinitializer, it can be used like this:

contract MyToken is ERC20Upgradeable {
    function initialize() initializer public {
        __ERC20_init("MyToken", "MTK");
    }
}
contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
    function initializeV2() reinitializer(2) public {
        __ERC20Permit_init("MyToken");
    }
}
1 Like

Can we have a initializer that can make changes to variables that was in initialize at first.
Eg:
initStaking(address _rewardsContract) initializer
initStaking2(addres _rewardsContract) reinitializer(2)

Yes, I think so, you can have a test on your local environment.