Hi, following the question and given example
ragma solidity >=0.4.22 <0.9.0;
contract BoxV1 {
uint256 private a;
uint256 private b;
function initialize() public initializer {
// Something meaningful here
}
function store(uint256 _a, uint256 _b) public {
a = _a;
b = _b;
}
function add() public view returns (uint256) {
return a + b;
}
}
contract BoxV2 {
uint256 private a;
uint256 private b;
uint256 private c;
function initializeV2(uint256 _c) public initializerV2(?) {
c = _c;
}
function store(uint256 _a, uint256 _b) public {
a = _a;
b = _b;
}
function add() public view returns (uint256) {
return a + b + c;
}
}
As state is stored in the proxy, if when you upgrade, you need to initialize state in an upgrade function, then you would need to call the upgrade function for each proxy.
Please note you should protect the upgrade function so that it can only be called once, and if it isn’t called in the same transaction as the upgrade, then it also needs to be protected so that only you can call it.
There is an upgradeToAndCall function in the AdminUpgradeabilityProxy, used in upgrades plugins. I assume correct way to upgrade to BoxV2 in the example above is (please correct me if I'm wrong):
- Deploy V2
- Call upgradeToAndCall on the proxy, pass address from v2 and packed
initialize
with argument c in data (question: do I need to also makeinitializerV2
modifier? Because in general,_initialized
already set to true in the proxy storage)
Unfortunately, I struggle to find examples of the usage. It seems upgrade and call is not supported by hardhat plugin. In basic example upgrade proxy method was used, which doesn't have any arguments to pass additional data. Do I need to manually call this method?