-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmultisig-migration.test.ts
More file actions
399 lines (343 loc) · 11.6 KB
/
multisig-migration.test.ts
File metadata and controls
399 lines (343 loc) · 11.6 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { ethers, upgrades } from "hardhat";
import * as helpers from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { changeMultisigOwner } from "../scripts/deployment-utils/change-multisig-owner";
import { expect } from "chai";
import { DeploymentConfig, read } from "../scripts/deployment-utils/deploy";
import multsigInfoJson from "../multisig-owners.json";
import {
GnosisSafe,
LiquidityBridgeContract,
LiquidityBridgeContractAdmin,
LiquidityBridgeContractV2,
} from "../typechain-types";
import { deployUpgradeLibraries } from "../scripts/deployment-utils/upgrade-proxy";
import hre from "hardhat";
type MultisigInfo = Record<
string,
{
address: string;
owners?: string[];
}
>;
const { FORK_NETWORK_NAME } = process.env;
const multsigInfo: MultisigInfo = multsigInfoJson;
describe("Should change LBC owner to the multisig.ts", function () {
it("Should change the owner", async () => {
await checkForkedNetwork();
const networkName = FORK_NETWORK_NAME ?? "rskTestnet";
const lbcName = "LiquidityBridgeContract";
const addresses: Partial<DeploymentConfig> = read();
const networkDeployments: Partial<DeploymentConfig[string]> | undefined =
addresses[networkName];
const lbcAddress = networkDeployments?.LiquidityBridgeContract?.address;
const safeAddress = multsigInfo[networkName].address;
if (!lbcAddress) {
throw new Error(
"LiquidityBridgeContract proxy deployment info not found"
);
}
console.info(`LBC address: ${lbcAddress}`);
console.info(`Safe address: ${safeAddress}`);
const lbc = await ethers.getContractAt(lbcName, lbcAddress);
const lbcOwner = await lbc.owner();
console.info("LBC owner:", lbcOwner);
await helpers.impersonateAccount(lbcOwner);
const impersonatedSigner = await ethers.getSigner(lbcOwner);
const desiredBalance = ethers.toQuantity(ethers.parseEther("100"));
await ethers.provider.send("hardhat_setBalance", [
impersonatedSigner.address,
desiredBalance,
]);
await expect(
changeMultisigOwner(safeAddress, networkName, impersonatedSigner)
).to.not.be.reverted;
const newLbcOwner = await lbc.owner();
console.info("New LBC owner:", newLbcOwner);
await expect(
lbc.connect(impersonatedSigner).setProviderStatus(1, false)
).to.be.revertedWith("LBC005");
const safeContract = await ethers.getContractAt("GnosisSafe", safeAddress);
const opts = { verbose: true };
const libs = await deployUpgradeLibraries(networkName, opts);
const NewLbcFactory = await ethers.getContractFactory(
"LiquidityBridgeContractV2",
{
libraries: {
QuotesV2: libs.quotesV2,
BtcUtils: libs.btcUtils,
SignatureValidator: libs.signatureValidator,
},
}
);
const newLbcDeployed = await NewLbcFactory.deploy();
const newLbcAddress = await newLbcDeployed.getAddress();
await expect(
multisigExecProviderStatusChangeTransaction(safeContract, lbc)
).to.eventually.be.equal(true);
const adminAddress = await upgrades.erc1967.getAdminAddress(lbcAddress);
const adminContract = await ethers.getContractAt(
"LiquidityBridgeContractAdmin",
adminAddress
);
await expect(
adminContract.upgrade(lbcAddress, newLbcAddress)
).to.revertedWith("Ownable: caller is not the owner");
await expect(
multisigExecUpgradeTransaction(
safeContract,
lbc,
newLbcDeployed,
adminContract
)
).to.eventually.be.equal(true);
const newLbc = await ethers.getContractAt(
"LiquidityBridgeContractV2",
lbcAddress
);
await expect(newLbc.version()).to.eventually.be.equal("1.3.0");
});
it("Should change the owner using Tenderly", async () => {
await checkForkedNetwork();
const deploymentNetwork = hre.network.name;
const multisigAddress = multsigInfo[deploymentNetwork].address;
if (!multisigAddress || multisigAddress === "")
throw new Error("Multisig address not found for current network");
// Get the current owner of LiquidityBridgeContract
const deploymentData = read()[deploymentNetwork];
const proxyAddress = deploymentData.LiquidityBridgeContract.address;
if (!proxyAddress)
throw new Error(
`LiquidityBridgeContract address not found on network ${deploymentNetwork}`
);
const contract = await ethers.getContractAt(
"LiquidityBridgeContractV2",
proxyAddress
);
const currentOwner = await contract.owner();
console.info(`Current owner: ${currentOwner}`);
// Impersonate the current owner account
await helpers.impersonateAccount(currentOwner);
const impersonatedSigner = await ethers.getSigner(currentOwner);
// Set balance for the impersonated account
const desiredBalance = ethers.toQuantity(ethers.parseEther("100"));
await ethers.provider.send("hardhat_setBalance", [
impersonatedSigner.address,
desiredBalance,
]);
console.info(`Impersonated account: ${impersonatedSigner.address}`);
// Call changeMultisigOwner
await expect(
changeMultisigOwner(
multisigAddress,
deploymentNetwork,
impersonatedSigner
)
).to.not.be.reverted;
const newOwner = await contract.owner();
console.info(`New owner: ${newOwner}`);
// Verify ownership change
expect(newOwner.toLowerCase()).to.equal(multisigAddress.toLowerCase());
// Verify old owner can no longer perform owner functions
const adminAddress = await upgrades.erc1967.getAdminAddress(proxyAddress);
const adminContract = await ethers.getContractAt(
"LiquidityBridgeContractAdmin",
adminAddress
);
await expect(
adminContract.upgrade(proxyAddress, multisigAddress)
).to.revertedWith("Ownable: caller is not the owner");
console.info(
`Ownership of LiquidityBridgeContract proxy changed to multisig in ${deploymentNetwork}`
);
});
});
async function checkForkedNetwork() {
try {
await ethers.provider.send("evm_snapshot", []);
} catch (error) {
console.error("Not a forked network:", error);
}
}
function generateConcatenatedSignatures(owners: string[]) {
const concatenatedSignatures =
"0x" +
owners
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())) // SORT owners in ascending order
.map((owner) => {
return "0".repeat(24) + owner.slice(2) + "0".repeat(64) + "01";
})
.join("");
return concatenatedSignatures;
}
export async function multisigExecProviderStatusChangeTransaction(
safeContract: GnosisSafe,
lbc: LiquidityBridgeContract
): Promise<boolean> {
const callData = lbc.interface.encodeFunctionData("setProviderStatus", [
1,
false,
]);
console.info("Call data:", callData);
const nonce = await safeContract.nonce();
console.info("Nonce:", nonce);
const txData = {
to: await lbc.getAddress(),
value: 0,
data: callData,
operation: 0,
safeTxGas: 0,
baseGas: 0,
gasPrice: 0,
gasToken: ethers.ZeroAddress,
refundReceiver: ethers.ZeroAddress,
nonce: nonce,
signatures: "0x",
};
const owners = await safeContract.getOwners();
const desiredBalance = ethers.toQuantity(ethers.parseEther("100"));
await helpers.impersonateAccount(owners[0]);
const impersonateOwner1 = await ethers.getSigner(owners[0]);
await ethers.provider.send("hardhat_setBalance", [
impersonateOwner1.address,
desiredBalance,
]);
await helpers.impersonateAccount(owners[1]);
const impersonateOwner2 = await ethers.getSigner(owners[1]);
await ethers.provider.send("hardhat_setBalance", [
impersonateOwner2.address,
desiredBalance,
]);
await helpers.impersonateAccount(owners[2]);
const impersonateOwner3 = await ethers.getSigner(owners[2]);
await ethers.provider.send("hardhat_setBalance", [
impersonateOwner3.address,
desiredBalance,
]);
const transactionHash = await safeContract
.connect(impersonateOwner1)
.getTransactionHash(
txData.to,
txData.value,
txData.data,
txData.operation,
txData.safeTxGas,
txData.baseGas,
txData.gasPrice,
txData.gasToken,
txData.refundReceiver,
txData.nonce
);
console.info("Transaction hash:", transactionHash);
await safeContract.connect(impersonateOwner1).approveHash(transactionHash);
await safeContract.connect(impersonateOwner2).approveHash(transactionHash);
await safeContract.connect(impersonateOwner3).approveHash(transactionHash);
const signature = generateConcatenatedSignatures([
impersonateOwner1.address,
impersonateOwner2.address,
impersonateOwner3.address,
]);
console.info("Signature:", signature);
txData.signatures = signature;
const result = await safeContract.execTransaction(
txData.to,
txData.value,
txData.data,
txData.operation,
txData.safeTxGas,
txData.baseGas,
txData.gasPrice,
txData.gasToken,
txData.refundReceiver,
txData.signatures
);
return Boolean(result);
}
export async function multisigExecUpgradeTransaction(
safeContract: GnosisSafe,
lbc: LiquidityBridgeContract,
lbcV2: LiquidityBridgeContractV2,
adminContract: LiquidityBridgeContractAdmin
): Promise<boolean> {
const proxyAddress = await lbc.getAddress();
let result = false;
const callData = adminContract.interface.encodeFunctionData("upgrade", [
proxyAddress,
await lbcV2.getAddress(),
]);
console.info("Call data:", callData);
const nonce = await safeContract.nonce();
console.info("Nonce:", nonce);
const txData = {
to: await adminContract.getAddress(),
value: 0,
data: callData,
operation: 0,
safeTxGas: 0,
baseGas: 0,
gasPrice: 0,
gasToken: ethers.ZeroAddress,
refundReceiver: ethers.ZeroAddress,
nonce: nonce,
signatures: "0x",
};
const owners = await safeContract.getOwners();
const desiredBalance = ethers.toQuantity(ethers.parseEther("100"));
await helpers.impersonateAccount(owners[0]);
const impersonateOwner1 = await ethers.getSigner(owners[0]);
await ethers.provider.send("hardhat_setBalance", [
impersonateOwner1.address,
desiredBalance,
]);
await helpers.impersonateAccount(owners[1]);
const impersonateOwner2 = await ethers.getSigner(owners[1]);
await ethers.provider.send("hardhat_setBalance", [
impersonateOwner2.address,
desiredBalance,
]);
await helpers.impersonateAccount(owners[2]);
const impersonateOwner3 = await ethers.getSigner(owners[2]);
await ethers.provider.send("hardhat_setBalance", [
impersonateOwner3.address,
desiredBalance,
]);
const transactionHash = await safeContract
.connect(impersonateOwner1)
.getTransactionHash(
txData.to,
txData.value,
txData.data,
txData.operation,
txData.safeTxGas,
txData.baseGas,
txData.gasPrice,
txData.gasToken,
txData.refundReceiver,
txData.nonce
);
console.info("Transaction hash:", transactionHash);
await safeContract.connect(impersonateOwner1).approveHash(transactionHash);
await safeContract.connect(impersonateOwner2).approveHash(transactionHash);
await safeContract.connect(impersonateOwner3).approveHash(transactionHash);
const signature = generateConcatenatedSignatures([
impersonateOwner1.address,
impersonateOwner2.address,
impersonateOwner3.address,
]);
console.info("Signature:", signature);
txData.signatures = signature;
result = Boolean(
await safeContract.execTransaction(
txData.to,
txData.value,
txData.data,
txData.operation,
txData.safeTxGas,
txData.baseGas,
txData.gasPrice,
txData.gasToken,
txData.refundReceiver,
txData.signatures
)
);
return Boolean(result);
}