Truffle

松露遷移:部署或升級

  • December 18, 2021

我想在我的 truffle 項目的遷移腳本中編寫以下邏輯,並使用可升級的合約:

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

如果契約已經部署,我該如何處理或者如何簽入遷移腳本?

第一次安裝,

npm install @openzeppelin/truffle-upgrades

在您的 migrations/2_deploy_contracts.js 中,我不得不包裝一個 try-catch,因為 MyContract.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);    
}
}

引用自:https://ethereum.stackexchange.com/questions/93173