-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
469 lines (385 loc) · 17.2 KB
/
index.ts
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* eslint-disable @typescript-eslint/no-explicit-any */
import { extendConfig, subtask, task } from "hardhat/config";
import { crossdeployConfigExtender } from "./config";
import { networks, Network } from "./networks";
import {
PLUGIN_NAME,
TASK_VERIFY_SUPPORTED_NETWORKS,
TASK_VERIFY_SIGNER,
TASK_VERIFY_CONTRACT,
TASK_VERIFY_GASLIMIT,
} from "./constants";
import "./type-extensions";
import { InitializeCalldataStruct, StrategyStruct, ProposalStruct} from "../../types/contracts/target/Space";
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
import "@nomicfoundation/hardhat-ethers";
import { expect } from "chai";
// import { ethers } from "hardhat";
import { createInstances } from "./instance";
import { getSigners } from "./signers";
import { createTransaction } from "./utils";
import fhevmjs, { FhevmInstance } from "fhevmjs";
import { AbiCoder } from "ethers";
extendConfig(crossdeployConfigExtender);
task(
"crossdeploy",
"Deploys the contract across all predefined networks",
).setAction(async (_, hre) => {
await hre.run(TASK_VERIFY_SUPPORTED_NETWORKS);
await hre.run(TASK_VERIFY_SIGNER);
await hre.run(TASK_VERIFY_CONTRACT);
await hre.run(TASK_VERIFY_GASLIMIT);
await hre.run("compile");
if (hre.config.crossdeploy.contracts) {
const providers: any[] = [];
const wallets: any[] = [];
const signers: any[] = [];
// console.info("Deploying to:", hre.network.name);
const incoNetwork: Network = networks["inco"] as Network;
const targetNetwork: Network = networks["baseSepolia"] as Network;
const nets = [incoNetwork, targetNetwork];
[0, 1].map((i) => {
providers[i] = new hre.ethers.JsonRpcProvider(
nets[i].rpcUrl,
);
wallets[i] = new hre.ethers.Wallet(
hre.config.crossdeploy.signer,
providers[i],
);
signers[i] = wallets[i].connect(providers[i]);
});
// console.log("signers array -> " + signers[0].address);
const IncoContract = await hre.ethers.getContractFactory(
hre.config.crossdeploy.contracts[0],
);
const TargetContract = await hre.ethers.getContractFactory(
hre.config.crossdeploy.contracts[1],
);
const Space = await hre.ethers.getContractFactory(
hre.config.crossdeploy.contracts[2],
);
const VanillaAuthenticator = await hre.ethers.getContractFactory(
hre.config.crossdeploy.contracts[3],
);
const VanillaProposalValidationStrategy = await hre.ethers.getContractFactory(
hre.config.crossdeploy.contracts[4],
);
const VanillaVotingStrategy = await hre.ethers.getContractFactory(
hre.config.crossdeploy.contracts[5],
);
const VanillaExecutionStrategy = await hre.ethers.getContractFactory(
hre.config.crossdeploy.contracts[6],
);
// let defaultSigners = await hre.ethers.getSigners();
// console.log("default signers -> " + await defaultSigners[0]);
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
try {
console.info("\nDeploying contracts on Inco...");
//Contracts Deployed on Inco:
const incoContractInstance: any = await IncoContract.connect(signers[0]).deploy();
const incoContractAddr = await incoContractInstance.getAddress();
await incoContractInstance.waitForDeployment();
console.info("IncoContract -> ", incoContractAddr);
let defaultsigners = await getSigners(hre.ethers);
// console.log("default signers -> " + defaultsigners.alice.address);
let fhevmInstance = await createInstances(incoContractAddr, hre.ethers, defaultsigners);
// console.log("fhevmInstance -> " + fhevmInstance);
const token = fhevmInstance.alice.getPublicKey(incoContractAddr) || {
signature: "",
publicKey: "",
};
const VanillaExecutionStrategyInstance: any = await VanillaExecutionStrategy.connect(signers[0]).deploy(signers[0].address, 1); // random address as the address is of no use during testing
const VanillaExecutionStrategyAddr = await VanillaExecutionStrategyInstance.getAddress();
await VanillaExecutionStrategyInstance.waitForDeployment();
console.info("VanillaExecutionStrategy -> ", VanillaExecutionStrategyAddr);
console.info("\nDeploying contracts on baseSepolia...");
//Contracts Deployed on Base:
const targetContractInstance: any = await TargetContract.connect(signers[1]).deploy();
const targetContractAddr = await targetContractInstance.getAddress();
await targetContractInstance.waitForDeployment();
console.info("TargetContract -> ", targetContractAddr);
const SpaceInstance: any = await Space.connect(signers[1]).deploy();
const SpaceAddr = await SpaceInstance.getAddress();
await SpaceInstance.waitForDeployment();
console.info("Space -> ", SpaceAddr);
const VanillaAuthenticatorInstance: any = await VanillaAuthenticator.connect(signers[1]).deploy();
const VanillaAuthenticatorAddr = await VanillaAuthenticatorInstance.getAddress();
await VanillaAuthenticatorInstance.waitForDeployment();
console.info("VanillaAuthenticator -> ", VanillaAuthenticatorAddr);
const VanillaProposalValidationStrategyInstance: any = await VanillaProposalValidationStrategy.connect(signers[1]).deploy();
const VanillaProposalValidationStrategyAddr = await VanillaProposalValidationStrategyInstance.getAddress();
await VanillaProposalValidationStrategyInstance.waitForDeployment();
console.info("VanillaProposalValidationStrategy -> ", VanillaProposalValidationStrategyAddr);
const VanillaVotingStrategyInstance: any = await VanillaVotingStrategy.connect(signers[1]).deploy();
const VanillaVotingStrategyAddr = await VanillaVotingStrategyInstance.getAddress();
await VanillaVotingStrategyInstance.waitForDeployment();
console.info("VanillaVotingStrategy -> ", VanillaVotingStrategyAddr);
let defaultSigners = await hre.ethers.getSigners();
let data2voteAgainst = [
await defaultSigners[3].address,
1,
fhevmInstance.alice.encrypt8(0),
// "0xdead",
[[0,"0x"]],
""
];
console.log("data2voteAgainst - " + (AbiCoder.defaultAbiCoder().encode(["address", "uint256", "bytes", "tuple(uint8, bytes)[]", "string"], data2voteAgainst)).length);
{
console.log("\ninitializing Space contract \n");
let data0 : StrategyStruct = {
addr: VanillaProposalValidationStrategyAddr,
params: "0x"
}
let data1 : StrategyStruct = {
addr: VanillaVotingStrategyInstance,
params: "0x"
}
let data : InitializeCalldataStruct = {
owner: signers[1].address, // Example address
votingDelay: 0,
minVotingDuration: 0,
maxVotingDuration: 1000,
proposalValidationStrategy: data0,
proposalValidationStrategyMetadataURI: "proposalValidationStrategyMetadataURI",
daoURI: "SOC Test DAO",
metadataURI: "SOC Test Space",
votingStrategies: [data1],
votingStrategyMetadataURIs: ["votingStrategyMetadataURIs"],
authenticators: [VanillaAuthenticatorAddr],
_targetEndpoint: targetContractAddr
};
// console.log("owner before initialize: " + await contractSpace.owner());
try {
const txn = await SpaceInstance.initialize(data);
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("Transaction successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
// console.log("alice address -> " + addressSigner);
// console.log("owner after initialize: " + await contractSpace.owner());
// assert(addressSigner == contractSpace.owner());
console.log("space maxVotingDuration" + await SpaceInstance.maxVotingDuration());
}
console.log("\n making a proposal \n");
let data2propose =
[
signers[1].address,
"",
[
VanillaExecutionStrategyAddr, "0x"
],
"0x",
];
// console.log(AbiCoder.defaultAbiCoder().encode(["address", "string", "tuple(address, bytes)", "bytes"], data2propose));
// console.log("old proposal -> " + await contractSpace.proposals(1));
try {
const txn = await VanillaAuthenticatorInstance.authenticate(SpaceAddr, '0xaad83f3b', AbiCoder.defaultAbiCoder().encode(["address", "string", "tuple(address, bytes)", "bytes"], data2propose));
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("Transaction2 successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
console.log("new proposal -> " + await SpaceInstance.proposals(1));
{
console.log("\n initializing incoEndpoint and targetEndpoint \n");
try {
const txn = await incoContractInstance.initialize(targetContractAddr);
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("inco endpoint initization successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
try {
const txn = await targetContractInstance.initialize(incoContractAddr);
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("target endpoint initialization successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
}
{
console.log("\n voting \n");
let defaultSigners = await hre.ethers.getSigners();
// Voting on Target Chain
const eChoiceAgainst = fhevmInstance.alice.encrypt8(2);
const eChoiceFor1 = fhevmInstance.alice.encrypt8(1);
const eChoiceFor2 = fhevmInstance.alice.encrypt8(1);
const eChoiceAbstain = fhevmInstance.alice.encrypt8(0);
let data2voteAbstain = [
await defaultSigners[0].address,
1,
eChoiceAgainst,
[[0,"0x"]],
""
];
let data2voteFor1 = [
await defaultSigners[1].address,
1,
eChoiceFor1,
[[0,"0x"]],
""
];
let data2voteFor2 = [
await defaultSigners[2].address,
1,
eChoiceFor2,
[[0,"0x"]],
""
];
let data2voteAgainst = [
await defaultSigners[3].address,
1,
eChoiceAbstain,
// "0xdead",
[[0,"0x"]],
""
];
let eChoiceAbstainHash = hre.ethers.keccak256(eChoiceAbstain);
let eChoiceFor1Hash = hre.ethers.keccak256(eChoiceFor1);
let eChoiceFor2Hash = hre.ethers.keccak256(eChoiceFor2);
let eChoiceAgainstHash = hre.ethers.keccak256(eChoiceAgainst);
console.log("eChoiceAbstainHash - " + eChoiceAbstainHash);
console.log("eChoiceFor1Hash - " + eChoiceFor1Hash);
console.log("eChoiceFor2Hash - " + eChoiceFor2Hash);
console.log("eChoiceAgainstHash - " + eChoiceAgainstHash);
// console.log("votePower before vote -> " + (await contractSpace.votePower(1, 2)).toString());
console.log("current block number -> " + await hre.ethers.provider.getBlockNumber());
try {
const txn = await VanillaAuthenticatorInstance.authenticate(SpaceAddr, '0x954ee6da', AbiCoder.defaultAbiCoder().encode(["address", "uint256", "bytes", "tuple(uint8, bytes)[]", "string"], data2voteAgainst));
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("Against successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
try {
const txn = await VanillaAuthenticatorInstance.authenticate(SpaceAddr, '0x954ee6da', AbiCoder.defaultAbiCoder().encode(["address", "uint256", "bytes", "tuple(uint8, bytes)[]", "string"], data2voteFor1));
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("For1 successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
try {
const txn = await VanillaAuthenticatorInstance.authenticate(SpaceAddr, '0x954ee6da', AbiCoder.defaultAbiCoder().encode(["address", "uint256", "bytes", "tuple(uint8, bytes)[]", "string"], data2voteFor2));
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("For2 successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
try {
const txn = await VanillaAuthenticatorInstance.authenticate(SpaceAddr, '0x954ee6da', AbiCoder.defaultAbiCoder().encode(["address", "uint256", "bytes", "tuple(uint8, bytes)[]", "string"], data2voteAbstain));
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("Abstain successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
console.log("\n\n checking the vote mapping in incoEndpoint \n");
console.log("waiting for 28 seconds...");
await delay(28000);
console.log("waiting for 28 seconds...");
await delay(28000);
console.log("checking only for data2voteAbstainHash");
console.log(await incoContractInstance.getCollectChoiceHashStatus(eChoiceAbstainHash));
console.log(await incoContractInstance.getCollectChoiceData(eChoiceAbstainHash));
let For_votes = (await incoContractInstance.getVotePower(1, 1, token.publicKey)).toString();
let Abstain_votes = (await incoContractInstance.getVotePower(1, 2, token.publicKey)).toString();
let Against_votes = (await incoContractInstance.getVotePower(1, 0, token.publicKey)).toString();
console.log(For_votes);
console.log(Abstain_votes);
console.log(Against_votes);
// Verifying the vote count on Inco
console.log("For votes -> " + fhevmInstance.alice.decrypt(incoContractAddr, For_votes));
console.log("Abstain votes -> " + fhevmInstance.alice.decrypt(incoContractAddr, Abstain_votes));
console.log("Against votes -> " + fhevmInstance.alice.decrypt(incoContractAddr, Against_votes));
console.log("\n\n\n\n execution \n");
//Executing on Target Chain
let executionPayload = "0x";
try {
const txn = await SpaceInstance.execute(1, executionPayload);
console.log("Transaction hash:", txn.hash);
// Wait for 1 confirmation (adjust confirmations as needed)
await txn.wait(1);
console.log("execution successful!");
} catch (error) {
console.error("Transaction failed:", error);
// Handle the error appropriately (e.g., retry, notify user)
}
// Waiting for 30 seconds till the Execution is done on Inco
console.log("waiting 30 seconds till the execution is done...");
await delay(30000);
console.log("is executed - " + await incoContractInstance.getIsExecuted(1));
}
} catch (err) {
console.error(err);
}
}
});
subtask(TASK_VERIFY_SUPPORTED_NETWORKS).setAction(async (_, hre) => {
if (
!Object.keys(networks).includes(hre.network.name) ||
hre.network.name === "inco"
) {
throw new NomicLabsHardhatPluginError(
PLUGIN_NAME,
`The network you are trying to deploy to is not supported by this plugin.
The currently supported networks are ${Object.keys(networks).filter(n => n !== "inco")}.`,
);
}
});
subtask(TASK_VERIFY_SIGNER).setAction(async (_, hre) => {
if (!hre.config.crossdeploy.signer || hre.config.crossdeploy.signer === "") {
throw new NomicLabsHardhatPluginError(
PLUGIN_NAME,
`Please provide a signer private key. We recommend using Hardhat configuration variables.
See https://hardhat.org/hardhat-runner/docs/guides/configuration-variables.
E.g.: { [...], crossdeploy: { signer: vars.get("PRIVATE_KEY", "") }, [...] }.`,
);
}
});
subtask(TASK_VERIFY_CONTRACT).setAction(async (_, hre) => {
if (!hre.config.crossdeploy.contracts) {
throw new NomicLabsHardhatPluginError(
PLUGIN_NAME,
`Please specify a pair of contract names to be deployed.
E.g.: { [...], crossdeploy: { contracts: ["WERC20", "ERC20"] }, [...] }.`,
);
}
});
subtask(TASK_VERIFY_GASLIMIT).setAction(async (_, hre) => {
if (
hre.config.crossdeploy.gasLimit &&
hre.config.crossdeploy.gasLimit > 15 * 10 ** 6
) {
throw new NomicLabsHardhatPluginError(
PLUGIN_NAME,
`Please specify a lower gasLimit. Each block has currently
a target size of 15 million gas.`,
);
}
});