-
Notifications
You must be signed in to change notification settings - Fork 4
/
upgrade.s.sol
68 lines (49 loc) · 2.31 KB
/
upgrade.s.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {MyNFTUpgradeableV2} from "./ExampleNFT.sol";
import "forge-std/Script.sol";
/*
For a more complete example (of deploying, upgrading & keeping track of proxies),
have a look at https://github.com/0xPhaze/upgrade-scripts.
1. Run script (NOTE: replace `PROXY_ADDRESS=...` below with your deployed address or add it to your .env)
```sh
source .env && PROXY_ADDRESS=0x123 forge script upgrade --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --verify --etherscan-api-key $ETHERSCAN_KEY -vvvv
```
4. (optional) if verification failed
```sh
source .env && PROXY_ADDRESS=0x123 forge script upgrade --rpc-url $RPC_URL --private-key $PRIVATE_KEY --resume --verify --etherscan-api-key $ETHERSCAN_KEY -vvvv
```
*/
contract upgrade is Script {
address proxyAddress;
MyNFTUpgradeableV2 myNFT;
function run() external {
proxyAddress = tryLoadEnvVar("PROXY_ADDRESS");
require(proxyAddress.code.length != 0, "Invalid proxy address. Address contains no code.");
myNFT = MyNFTUpgradeableV2(proxyAddress);
vm.startBroadcast();
// deploys the new implementation contract
address implementation = address(new MyNFTUpgradeableV2());
// call the init function upon deployment
bytes memory initCalldata = abi.encodePacked(MyNFTUpgradeableV2.init.selector);
// calls upgradeTo and MyNFTUpgradeableV2.init() in the context of the proxy
myNFT.upgradeToAndCall(implementation, initCalldata);
vm.stopBroadcast();
integrationTest();
console.log("new implementation:", implementation);
}
function tryLoadEnvVar(string memory key) internal returns (address) {
try vm.envAddress(key) returns (address addr) {
return addr;
} catch {
console.log("Make sure `%s=` is set in your `.env` file.", key);
revert("Could not load environment variable.");
}
}
/// @notice the script will fail if these conditions aren't met
function integrationTest() internal view {
require(myNFT.owner() == msg.sender);
require(keccak256(abi.encode(myNFT.name())) == keccak256(abi.encode("Non-fungible Contract V2")));
require(keccak256(abi.encode(myNFT.symbol())) == keccak256(abi.encode("NFTV2")));
}
}