-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmigrate_2.4.0.js
323 lines (265 loc) · 12.5 KB
/
migrate_2.4.0.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
const shell = require("shelljs");
const { getStateModifyingFunctionsHashes, getSelectors } = require("../../scripts/util/diamond-utils.js");
const environments = require("../../environments");
const Role = require("../domain/Role");
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, deploymentComplete } = require("../util/utils.js");
const hre = require("hardhat");
const PausableRegion = require("../domain/PausableRegion.js");
const ethers = hre.ethers;
const { getContractAt, getSigners, ZeroAddress, getDefaultProvider, Wallet } = ethers;
const network = hre.network.name;
const abiCoder = new ethers.AbiCoder();
const tag = "v2.4.0";
const version = "2.4.0";
const { EXCHANGE_ID_2_2_0, WrappedNative } = require("../config/protocol-parameters");
const { META_TRANSACTION_FORWARDER } = require("../config/client-upgrade");
const confirmations = hre.network.name == "hardhat" ? 1 : environments.confirmations;
const config = {
// status at v2.4.0
addOrUpgrade: [
"AccountHandlerFacet",
"AgentHandlerFacet",
"BundleHandlerFacet",
"BuyerHandlerFacet",
"ConfigHandlerFacet",
"DisputeHandlerFacet",
"DisputeResolverHandlerFacet",
"ExchangeHandlerFacet",
"FundsHandlerFacet",
"GroupHandlerFacet",
"MetaTransactionsHandlerFacet",
"OfferHandlerFacet",
"OrchestrationHandlerFacet1",
"OrchestrationHandlerFacet2",
"PauseHandlerFacet",
"ProtocolInitializationHandlerFacet",
"SellerHandlerFacet",
"TwinHandlerFacet",
"PriceDiscoveryHandlerFacet",
"SequentialCommitHandlerFacet",
],
remove: [],
skipSelectors: {},
facetsToInit: {
ExchangeHandlerFacet: { init: [], constructorArgs: [EXCHANGE_ID_2_2_0[network]] }, // must match nextExchangeId at the time of the upgrade
AccountHandlerFacet: { init: [] },
DisputeResolverHandlerFacet: { init: [] },
MetaTransactionsHandlerFacet: { init: [[]] },
OfferHandlerFacet: { init: [] },
OrchestrationHandlerFacet1: { init: [] },
PriceDiscoveryHandlerFacet: { init: [], constructorArgs: [WrappedNative[network]] },
SequentialCommitHandlerFacet: { init: [], constructorArgs: [WrappedNative[network]] },
},
initializationData: abiCoder.encode(
["uint256[]", "uint256[][]", "uint256[][]", "address"],
[[], [], [], ZeroAddress]
), // dummy; populated in migrate script
};
async function migrate(env, params) {
console.log(`Migration ${tag} started`);
if (params) {
if (params.WrappedNative) {
console.log("Using WrappedNative from params");
config.facetsToInit.PriceDiscoveryHandlerFacet.constructorArgs[0] = params.WrappedNative;
config.facetsToInit.SequentialCommitHandlerFacet.constructorArgs[0] = params.WrappedNative;
}
}
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.3.0") {
throw new Error("Current contract version must be 2.3.0");
}
let contracts = contractsFile?.contracts;
await recompileContracts();
// Get addresses of currently deployed contracts
const accessControllerAddress = contracts.find((c) => c.name === "AccessController")?.address;
const accessController = await getContractAt("AccessController", accessControllerAddress);
const signer = (await getSigners())[0].address;
if (env === "upgrade-test") {
// Grant PAUSER role to the deployer
await accessController.grantRole(Role.PAUSER, signer);
} else {
checkRole(contracts, "PAUSER", signer);
}
const protocolAddress = contracts.find((c) => c.name === "ProtocolDiamond")?.address;
if (env !== "upgrade-test") {
// Checking old version contracts to get selectors to remove
console.log("Checking out contracts on version 2.3.0");
shell.exec(`rm -rf contracts/*`);
shell.exec(`git checkout v2.3.0 contracts package.json package-lock.json`);
console.log("Installing dependencies");
shell.exec("npm install");
console.log("Compiling old contracts");
await recompileContracts();
}
console.log("Pausing the Seller, Offer and Exchanges region...");
let pauseHandler = await getContractAt("IBosonPauseHandler", protocolAddress);
const pauseTransaction = await pauseHandler.pause(
[PausableRegion.Sellers, PausableRegion.Offers, PausableRegion.Exchanges],
await getFees(maxPriorityFeePerGas)
);
// await 1 block to ensure the pause is effective
await pauseTransaction.wait(confirmations);
let functionNamesToSelector = {};
const preUpgradeFacetList = config.addOrUpgrade.filter(
(f) => !["PriceDiscoveryHandlerFacet", "SequentialCommitHandlerFacet"].includes(f)
);
for (const facet of preUpgradeFacetList) {
const facetContract = await getContractAt(facet, protocolAddress);
const { signatureToNameMapping } = getSelectors(facetContract, true);
functionNamesToSelector = { ...functionNamesToSelector, ...signatureToNameMapping };
}
let getFunctionHashesClosure = getStateModifyingFunctionsHashes(preUpgradeFacetList, ["executeMetaTransaction"]);
const oldSelectors = await getFunctionHashesClosure();
// Get the data for back-filling
const { sellerIds, royaltyPercentages, offerIds } = await prepareInitializationData(protocolAddress);
const { backFillData } = require("../../scripts/upgrade-hooks/2.4.0.js");
backFillData({ sellerIds, royaltyPercentages, offerIds });
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();
// Deploy Boson Price Discovery Client
console.log("Deploying Boson Price Discovery Client...");
const constructorArgs = [WrappedNative[network], protocolAddress];
const bosonPriceDiscoveryFactory = await ethers.getContractFactory("BosonPriceDiscovery");
const bosonPriceDiscovery = await bosonPriceDiscoveryFactory.deploy(...constructorArgs);
await bosonPriceDiscovery.waitForDeployment();
deploymentComplete(
"BosonPriceDiscoveryClient",
await bosonPriceDiscovery.getAddress(),
constructorArgs,
"",
contracts
);
await writeContracts(contracts, env, contractsFile.protocolVersion); // keep existing protocol version to avoid troubles in the upgrade script
console.log("Executing upgrade facets script");
await hre.run("upgrade-facets", {
env,
facetConfig: JSON.stringify(config),
newVersion: version,
functionNamesToSelector: JSON.stringify(functionNamesToSelector),
});
getFunctionHashesClosure = getStateModifyingFunctionsHashes(config.addOrUpgrade, ["executeMetaTransaction"]);
const newSelectors = await getFunctionHashesClosure();
const unchanged = oldSelectors.filter((value) => newSelectors.includes(value));
const selectorsToRemove = oldSelectors.filter((value) => !unchanged.includes(value)); // unique old selectors
const selectorsToAdd = newSelectors.filter((value) => !unchanged.includes(value)); // unique new selectors
const metaTransactionHandlerFacet = await getContractAt("MetaTransactionsHandlerFacet", protocolAddress);
console.log("Removing selectors", selectorsToRemove.join(","));
const tx = await metaTransactionHandlerFacet.setAllowlistedFunctions(selectorsToRemove, false);
await tx.wait(confirmations); // wait, so the next check is accurate
// check if functions were removed
for (const selector of selectorsToRemove) {
const isAllowed = await metaTransactionHandlerFacet["isFunctionAllowlisted(bytes32)"](selector);
if (isAllowed) {
console.error(`Selector ${selector} was not removed`);
}
}
console.log("Adding selectors", selectorsToAdd.join(","));
await metaTransactionHandlerFacet.setAllowlistedFunctions(selectorsToAdd, true);
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,
});
console.log("Unpausing all regions...");
pauseHandler = await getContractAt("IBosonPauseHandler", protocolAddress);
await pauseHandler.unpause([], await getFees(maxPriorityFeePerGas));
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 {}
}
async function prepareInitializationData(protocolAddress) {
// We initialize a new provider which is not forked
const ethereumProvider = getDefaultProvider(hre.network.config.url || hre.network.config.forking.url);
const signer = Wallet.createRandom(ethereumProvider);
const sellerHandler = await getContractAt("IBosonAccountHandler", protocolAddress, signer);
const nextSellerId = await sellerHandler.getNextAccountId();
console.log("Preparing initialization data...");
console.log("Number of accounts", nextSellerId - 1n);
let decile = (nextSellerId - 1n) / 10n + 1n; // not very precise for small numbers, but it's just a progress indicator
const collections = {};
const royaltyPercentageToSellersAndOffers = {};
for (let i = 1n; i < nextSellerId; i++) {
if (i % decile === 0n) {
console.log(`${(i / decile) * 10n}%`);
}
const [exists] = await sellerHandler.getSeller(i);
if (!exists) {
continue;
}
const [defaultVoucherAddress, additionalCollections] = await sellerHandler.getSellersCollections(i);
const allCollections = [defaultVoucherAddress, ...additionalCollections];
const bosonVouchers = await Promise.all(
allCollections.map((collectionAddress) => getContractAt("IBosonVoucher", collectionAddress, signer))
);
const royaltyPercentages = await Promise.all(bosonVouchers.map((voucher) => voucher.getRoyaltyPercentage()));
collections[i] = royaltyPercentages;
for (const rp of royaltyPercentages) {
if (!royaltyPercentageToSellersAndOffers[rp]) {
royaltyPercentageToSellersAndOffers[rp] = { sellers: [], offers: [] };
}
}
// Find the minimum royalty percentage
const minRoyaltyPercentage = royaltyPercentages.reduce((a, b) => Math.min(a, b));
royaltyPercentageToSellersAndOffers[minRoyaltyPercentage].sellers.push(i);
}
const offerHandler = await getContractAt("IBosonOfferHandler", protocolAddress, signer);
const nextOfferId = await offerHandler.getNextOfferId();
console.log("Number of offers", nextOfferId - 1n);
decile = (nextOfferId - 1n) / 10n + 1n; // not very precise for small numbers, but it's just a progress indicator
for (let i = 1n; i < nextOfferId; i++) {
if (i % decile === 0n) {
console.log(`${(i / decile) * 10n}%`);
}
const [, offer] = await offerHandler.getOffer(i);
const collectionIndex = offer.collectionIndex;
const sellerId = offer.sellerId;
const offerId = i;
const offerRoyaltyPercentage = collections[sellerId][collectionIndex];
// Guaranteed to exist
royaltyPercentageToSellersAndOffers[offerRoyaltyPercentage].offers.push(offerId);
}
const royaltyPercentages = Object.keys(royaltyPercentageToSellersAndOffers);
const sellersAndOffers = Object.values(royaltyPercentageToSellersAndOffers);
const sellerIds = sellersAndOffers.map((sellerAndOffer) => sellerAndOffer.sellers);
const offerIds = sellersAndOffers.map((sellerAndOffer) => sellerAndOffer.offers);
return { royaltyPercentages, sellerIds, offerIds };
}
exports.migrate = migrate;