How to initialize variables upon upgrade from Gnosis Safe

I want to deploy a v2 of a super simple smart contract where I add an additional state variable.

From my understanding, the way to do this is to create a new variable

uint myNewVar;

Then create a function like

function setMyNewVar(uint newVar) public {
  myNewVar = newVar;
}

Then I'd do something like:

const myNewVar = 5;
await upgradeProxy(contractA.address, 
        ContractB, {
        deployer, call: { fn: 'setMyNewVar', args: [myNewVar] },
});

The Problem:

I want to use OpenZeppelin's app within Gnosis safe to trigger my contract's upgrade. Is there any way to run setMyNewVar right after v2 gets deployed?

If not, how can I set new variables when doing upgrades through the Gnosis UI?

Or, am I missing something?

I'd prefer to make the owner of the contract a gnosis safe and not a single person (single point of failure).

Hi @itsgeorgep,

The OpenZeppelin app on Gnosis safe isn't actively maintained, and Defender is recommended for a more fully-featured alternative.

Although Defender doesn't support calling a function on upgrade yet (there is an issue here), you should be able to create a custom action in Defender Admin to invoke upgradeToAndCall() on your proxy. The data parameter of upgradeToAndCall() must be an encoded function call (you can use this tool to encode your function and arguments).