0

I would like to write the following logic in the migration script for my truffle project with upgradeable contracts:

if(contract is not yet deployed) {
   const mynewcontract = deployProxy(MyContract, {deployer});
} else {
   const myupgradedcontract = upgradeProxy(MyContract, {deployer});
}

How can I handle that or how can I check in the migration script if the contract has already been deployed?

1 Answer 1

0

First install,

npm install @openzeppelin/truffle-upgrades

In your migrations/2_deploy_contracts.js, I had to wrap around a try-catch because MyContract.deployed() was throwing an exception when the contract is not deployed.

const { deployProxy, upgradeProxy } = require('@openzeppelin/truffle-upgrades');
var MyContract = artifacts.require('./MyContract');
module.exports = async function(deployer) {
  try {
    var instance = await MyContract.deployed();
    var upgraded = await upgradeProxy(instance.address, MyContract, { deployer });
    console.log("Upgraded", upgraded.address);    
  } catch(error) {
    var instance = await deployProxy(MyContract, [], { deployer });
    console.log("Deployed", instance.address);    
}
}

Your Answer

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

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