-
Notifications
You must be signed in to change notification settings - Fork 32
/
upgradable.js
168 lines (143 loc) · 5.21 KB
/
upgradable.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
'use strict';
const {
Contract,
ContractFactory,
utils: { keccak256 },
} = require('ethers');
const { create2DeployAndInitContract } = require('./create2Deployer');
const { create3DeployContract, create3DeployAndInitContract } = require('./create3Deployer');
const { verifyContract } = require('./utils');
const IUpgradable = require('../interfaces/IUpgradable.json');
// for deploying upgradable contracts with InitProxy via CREATE2 method
async function deployCreate2InitUpgradable(
create2DeployerAddress,
wallet,
implementationJson,
proxyJson,
implementationConstructorArgs = [],
proxyConstructorArgs = [],
setupParams = '0x',
key = Date.now(),
gasLimit = null,
env = 'testnet',
chain = 'ethereum',
shouldVerifyContract = false,
) {
const implementationFactory = new ContractFactory(implementationJson.abi, implementationJson.bytecode, wallet);
const implementation = await implementationFactory.deploy(...implementationConstructorArgs);
await implementation.deployed();
const proxy = await create2DeployAndInitContract(
create2DeployerAddress,
wallet,
proxyJson,
key,
proxyConstructorArgs,
[implementation.address, wallet.address, setupParams],
gasLimit,
);
if (shouldVerifyContract) {
await verifyContract(env, chain, implementation.address, implementationConstructorArgs);
await verifyContract(env, chain, proxy.address, proxyConstructorArgs);
}
return new Contract(proxy.address, implementationJson.abi, wallet);
}
// for deploying upgradable contracts with Proxy via CREATE3 method
async function deployCreate3Upgradable(
create3DeployerAddress,
wallet,
implementationJson,
proxyJson,
implementationConstructorArgs = [],
additionalProxyConstructorArgs = [],
setupParams = '0x',
key = Date.now().toString(),
gasLimit = null,
env = 'testnet',
chain = 'ethereum',
shouldVerifyContract = false,
) {
const implementationFactory = new ContractFactory(implementationJson.abi, implementationJson.bytecode, wallet);
const implementation = await implementationFactory.deploy(...implementationConstructorArgs);
await implementation.deployed();
const proxy = await create3DeployContract(
create3DeployerAddress,
wallet,
proxyJson,
key,
[implementation.address, wallet.address, setupParams, ...additionalProxyConstructorArgs],
gasLimit,
);
if (shouldVerifyContract) {
await verifyContract(env, chain, implementation.address, implementationConstructorArgs);
await verifyContract(env, chain, proxy.address, [
implementation.address,
wallet.address,
setupParams,
...additionalProxyConstructorArgs,
]);
}
return new Contract(proxy.address, implementationJson.abi, wallet);
}
// for deploying upgradable contracts with InitProxy via CREATE3 method
async function deployCreate3InitUpgradable(
create3DeployerAddress,
wallet,
implementationJson,
proxyJson,
implementationConstructorArgs = [],
proxyConstructorArgs = [],
setupParams = '0x',
key = Date.now(),
gasLimit = null,
env = 'testnet',
chain = 'ethereum',
shouldVerifyContract = false,
) {
const implementationFactory = new ContractFactory(implementationJson.abi, implementationJson.bytecode, wallet);
const implementation = await implementationFactory.deploy(...implementationConstructorArgs);
await implementation.deployed();
const proxy = await create3DeployAndInitContract(
create3DeployerAddress,
wallet,
proxyJson,
key,
proxyConstructorArgs,
[implementation.address, wallet.address, setupParams],
gasLimit,
);
if (shouldVerifyContract) {
await verifyContract(env, chain, implementation.address, implementationConstructorArgs);
await verifyContract(env, chain, proxy.address, proxyConstructorArgs);
}
return new Contract(proxy.address, implementationJson.abi, wallet);
}
async function upgradeUpgradable(
proxyAddress,
wallet,
contractJson,
implementationConstructorArgs = [],
setupParams = '0x',
env = 'testnet',
chain = 'ethereum',
shouldVerifyContract = false,
) {
const proxy = new Contract(proxyAddress, IUpgradable.abi, wallet);
const implementationFactory = new ContractFactory(contractJson.abi, contractJson.bytecode, wallet);
const implementation = await implementationFactory.deploy(...implementationConstructorArgs);
await implementation.deployed();
const implementationCode = await wallet.provider.getCode(implementation.address);
const implementationCodeHash = keccak256(implementationCode);
const tx = await proxy.upgrade(implementation.address, implementationCodeHash, setupParams);
await tx.wait();
if (shouldVerifyContract) {
await verifyContract(env, chain, implementation.address, implementationConstructorArgs);
}
return tx;
}
module.exports = {
deployUpgradable: deployCreate2InitUpgradable, // deprecated alias
deployCreate2InitUpgradable,
deployCreate3Upgradable,
deployCreate3InitUpgradable,
upgradeUpgradable,
};