4

What is the correct way to deploy OpenZeppelin upgradable contracts? OpenZeppelin upgrdable contracts have default initialize() function which is automatically called by the hardhat-upgrades plugin.

There is no automatic call from hardhat-deploy, only post-upgrade method, e.g.:

const deployResult = await deploy('ContractX', {
    from: deployer,
    proxyContract: 'OpenZeppelinTransparentProxy',
    method: 'postUpgrade',
    args: []
  });

Should I deploy the contracts first through the scripts in the deploy folder and then call initialize() functions from some script outside deploy folder? Or is there a way to incorporate calling initialize() function from the deploy folder itself?

2

2 Answers 2

1
const deployResult = await deploy('ContractX', {
    from: deployer,
    proxy: {
        proxyContract: 'OpenZeppelinTransparentProxy',
        execute: {
            methodName: "initialize",
            args: []
        }
    }    
});
1
  • add more information about the answer please
    – Majd TL
    Sep 27, 2021 at 6:27
0

a more complex example:

import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { DeployFunction } from 'hardhat-deploy/types';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
  const { deployments, getNamedAccounts } = hre;
  const { deploy } = deployments;
  const { deployer, proxyAdminOwner } = await getNamedAccounts();

  const DataTypes = await deploy('DataTypes', {
    from: deployer,
  });

  const Errors = await deploy('Errors', {
    from: deployer,
  });

  const MintingLogic = await deploy('MintingLogic', {
    from: deployer,
  });

  const ValidationLogic = await deploy('ValidationLogic', {
    from: deployer,
  });

  await deploy('ContractName', {
    from: deployer,
    contract: 'ContractName',
    log: true,
    proxy: {
      owner: proxyAdminOwner,
      proxyContract: 'OptimizedTransparentProxy',
      execute: {
        init: {
          methodName: 'initialize',
          args: [],
        },
        onUpgrade: {
          methodName: 'postUpgrade',
          args: [],
        },
      },
    },
    libraries: {
      Errors: Errors.address,
      DataTypes: DataTypes.address,
      ValidationLogic: ValidationLogic.address,
      MintingLogic: MintingLogic.address,
    },
  });
};

export default func;
func.tags = ['ContractName'];

If you want more information don't hesitate to ask specific questions. There is too much to unpack here to explain it all

1
  • What are these methods? 'initialize'. 'postUpgrade'. Are they supposed to be on the implementation contract or proxy contract, what if you dont have a need for them? Nov 17, 2023 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.