-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdeploy-amplifier-gateway.js
426 lines (334 loc) · 15.6 KB
/
deploy-amplifier-gateway.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
'use strict';
const { Command, Option } = require('commander');
const chalk = require('chalk');
const { ethers } = require('hardhat');
const {
ContractFactory,
Contract,
Wallet,
utils: { defaultAbiCoder, keccak256 },
getDefaultProvider,
} = ethers;
const {
saveConfig,
getBytecodeHash,
printInfo,
printError,
printWalletInfo,
printWarn,
prompt,
mainProcessor,
deployContract,
getGasOptions,
getWeightedSigners,
getContractJSON,
getDeployedAddress,
getDeployOptions,
getDomainSeparator,
isContract,
} = require('./utils');
const { addEvmOptions } = require('./cli-utils');
const { storeSignedTx, signTransaction, getWallet } = require('./sign-utils.js');
const { WEIGHTED_SIGNERS_TYPE, encodeWeightedSigners } = require('@axelar-network/axelar-gmp-sdk-solidity/scripts/utils');
const AxelarAmplifierGatewayProxy = require('@axelar-network/axelar-gmp-sdk-solidity/artifacts/contracts/gateway/AxelarAmplifierGatewayProxy.sol/AxelarAmplifierGatewayProxy.json');
const AxelarAmplifierGateway = require('@axelar-network/axelar-gmp-sdk-solidity/artifacts/contracts/gateway/AxelarAmplifierGateway.sol/AxelarAmplifierGateway.json');
async function getSetupParams(config, chain, operator, options) {
const { signers: signerSets, verifierSetId } = await getWeightedSigners(config, chain, options);
printInfo('Setup params', JSON.stringify([operator, signerSets], null, 2));
return { params: defaultAbiCoder.encode([`address`, `${WEIGHTED_SIGNERS_TYPE}[]`], [operator, signerSets]), verifierSetId };
}
async function deploy(config, chain, options) {
const { privateKey, reuseProxy, yes, predictOnly } = options;
const contractName = 'AxelarGateway';
const rpc = options.rpc || chain.rpc;
const provider = getDefaultProvider(rpc);
const wallet = new Wallet(privateKey).connect(provider);
await printWalletInfo(wallet);
if (chain.contracts === undefined) {
chain.contracts = {};
}
if (chain.contracts[contractName] === undefined) {
chain.contracts[contractName] = {};
}
const contractConfig = chain.contracts[contractName];
const owner = options.owner || chain.contracts.InterchainGovernance?.address || wallet.address;
if (!reuseProxy) {
if (owner === undefined) {
throw new Error('owner address is required');
}
if (owner !== wallet.address) {
printWarn(
'Owner address is not set to the wallet address. This is needed for official deployment and is transferred after deployment',
);
}
printInfo('Owner address', owner);
}
const gasOptions = await getGasOptions(chain, options, contractName);
const gatewayFactory = new ContractFactory(AxelarAmplifierGateway.abi, AxelarAmplifierGateway.bytecode, wallet);
const { deployerContract, salt } = getDeployOptions(options.deployMethod, options.salt || 'AxelarAmplifierGateway', chain);
let gateway;
let proxyAddress;
if (reuseProxy) {
proxyAddress = chain.contracts.AxelarGateway?.address;
if (proxyAddress === undefined) {
throw new Error('Proxy address is missing in the config file');
}
printInfo('Reusing Gateway Proxy address', proxyAddress);
gateway = gatewayFactory.attach(proxyAddress);
} else {
if (options.deployMethod === 'create2') {
// TODO: support create2 prediction
printError('create2 prediction is not supported yet');
}
proxyAddress = await getDeployedAddress(wallet.address, options.deployMethod, {
salt,
deployerContract,
contractJson: getContractJSON('AxelarAmplifierGatewayProxy'),
constructorArgs: [], // TODO: populate constructor args for create2 prediction to work
provider: wallet.provider,
nonce: (await wallet.getTransactionCount()) + 1,
});
printInfo('Predicted gateway proxy address', proxyAddress, chalk.cyan);
}
let existingAddress;
for (const chainConfig of Object.values(config.chains)) {
existingAddress = chainConfig.contracts?.[contractName]?.address;
if (existingAddress !== undefined) {
break;
}
}
if (existingAddress !== undefined && proxyAddress !== existingAddress) {
printWarn(`Predicted address ${proxyAddress} does not match existing deployment ${existingAddress} in chain configs.`);
printWarn('For official deployment, recheck the deployer, salt, args, or contract bytecode.');
printWarn('This is NOT required if the deployments are done by different integrators');
}
if (await isContract(proxyAddress, wallet.provider)) {
printError(`Contract already deployed at predicted address "${proxyAddress}"!`);
}
if (predictOnly || prompt(`Does derived address match existing gateway deployments? Proceed with deployment on ${chain.name}?`, yes)) {
return;
}
contractConfig.deployer = wallet.address;
const domainSeparator = await getDomainSeparator(config, chain, options);
const minimumRotationDelay = Number(options.minimumRotationDelay);
printInfo(`Deploying gateway implementation contract`);
printInfo('Gateway Implementation args', `${options.previousSignersRetention}, ${domainSeparator}, ${minimumRotationDelay}`);
printInfo('Deploy method', options.deployMethod);
printInfo('Deploy salt (if not create based deployment)', salt);
let implementation;
if (options.skipExisting && contractConfig.implementation) {
implementation = gatewayFactory.attach(contractConfig.implementation);
} else {
const implementationSalt = `${salt} Implementation`;
implementation = await deployContract(
options.deployMethod,
wallet,
AxelarAmplifierGateway,
[options.previousSignersRetention, domainSeparator, minimumRotationDelay],
{ salt: implementationSalt, deployerContract },
gasOptions,
{},
chain,
);
}
printInfo('Gateway Implementation', implementation.address);
const implementationCodehash = await getBytecodeHash(implementation, chain.axelarId);
printInfo('Gateway Implementation codehash', implementationCodehash);
if (options.skipExisting && contractConfig.address) {
proxyAddress = contractConfig?.address;
gateway = gatewayFactory.attach(proxyAddress);
} else if (!reuseProxy) {
const operator = options.operator || contractConfig.operator || wallet.address;
const { params, verifierSetId } = await getSetupParams(config, chain, operator, options);
printInfo('Deploying gateway proxy contract');
printInfo('Proxy deployment args', `${implementation.address}, ${params}`);
contractConfig.operator = operator;
const proxyDeploymentArgs = [implementation.address, owner, params];
contractConfig.proxyDeploymentArgs = proxyDeploymentArgs;
contractConfig.initialVerifierSetId = verifierSetId;
const gatewayProxy = await deployContract(
options.deployMethod,
wallet,
AxelarAmplifierGatewayProxy,
proxyDeploymentArgs,
{ salt, deployerContract },
gasOptions,
{},
chain,
);
printInfo('Gateway Proxy', gatewayProxy.address);
gateway = gatewayFactory.attach(gatewayProxy.address);
}
// Verify deployment
let error = false;
const ownerAddress = await gateway.owner();
printInfo(`Existing owner`, ownerAddress);
if (!reuseProxy && owner !== ownerAddress) {
printError(`ERROR: Retrieved owner address is different:`);
printError(` Actual: ${ownerAddress}`);
printError(` Expected: ${owner}`);
error = true;
}
const gatewayImplementation = await gateway.implementation();
if (!reuseProxy && gatewayImplementation !== implementation.address) {
printError(
`ERROR: Implementation contract retrieved from gateway ${gatewayImplementation} doesn't match deployed contract ${implementation.address}`,
);
error = true;
}
if (Number(options.previousSignersRetention) !== (await gateway.previousSignersRetention()).toNumber()) {
printError('ERROR: Previous signer retention mismatch');
error = true;
}
if (domainSeparator !== (await gateway.domainSeparator())) {
printError('ERROR: Domain separator mismatch');
error = true;
}
if (minimumRotationDelay !== (await gateway.minimumRotationDelay()).toNumber()) {
printError('ERROR: Minimum rotation delay mismatch');
error = true;
}
if (contractConfig.operator !== (await gateway.operator())) {
printError('ERROR: Operator mismatch');
error = true;
}
if (!reuseProxy) {
const { signers: signerSets } = await getWeightedSigners(config, chain, options);
for (let i = 0; i < signerSets.length; i++) {
const signersHash = keccak256(encodeWeightedSigners(signerSets[i]));
const epoch = (await gateway.epochBySignersHash(signersHash)).toNumber();
const signersHashByEpoch = await gateway.signersHashByEpoch(i + 1);
if (epoch !== i + 1) {
printError(`ERROR: Epoch mismatch for signer set ${i + 1}`);
printError(` Actual: ${epoch}`);
printError(` Expected: ${i + 1}`);
error = true;
}
if (signersHashByEpoch !== signersHash) {
printError(`ERROR: Signer hash mismatch for signer set ${i + 1}`);
printError(` Actual: ${signersHashByEpoch}`);
printError(` Expected: ${signersHash}`);
error = true;
}
}
}
if (error) {
printError('Deployment status', 'FAILED');
return;
}
contractConfig.address = gateway.address;
contractConfig.implementation = implementation.address;
contractConfig.implementationCodehash = implementationCodehash;
contractConfig.deploymentMethod = options.deployMethod;
contractConfig.previousSignersRetention = options.previousSignersRetention;
contractConfig.domainSeparator = domainSeparator;
contractConfig.minimumRotationDelay = minimumRotationDelay;
contractConfig.connectionType = 'amplifier';
contractConfig.owner = owner;
chain.chainType = 'evm';
if (options.deployMethod !== 'create') {
contractConfig.salt = salt;
}
printInfo('Deployment status', 'SUCCESS');
saveConfig(config, options.env);
}
async function upgrade(_, chain, options) {
const { privateKey, yes, offline, env, predictOnly } = options;
const contractName = 'AxelarGateway';
const chainName = chain.name.toLowerCase();
const rpc = options.rpc || chain.rpc;
const provider = getDefaultProvider(rpc);
const wallet = await getWallet(privateKey, provider, options);
const { address } = await printWalletInfo(wallet, options);
const contractConfig = chain.contracts[contractName];
const gateway = new Contract(contractConfig.address, AxelarAmplifierGateway.abi, wallet);
let implementationCodehash = contractConfig.implementationCodehash;
const setupParams = '0x';
if (!offline) {
const codehash = await getBytecodeHash(contractConfig.implementation, chain.axelarId, provider);
if (!implementationCodehash) {
// retrieve codehash dynamically if not specified in the config file
implementationCodehash = codehash;
} else if (codehash !== implementationCodehash) {
throw new Error(
`Implementation codehash mismatch. Expected ${implementationCodehash} but got ${codehash}. Please check if the implementation contract is deployed correctly.`,
);
}
} else {
if (!implementationCodehash) {
throw new Error('Implementation codehash is missing in the config file');
}
}
printInfo('Gateway Proxy', gateway.address);
if (!offline) {
printInfo('Current implementation', await gateway.implementation());
}
printInfo('Upgrading to implementation', contractConfig.implementation);
printInfo('New Implementation codehash', implementationCodehash);
printInfo('Setup params', setupParams);
const gasOptions = await getGasOptions(chain, options, contractName);
if (predictOnly || prompt(`Proceed with an upgrade on ${chain.name}?`, yes)) {
return;
}
const tx = await gateway.populateTransaction.upgrade(contractConfig.implementation, implementationCodehash, setupParams, gasOptions);
const { baseTx, signedTx } = await signTransaction(wallet, chain, tx, options);
if (offline) {
const filePath = `./tx/signed-tx-${env}-gateway-upgrade-${chainName}-address-${address}-nonce-${baseTx.nonce}.json`;
printInfo(`Storing signed Tx offline in file ${filePath}`);
// Storing the fields in the data that will be stored in file
const data = {
msg: `This transaction will upgrade gateway ${gateway.address} to implementation ${contractConfig.implementation} on chain ${chain.name}`,
unsignedTx: baseTx,
signedTx,
status: 'PENDING',
};
storeSignedTx(filePath, data);
} else {
const newImplementation = await gateway.implementation();
printInfo('New implementation', newImplementation);
if (newImplementation !== contractConfig.implementation) {
printWarn('Implementation not upgraded yet!');
return;
}
printInfo('Upgraded!');
}
}
async function processCommand(config, chain, options) {
if (!options.upgrade) {
await deploy(config, chain, options);
} else {
await upgrade(config, chain, options);
}
}
async function main(options) {
await mainProcessor(options, processCommand);
}
async function programHandler() {
const program = new Command();
program.name('deploy-amplifier-gateway').description('Deploy Amplifier Gateway');
// use create3 as default deploy method
addEvmOptions(program, { salt: true, deployMethod: 'create3', skipExisting: true, upgrade: true, predictOnly: true });
program.addOption(new Option('-r, --rpc <rpc>', 'chain rpc url').env('URL'));
program.addOption(new Option('--previousSignersRetention <previousSignersRetention>', 'previous signer retention').default(15));
program.addOption(new Option('--domainSeparator <domainSeparator>', 'domain separator'));
program.addOption(
new Option('--minimumRotationDelay <minimumRotationDelay>', 'minium delay for signer rotations').default(24 * 60 * 60),
); // 1 day
program.addOption(new Option('--reuseProxy', 'reuse proxy contract modules for new implementation deployment'));
program.addOption(new Option('--ignoreError', 'Ignore deployment errors and proceed to next chain'));
program.addOption(new Option('--owner <owner>', 'owner/governance address').env('OWNER'));
program.addOption(new Option('--operator <operator>', 'gateway operator address'));
program.addOption(new Option('--keyID <keyID>', 'use the specified key ID address instead of the querying the chain').env('KEY_ID'));
program.addOption(new Option('--offline', 'Run in offline mode'));
program.addOption(new Option('--nonceOffset <nonceOffset>', 'The value to add in local nonce if it deviates from actual wallet nonce'));
program.action((options) => {
main(options);
});
program.parse();
}
if (require.main === module) {
programHandler();
}
module.exports = {
deployAmplifierGateway: deploy,
};