From 7910fb38237726d35841f27ef202741bf7d9ef17 Mon Sep 17 00:00:00 2001 From: aalavandhan1984 Date: Fri, 22 Oct 2021 14:19:48 -0400 Subject: [PATCH] xc-ample upgrade script --- hardhat.config.js | 2 ++ helpers/contracts.js | 66 +++++++++++++++++++++++++++++++++++++++++--- tasks/upgrade.js | 38 +++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 tasks/upgrade.js diff --git a/hardhat.config.js b/hardhat.config.js index 83368b5..e2aecdf 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -20,6 +20,8 @@ require('./tasks/info/ampl'); require('./tasks/info/chain_bridge'); require('./tasks/info/cb_ampl_tx'); +require('./tasks/upgrade'); + module.exports = { solidity: { compilers: [ diff --git a/helpers/contracts.js b/helpers/contracts.js index 76ad33d..46cee2c 100644 --- a/helpers/contracts.js +++ b/helpers/contracts.js @@ -44,6 +44,10 @@ const ContractABIPaths = { ChainBridgeBatchRebaseReport: 'contracts/_utilities', }; +const sleep = (sec) => { + return new Promise((resolve) => setTimeout(resolve, sec * 1000)); +}; + const getCompiledContractFactory = (ethers, contract) => { return ethers.getContractFactory( `${ContractABIPaths[contract]}/${contract}.sol:${contract}`, @@ -100,6 +104,63 @@ const deployProxyContract = async ( return contract; }; +const upgradeProxyContract = async ( + ethers, + network, + contractName, + deployedContractRef, + signer, + txParams, + force=false, +) => { + const proxyAdmin = await getDeployedContractInstance( + network, + 'proxyAdmin', + ethers.provider, + ); + + const proxy = await getDeployedContractInstance( + network, + deployedContractRef, + ethers.provider, + ); + + const currentImplAddr = await proxyAdmin.getProxyImplementation(proxy.address); + console.log(`Current implementation for ${contractName} is at`, currentImplAddr); + + // deploy new implementation + let newImpl; + if(!force) { + const Factory = await getCompiledContractFactory(ethers, contractName); + newImpl = await upgrades.prepareUpgrade(proxy.address, Factory); + } else { + console.log(`CAUTION: Skpping storage layout verification!`) + console.log(`CANCEL NOW to stop, this action is not reversable`) + await sleep(10); + newImpl = await deployContract( + ethers, + contractName, + signer, + [], + txParams, + ); + } + console.log(`New implementation for ${contractName} is at`, newImpl.address); + + if ((await proxyAdmin.owner()) == (await signer.getAddress())) { + await proxyAdmin + .connect(signer) + .upgrade(proxy.address, newImpl.address, txParams); + } else { + console.log('Signer not proxy onwer, cant upgrade'); + console.log( + `Execute proxyAdmin.upgrade(${proxy.address}, ${newImpl.address})`, + ); + } + + return newImpl; +}; + const getDeployedContractInstance = async (network, contractName, provider) => { const contractData = await readContractDeploymentData(network, contractName); return new ethers.Contract(contractData.address, contractData.abi, provider); @@ -188,10 +249,6 @@ const filterContractEvents = async ( endBlock = endBlock || (await provider.getBlockNumber()); const freq = timeFrameSec * BLOCKS_PER_SEC; - const sleep = (sec) => { - return new Promise((resolve) => setTimeout(resolve, sec * 1000)); - }; - console.log(address, event, startBlock, endBlock, timeFrameSec); let logs = []; @@ -250,6 +307,7 @@ module.exports = { deployContract, deployProxyAdminContract, deployProxyContract, + upgradeProxyContract, filterContractEvents, }; diff --git a/tasks/upgrade.js b/tasks/upgrade.js new file mode 100644 index 0000000..8f7617b --- /dev/null +++ b/tasks/upgrade.js @@ -0,0 +1,38 @@ +const { types } = require('hardhat/config'); +const { txTask, loadSignerSync, etherscanVerify } = require('../helpers/tasks'); +const { getDeployedContractInstance, upgradeProxyContract } = require('../helpers/contracts'); + +txTask( + 'upgrade:xc_ample', + 'Uprades the implementation of the xc-ample ERC-20 contract', +) +.addParam('force', 'Skip storage layout verification', false, types.boolean) +.setAction(async (args, hre) => { + const txParams = { gasPrice: args.gasPrice, gasLimit: args.gasLimit }; + if (txParams.gasPrice == 0) { + txParams.gasPrice = await hre.ethers.provider.getGasPrice(); + } + + const deployer = await loadSignerSync(args, hre.ethers.provider); + const deployerAddress = await deployer.getAddress(); + + console.log('------------------------------------------------------------'); + console.log('Deployer:', deployerAddress); + console.log(txParams); + + console.log('------------------------------------------------------------'); + console.log('Upgrading xc-ample contract'); + const newImpl = await upgradeProxyContract( + hre.ethers, + hre.network.name, + 'XCAmple', + 'xcAmple', + deployer, + txParams, + args.force + ); + + console.log('------------------------------------------------------------'); + console.log('Verify on etherscan'); + await etherscanVerify(hre, newImpl.address); +});