-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathLegacyUpgrades.sol
315 lines (294 loc) · 15.8 KB
/
LegacyUpgrades.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Options} from "../src/Options.sol";
import {Core} from "../src/internal/Core.sol";
/**
* @dev Library for managing upgradeable contracts from Forge scripts or tests.
*
* NOTE: Only for upgrading existing deployments using OpenZeppelin Contracts v4.
* For new deployments, use OpenZeppelin Contracts v5 and Upgrades.sol.
*/
library Upgrades {
/**
* @dev Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* @param proxy Address of the proxy to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param data Encoded call data of an arbitrary function to call during the upgrade process, or empty if no function needs to be called during the upgrade
* @param opts Common options
*/
function upgradeProxy(address proxy, string memory contractName, bytes memory data, Options memory opts) internal {
Core.upgradeProxy(proxy, contractName, data, opts);
}
/**
* @dev Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* @param proxy Address of the proxy to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param data Encoded call data of an arbitrary function to call during the upgrade process, or empty if no function needs to be called during the upgrade
*/
function upgradeProxy(address proxy, string memory contractName, bytes memory data) internal {
Options memory opts;
Core.upgradeProxy(proxy, contractName, data, opts);
}
/**
* @notice For tests only. If broadcasting in scripts, use the `--sender <ADDRESS>` option with `forge script` instead.
*
* @dev Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* This function provides an additional `tryCaller` parameter to test an upgrade using a specific caller address.
* Use this if you encounter `OwnableUnauthorizedAccount` errors in your tests.
*
* @param proxy Address of the proxy to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param data Encoded call data of an arbitrary function to call during the upgrade process, or empty if no function needs to be called during the upgrade
* @param opts Common options
* @param tryCaller Address to use as the caller of the upgrade function. This should be the address that owns the proxy or its ProxyAdmin.
*/
function upgradeProxy(
address proxy,
string memory contractName,
bytes memory data,
Options memory opts,
address tryCaller
) internal {
Core.upgradeProxy(proxy, contractName, data, opts, tryCaller);
}
/**
* @notice For tests only. If broadcasting in scripts, use the `--sender <ADDRESS>` option with `forge script` instead.
*
* @dev Upgrades a proxy to a new implementation contract. Only supported for UUPS or transparent proxies.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* This function provides an additional `tryCaller` parameter to test an upgrade using a specific caller address.
* Use this if you encounter `OwnableUnauthorizedAccount` errors in your tests.
*
* @param proxy Address of the proxy to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param data Encoded call data of an arbitrary function to call during the upgrade process, or empty if no function needs to be called during the upgrade
* @param tryCaller Address to use as the caller of the upgrade function. This should be the address that owns the proxy or its ProxyAdmin.
*/
function upgradeProxy(address proxy, string memory contractName, bytes memory data, address tryCaller) internal {
Options memory opts;
Core.upgradeProxy(proxy, contractName, data, opts, tryCaller);
}
/**
* @dev Upgrades a beacon to a new implementation contract.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* @param beacon Address of the beacon to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param opts Common options
*/
function upgradeBeacon(address beacon, string memory contractName, Options memory opts) internal {
Core.upgradeBeacon(beacon, contractName, opts);
}
/**
* @dev Upgrades a beacon to a new implementation contract.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* @param beacon Address of the beacon to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
*/
function upgradeBeacon(address beacon, string memory contractName) internal {
Options memory opts;
Core.upgradeBeacon(beacon, contractName, opts);
}
/**
* @notice For tests only. If broadcasting in scripts, use the `--sender <ADDRESS>` option with `forge script` instead.
*
* @dev Upgrades a beacon to a new implementation contract.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* This function provides an additional `tryCaller` parameter to test an upgrade using a specific caller address.
* Use this if you encounter `OwnableUnauthorizedAccount` errors in your tests.
*
* @param beacon Address of the beacon to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param opts Common options
* @param tryCaller Address to use as the caller of the upgrade function. This should be the address that owns the beacon.
*/
function upgradeBeacon(
address beacon,
string memory contractName,
Options memory opts,
address tryCaller
) internal {
Core.upgradeBeacon(beacon, contractName, opts, tryCaller);
}
/**
* @notice For tests only. If broadcasting in scripts, use the `--sender <ADDRESS>` option with `forge script` instead.
*
* @dev Upgrades a beacon to a new implementation contract.
*
* Requires that either the `referenceContract` option is set, or the new implementation contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* This function provides an additional `tryCaller` parameter to test an upgrade using a specific caller address.
* Use this if you encounter `OwnableUnauthorizedAccount` errors in your tests.
*
* @param beacon Address of the beacon to upgrade
* @param contractName Name of the new implementation contract to upgrade to, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param tryCaller Address to use as the caller of the upgrade function. This should be the address that owns the beacon.
*/
function upgradeBeacon(address beacon, string memory contractName, address tryCaller) internal {
Options memory opts;
Core.upgradeBeacon(beacon, contractName, opts, tryCaller);
}
/**
* @dev Validates a new implementation contract in comparison with a reference contract, but does not deploy it.
*
* Requires that either the `referenceContract` option is set, or the contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* @param contractName Name of the contract to validate, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param opts Common options
*/
function validateUpgrade(string memory contractName, Options memory opts) internal {
Core.validateUpgrade(contractName, opts);
}
/**
* @dev Validates a new implementation contract in comparison with a reference contract, deploys the new implementation contract,
* and returns its address.
*
* Requires that either the `referenceContract` option is set, or the contract has a `@custom:oz-upgrades-from <reference>` annotation.
*
* Use this method to prepare an upgrade to be run from an admin address you do not control directly or cannot use from your deployment environment.
*
* @param contractName Name of the contract to deploy, e.g. "MyContract.sol" or "MyContract.sol:MyContract" or artifact path relative to the project root directory
* @param opts Common options
* @return Address of the new implementation contract
*/
function prepareUpgrade(string memory contractName, Options memory opts) internal returns (address) {
return Core.prepareUpgrade(contractName, opts);
}
/**
* @dev Gets the admin address of a transparent proxy from its ERC1967 admin storage slot.
*
* @param proxy Address of a transparent proxy
* @return Admin address
*/
function getAdminAddress(address proxy) internal view returns (address) {
return Core.getAdminAddress(proxy);
}
/**
* @dev Gets the implementation address of a transparent or UUPS proxy from its ERC1967 implementation storage slot.
*
* @param proxy Address of a transparent or UUPS proxy
* @return Implementation address
*/
function getImplementationAddress(address proxy) internal view returns (address) {
return Core.getImplementationAddress(proxy);
}
/**
* @dev Gets the beacon address of a beacon proxy from its ERC1967 beacon storage slot.
*
* @param proxy Address of a beacon proxy
* @return Beacon address
*/
function getBeaconAddress(address proxy) internal view returns (address) {
return Core.getBeaconAddress(proxy);
}
}
/**
* @dev Library for managing upgradeable contracts from Forge tests, without validations.
*
* Can be used with `forge coverage`. Requires implementation contracts to be instantiated first.
* Does not require `--ffi` and does not require a clean compilation before each run.
*
* Not supported for OpenZeppelin Defender deployments.
*
* WARNING: Not recommended for use in Forge scripts.
* `UnsafeUpgrades` does not validate whether your contracts are upgrade safe or whether new implementations are compatible with previous ones.
* Use `Upgrades` if you want validations to be run.
*
* NOTE: Only for upgrading existing deployments using OpenZeppelin Contracts v4.
* For new deployments, use OpenZeppelin Contracts v5 and Upgrades.sol.
*/
library UnsafeUpgrades {
/**
* @dev Upgrades a proxy to a new implementation contract address. Only supported for UUPS or transparent proxies.
*
* @param proxy Address of the proxy to upgrade
* @param newImpl Address of the new implementation contract to upgrade to
* @param data Encoded call data of an arbitrary function to call during the upgrade process, or empty if no function needs to be called during the upgrade
*/
function upgradeProxy(address proxy, address newImpl, bytes memory data) internal {
Core.upgradeProxyTo(proxy, newImpl, data);
}
/**
* @notice For tests only. If broadcasting in scripts, use the `--sender <ADDRESS>` option with `forge script` instead.
*
* @dev Upgrades a proxy to a new implementation contract address. Only supported for UUPS or transparent proxies.
*
* This function provides an additional `tryCaller` parameter to test an upgrade using a specific caller address.
* Use this if you encounter `OwnableUnauthorizedAccount` errors in your tests.
*
* @param proxy Address of the proxy to upgrade
* @param newImpl Address of the new implementation contract to upgrade to
* @param data Encoded call data of an arbitrary function to call during the upgrade process, or empty if no function needs to be called during the upgrade
* @param tryCaller Address to use as the caller of the upgrade function. This should be the address that owns the proxy or its ProxyAdmin.
*/
function upgradeProxy(address proxy, address newImpl, bytes memory data, address tryCaller) internal {
Core.upgradeProxyTo(proxy, newImpl, data, tryCaller);
}
/**
* @dev Upgrades a beacon to a new implementation contract address.
*
* @param beacon Address of the beacon to upgrade
* @param newImpl Address of the new implementation contract to upgrade to
*/
function upgradeBeacon(address beacon, address newImpl) internal {
Core.upgradeBeaconTo(beacon, newImpl);
}
/**
* @notice For tests only. If broadcasting in scripts, use the `--sender <ADDRESS>` option with `forge script` instead.
*
* @dev Upgrades a beacon to a new implementation contract address.
*
* This function provides an additional `tryCaller` parameter to test an upgrade using a specific caller address.
* Use this if you encounter `OwnableUnauthorizedAccount` errors in your tests.
*
* @param beacon Address of the beacon to upgrade
* @param newImpl Address of the new implementation contract to upgrade to
* @param tryCaller Address to use as the caller of the upgrade function. This should be the address that owns the beacon.
*/
function upgradeBeacon(address beacon, address newImpl, address tryCaller) internal {
Core.upgradeBeaconTo(beacon, newImpl, tryCaller);
}
/**
* @dev Gets the admin address of a transparent proxy from its ERC1967 admin storage slot.
*
* @param proxy Address of a transparent proxy
* @return Admin address
*/
function getAdminAddress(address proxy) internal view returns (address) {
return Core.getAdminAddress(proxy);
}
/**
* @dev Gets the implementation address of a transparent or UUPS proxy from its ERC1967 implementation storage slot.
*
* @param proxy Address of a transparent or UUPS proxy
* @return Implementation address
*/
function getImplementationAddress(address proxy) internal view returns (address) {
return Core.getImplementationAddress(proxy);
}
/**
* @dev Gets the beacon address of a beacon proxy from its ERC1967 beacon storage slot.
*
* @param proxy Address of a beacon proxy
* @return Beacon address
*/
function getBeaconAddress(address proxy) internal view returns (address) {
return Core.getBeaconAddress(proxy);
}
}