-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmigrate_2.4.1.js
121 lines (98 loc) · 4.01 KB
/
migrate_2.4.1.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
const shell = require("shelljs");
const environments = require("../../environments");
const tipMultiplier = BigInt(environments.tipMultiplier);
const tipSuggestion = 1500000000n; // js always returns this constant, it does not vary per block
const maxPriorityFeePerGas = tipSuggestion + tipMultiplier;
const { readContracts, getFees, checkRole, writeContracts } = require("../util/utils.js");
const hre = require("hardhat");
const ethers = hre.ethers;
const { getContractAt, getSigners, encodeBytes32String } = ethers;
const network = hre.network.name;
const tag = "v2.4.1";
const version = "2.4.1";
const { META_TRANSACTION_FORWARDER } = require("../config/client-upgrade");
const confirmations = hre.network.name == "hardhat" ? 1 : environments.confirmations;
async function migrate(env) {
console.log(`Migration ${tag} started`);
try {
if (env !== "upgrade-test") {
console.log("Removing any local changes before upgrading");
shell.exec(`git reset @{u}`);
const statusOutput = shell.exec("git status -s -uno scripts package.json");
if (statusOutput.stdout) {
throw new Error("Local changes found. Please stash them before upgrading");
}
}
const { chainId } = await ethers.provider.getNetwork();
const contractsFile = readContracts(chainId, network, env);
if (contractsFile?.protocolVersion !== "2.4.0") {
throw new Error("Current contract version must be 2.4.0");
}
console.log(`Checking out contracts on version ${tag}`);
shell.exec(`rm -rf contracts/*`);
shell.exec(`git checkout ${tag} contracts package.json package-lock.json`);
shell.exec(`git checkout HEAD scripts`);
console.log("Installing dependencies");
shell.exec(`npm install`);
console.log("Compiling contracts");
await recompileContracts();
let contracts = contractsFile?.contracts;
const signer = (await getSigners())[0].address;
// Check if admin has UPGRADER role
checkRole(contracts, "UPGRADER", signer);
const protocolAddress = contracts.find((c) => c.name === "ProtocolDiamond")?.address;
const initializationFacetAddress = contracts.find((c) => c.name === "ProtocolInitializationHandlerFacet")?.address;
console.log("Update protocol version");
const diamondCutFacet = await getContractAt("DiamondCutFacet", protocolAddress);
const protocolInitializationFacet = await getContractAt("ProtocolInitializationHandlerFacet", protocolAddress);
const versionBytes32 = encodeBytes32String(version);
const initializationData = protocolInitializationFacet.interface.encodeFunctionData("initialize", [
versionBytes32,
[],
[],
true,
"0x",
[],
[],
]);
const tx = await diamondCutFacet.diamondCut(
[],
initializationFacetAddress,
initializationData,
await getFees(maxPriorityFeePerGas)
);
await tx.wait(confirmations);
// Update version in contracts file
const newVersion = (await protocolInitializationFacet.getVersion()).replace(/\0/g, "");
console.log(`\n📋 New version: ${newVersion}`);
const contractsPath = await writeContracts(contracts, env, newVersion);
console.log(`✅ Contracts written to ${contractsPath}`);
console.log("Executing upgrade clients script");
const clientConfig = {
META_TRANSACTION_FORWARDER,
};
// Upgrade clients
await hre.run("upgrade-clients", {
env,
clientConfig: JSON.stringify(clientConfig),
newVersion: version,
});
shell.exec(`git checkout HEAD`);
shell.exec(`git reset HEAD`);
console.log(`Migration ${tag} completed`);
} catch (e) {
console.error(e);
shell.exec(`git checkout HEAD`);
shell.exec(`git reset HEAD`);
throw `Migration failed with: ${e}`;
}
}
async function recompileContracts() {
await hre.run("clean");
// If some contract was removed, compilation succeeds, but afterwards it falsely reports missing artifacts
// This is a workaround to ignore the error
try {
await hre.run("compile");
} catch {}
}
exports.migrate = migrate;