Skip to content

Commit 7910fb3

Browse files
committed
xc-ample upgrade script
1 parent 5042cf0 commit 7910fb3

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

hardhat.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ require('./tasks/info/ampl');
2020
require('./tasks/info/chain_bridge');
2121
require('./tasks/info/cb_ampl_tx');
2222

23+
require('./tasks/upgrade');
24+
2325
module.exports = {
2426
solidity: {
2527
compilers: [

helpers/contracts.js

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const ContractABIPaths = {
4444
ChainBridgeBatchRebaseReport: 'contracts/_utilities',
4545
};
4646

47+
const sleep = (sec) => {
48+
return new Promise((resolve) => setTimeout(resolve, sec * 1000));
49+
};
50+
4751
const getCompiledContractFactory = (ethers, contract) => {
4852
return ethers.getContractFactory(
4953
`${ContractABIPaths[contract]}/${contract}.sol:${contract}`,
@@ -100,6 +104,63 @@ const deployProxyContract = async (
100104
return contract;
101105
};
102106

107+
const upgradeProxyContract = async (
108+
ethers,
109+
network,
110+
contractName,
111+
deployedContractRef,
112+
signer,
113+
txParams,
114+
force=false,
115+
) => {
116+
const proxyAdmin = await getDeployedContractInstance(
117+
network,
118+
'proxyAdmin',
119+
ethers.provider,
120+
);
121+
122+
const proxy = await getDeployedContractInstance(
123+
network,
124+
deployedContractRef,
125+
ethers.provider,
126+
);
127+
128+
const currentImplAddr = await proxyAdmin.getProxyImplementation(proxy.address);
129+
console.log(`Current implementation for ${contractName} is at`, currentImplAddr);
130+
131+
// deploy new implementation
132+
let newImpl;
133+
if(!force) {
134+
const Factory = await getCompiledContractFactory(ethers, contractName);
135+
newImpl = await upgrades.prepareUpgrade(proxy.address, Factory);
136+
} else {
137+
console.log(`CAUTION: Skpping storage layout verification!`)
138+
console.log(`CANCEL NOW to stop, this action is not reversable`)
139+
await sleep(10);
140+
newImpl = await deployContract(
141+
ethers,
142+
contractName,
143+
signer,
144+
[],
145+
txParams,
146+
);
147+
}
148+
console.log(`New implementation for ${contractName} is at`, newImpl.address);
149+
150+
if ((await proxyAdmin.owner()) == (await signer.getAddress())) {
151+
await proxyAdmin
152+
.connect(signer)
153+
.upgrade(proxy.address, newImpl.address, txParams);
154+
} else {
155+
console.log('Signer not proxy onwer, cant upgrade');
156+
console.log(
157+
`Execute proxyAdmin.upgrade(${proxy.address}, ${newImpl.address})`,
158+
);
159+
}
160+
161+
return newImpl;
162+
};
163+
103164
const getDeployedContractInstance = async (network, contractName, provider) => {
104165
const contractData = await readContractDeploymentData(network, contractName);
105166
return new ethers.Contract(contractData.address, contractData.abi, provider);
@@ -188,10 +249,6 @@ const filterContractEvents = async (
188249
endBlock = endBlock || (await provider.getBlockNumber());
189250
const freq = timeFrameSec * BLOCKS_PER_SEC;
190251

191-
const sleep = (sec) => {
192-
return new Promise((resolve) => setTimeout(resolve, sec * 1000));
193-
};
194-
195252
console.log(address, event, startBlock, endBlock, timeFrameSec);
196253

197254
let logs = [];
@@ -250,6 +307,7 @@ module.exports = {
250307
deployContract,
251308
deployProxyAdminContract,
252309
deployProxyContract,
310+
upgradeProxyContract,
253311

254312
filterContractEvents,
255313
};

tasks/upgrade.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { types } = require('hardhat/config');
2+
const { txTask, loadSignerSync, etherscanVerify } = require('../helpers/tasks');
3+
const { getDeployedContractInstance, upgradeProxyContract } = require('../helpers/contracts');
4+
5+
txTask(
6+
'upgrade:xc_ample',
7+
'Uprades the implementation of the xc-ample ERC-20 contract',
8+
)
9+
.addParam('force', 'Skip storage layout verification', false, types.boolean)
10+
.setAction(async (args, hre) => {
11+
const txParams = { gasPrice: args.gasPrice, gasLimit: args.gasLimit };
12+
if (txParams.gasPrice == 0) {
13+
txParams.gasPrice = await hre.ethers.provider.getGasPrice();
14+
}
15+
16+
const deployer = await loadSignerSync(args, hre.ethers.provider);
17+
const deployerAddress = await deployer.getAddress();
18+
19+
console.log('------------------------------------------------------------');
20+
console.log('Deployer:', deployerAddress);
21+
console.log(txParams);
22+
23+
console.log('------------------------------------------------------------');
24+
console.log('Upgrading xc-ample contract');
25+
const newImpl = await upgradeProxyContract(
26+
hre.ethers,
27+
hre.network.name,
28+
'XCAmple',
29+
'xcAmple',
30+
deployer,
31+
txParams,
32+
args.force
33+
);
34+
35+
console.log('------------------------------------------------------------');
36+
console.log('Verify on etherscan');
37+
await etherscanVerify(hre, newImpl.address);
38+
});

0 commit comments

Comments
 (0)