Skip to content

Commit

Permalink
xc-ample upgrade script
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Oct 22, 2021
1 parent 5042cf0 commit 7910fb3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
2 changes: 2 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
66 changes: 62 additions & 4 deletions helpers/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -250,6 +307,7 @@ module.exports = {
deployContract,
deployProxyAdminContract,
deployProxyContract,
upgradeProxyContract,

filterContractEvents,
};
38 changes: 38 additions & 0 deletions tasks/upgrade.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 7910fb3

Please sign in to comment.