forked from robocups/cdk-validium-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeLockUpgrade.js
178 lines (159 loc) · 6.24 KB
/
timeLockUpgrade.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* eslint-disable no-console, no-unused-vars, no-use-before-define */
const hre = require('hardhat');
const { ethers, upgrades } = require('hardhat');
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
const fs = require('fs');
const upgradeParameters = require('./upgrade_parameters.json');
const pathOutputJson = path.join(__dirname, `./upgrade_output_${new Date().getTime() / 1000}.json`);
async function main() {
// Set multiplier Gas
let currentProvider = ethers.provider;
if (upgradeParameters.multiplierGas) {
if (process.env.HARDHAT_NETWORK !== 'hardhat') {
const multiplierGas = upgradeParameters.multiplierGas;
currentProvider = new ethers.providers.JsonRpcProvider(`https://${process.env.HARDHAT_NETWORK}.infura.io/v3/${process.env.INFURA_PROJECT_ID}`);
async function overrideFeeData() {
const feedata = await ethers.provider.getFeeData();
return {
maxFeePerGas: feedata.maxFeePerGas.mul(multiplierGas),
maxPriorityFeePerGas: feedata.maxPriorityFeePerGas.mul(multiplierGas),
};
}
currentProvider.getFeeData = overrideFeeData;
}
}
// Check contract name existence
for (const upgrade of upgradeParameters.upgrades) {
await ethers.getContractFactory(upgrade.contractName);
}
let deployer;
if (upgradeParameters.deployerPvtKey) {
deployer = new ethers.Wallet(upgradeParameters.deployerPvtKey, currentProvider);
} else if (process.env.MNEMONIC) {
deployer = ethers.Wallet.fromMnemonic(process.env.MNEMONIC, 'm/44\'/60\'/0\'/0/0').connect(currentProvider);
console.log("using mnemonic", deployer.address)
} else {
[deployer] = (await ethers.getSigners());
}
// compìle contracts
await hre.run('compile');
const proxyAdmin = await upgrades.admin.getInstance();
const output = [];
// Upgrade cdkValidium
for (const upgrade of upgradeParameters.upgrades) {
const proxyPolygonAddress = upgrade.address;
const cdkValidiumFactory = await ethers.getContractFactory(upgrade.contractName, deployer);
let newImplPolygonAddress;
if (upgrade.constructorArgs) {
newImplPolygonAddress = await upgrades.prepareUpgrade(proxyPolygonAddress, cdkValidiumFactory,
{
constructorArgs: upgrade.constructorArgs,
unsafeAllow: ['constructor', 'state-variable-immutable'],
},
);
console.log({ newImplPolygonAddress });
console.log("you can verify the new impl address with:")
console.log(`npx hardhat verify --constructor-args upgrade/arguments.js ${newImplPolygonAddress} --network ${process.env.HARDHAT_NETWORK}\n`);
console.log("Copy the following constructor arguments on: upgrade/arguments.js \n", upgrade.constructorArgs)
} else {
newImplPolygonAddress = await upgrades.prepareUpgrade(proxyPolygonAddress, cdkValidiumFactory);
console.log({ newImplPolygonAddress });
console.log("you can verify the new impl address with:")
console.log(`npx hardhat verify ${newImplPolygonAddress} --network ${process.env.HARDHAT_NETWORK}`);
}
// Use timelock
const salt = upgradeParameters.timelockSalt || ethers.constants.HashZero;
let operation;
if (upgrade.callAfterUpgrade) {
operation = genOperation(
proxyAdmin.address,
0, // value
proxyAdmin.interface.encodeFunctionData(
'upgradeAndCall',
[
proxyPolygonAddress,
newImplPolygonAddress,
cdkValidiumFactory.interface.encodeFunctionData(
upgrade.callAfterUpgrade.functionName,
upgrade.callAfterUpgrade.arguments,
)
],
),
ethers.constants.HashZero, // predecesoor
salt, // salt
);
} else {
operation = genOperation(
proxyAdmin.address,
0, // value
proxyAdmin.interface.encodeFunctionData(
'upgrade',
[proxyPolygonAddress,
newImplPolygonAddress],
),
ethers.constants.HashZero, // predecesoor
salt, // salt
);
}
// Timelock operations
const TimelockFactory = await ethers.getContractFactory('CDKValidiumTimelock', deployer);
const minDelay = upgradeParameters.timelockMinDelay || 0;
// Schedule operation
const scheduleData = TimelockFactory.interface.encodeFunctionData(
'schedule',
[
operation.target,
operation.value,
operation.data,
operation.predecessor,
operation.salt,
minDelay,
],
);
// Execute operation
const executeData = TimelockFactory.interface.encodeFunctionData(
'execute',
[
operation.target,
operation.value,
operation.data,
operation.predecessor,
operation.salt,
],
);
console.log({ scheduleData });
console.log({ executeData });
output.push({
contractName: upgrade.contractName,
scheduleData,
executeData
})
}
fs.writeFileSync(pathOutputJson, JSON.stringify(output, null, 1));
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
// OZ test functions
function genOperation(target, value, data, predecessor, salt) {
const id = ethers.utils.solidityKeccak256([
'address',
'uint256',
'bytes',
'uint256',
'bytes32',
], [
target,
value,
data,
predecessor,
salt,
]);
return {
id, target, value, data, predecessor, salt,
};
}