How to upgrade contracts programmatically?

I see, and yeah, this approach will not work for you out of the box. The upgrades js library is just a library which does not interact with the files in the .openzeppelin folder, which track your deployed contracts. From the docs, emphasis mine:

The CLI does not just manage contract upgrades, but also compilation, interaction, and source code verification. The Upgrades library only takes care of creating and upgrading. The library also does not keep track of the contracts you have already deployed, nor runs any initializer or storage layout validations, as the CLI does. Nevertheless, these capabilities may be added to the Upgrades library in the near future.

Now, if you do want to programatically upgrade the contracts you have deployed via the CLI, probably the best option is to directly call the CLI from your code. You can use shelljs or child_process to call the CLI via your js code directly. You should set the OPENZEPPELIN_NON_INTERACTIVE env flag to prevent the CLI from making any interactive prompts while running on a script. Also, when running non-interactively, you'll have to manually call oz compile and oz push before every oz upgrade. The push command takes care of deploying your implementation contracts to the network. It is handled automatically for you when running the commands interactively, but not on a script.

There is another alternative which is importing the CLI as a dependency into your code, and use the code from the scripts folder as an entry point. However, this requires you to do some initialization beforehand, and it is not documented, so I would not recommend it.

3 Likes