Upgrade Contract by adding new parameter to initialize()

Hi, I am having an issue upgrading my contract using upgradeProxy when I adding a new parameter.
I am using truffle to do migration using upgradeProxy:

await upgradeProxy(existing.address, BoxV3, { deployer });

How do I add an extra parameter to BoxV3? the expectation is as below:

Before upgrade

    function initialize( USDToken _usdToken) public {
        usdToken = _usdToken;
    }

After upgrade

  function initialize( USDToken _usdToken, SGDToken _sgdToken) public {
        usdToken = _usdToken;
        sgdToken = _sgdToken
    }

Thanks in advance.

1 Like

Have you tried this? What error did you get?

Hi Frangio, thank you for helping. as upgradeProxy does not provide any args as parameter like deployProxy, i tried to deploy the BoxV3 using deployProxy before I pass it into upgradeProxy but also not working

Here is my code:

 const x = await deployProxy(BoxV3, [USDToken.address,SGDToken.address], { deployer, initializer: 'initialize' });
    await upgradeProxy(existing.address, x, { deployer });

I was wondering is that any reference to add extra parameter into initialize function when upgrade the contract like from 1 parameter into 2.

Ok, the thing is when you use upgradeProxy you’re changing the implementation of a contract that has already been initialized. So calling the initializer wouldn’t make sense. It would definitely make sense to call a function that “migrates” to the new version and initializes only the new state variables. There is nothing currently in the plugin to help you with that, although we do want to add it soon:

In the meantime you have to define a function and call it manually.

2 Likes

Just following up here - it would seem that based off the discussion in #62 there is a clear direction forward, but I may be missing it.

I have a Transparent Proxy and would like to do some form of an upgradeToAndCall, but am using (and would like to continue to use) OZ Upgrades with hardhat.

What would be the recommended way to handle that?

Hi! Just went through this same process and asked the same questions myself. Here is how I did it.

So you've got to the point where you want to call upgradeToAndCall on the ProxyAdmin, providing your newly created "migration function".
You're still going to want to use the upgrades.upgradeProxy function, but notice the third parameter is of type "UpgradeOptions" which looks like {call?: { fn: string; args?: unknown[] } | string}.
Notice if your migration function takes params, you can supply an object with your function name and arguments.

Hope this helps!

2 Likes

Thanks for sharing @Bridgerz. This is actually a relatively recent feature so it wasn't available before, but it's exactly what others in this thread would have needed.

1 Like