forked from FidelVe/crosschain-voting-dapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
661 lines (612 loc) · 18.9 KB
/
lib.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
const IconService = require("icon-sdk-js");
const fs = require("fs");
const config = require("./config");
const { Web3 } = require("web3");
const { ethers } = require("ethers");
const { BigNumber } = ethers;
const {
IconBuilder,
IconConverter,
SignedTransaction,
HttpProvider,
IconWallet
} = IconService.default;
const { CallTransactionBuilder, CallBuilder } = IconBuilder;
const {
contract,
PK_BERLIN,
PK_SEPOLIA,
NID,
ICON_RPC_URL,
EVM_RPC_URL,
jarPath,
solPath,
XCALL_PRIMARY,
XCALL_SECONDARY,
NETWORK_LABEL_PRIMARY,
NETWORK_LABEL_SECONDARY,
deploymentsPath,
xcallAbiPath
} = config;
const HTTP_PROVIDER = new HttpProvider(ICON_RPC_URL);
const ICON_SERVICE = new IconService.default(HTTP_PROVIDER);
const ICON_WALLET = IconWallet.loadPrivateKey(PK_BERLIN);
const EVM_SERVICE = new Web3(EVM_RPC_URL);
const EVM_WALLET = EVM_SERVICE.eth.accounts.privateKeyToAccount(
PK_SEPOLIA,
true
);
EVM_SERVICE.eth.accounts.wallet.add(EVM_WALLET);
/*
* getIcoNContractByteCode - returns the byte code of the contract
* @returns {string} - the byte code of the contract
* @throws {Error} - if there is an error reading the contract
*/
function getIconContractByteCode() {
try {
return fs.readFileSync(jarPath).toString("hex");
} catch (e) {
console.log(e);
throw new Error("Error reading contract info");
}
}
/*
* isDeployed - checks if the contract is deployed
* @returns {boolean} - true if the contract is deployed, false otherwise
* @throws {Error} - if there is an error checking the deployments
*/
function isDeployed() {
try {
if (!fs.existsSync(deploymentsPath)) {
return false;
}
return true;
} catch (e) {
console.log(e);
throw new Error("Error checking deployments");
}
}
/*
* saveDeployments - saves the deployments
* @param {object} deployments - the deployments to save
* @throws {Error} - if there is an error saving the deployments
*/
function saveDeployments(deployments) {
try {
fs.writeFileSync(deploymentsPath, JSON.stringify(deployments));
} catch (e) {
console.log(e);
throw new Error("Error saving deployments");
}
}
/*
* getDeployments - returns the deployments
* @returns {object} - the deployments
* @throws {Error} - if there is an error reading the deployments
*/
function getDeployments() {
try {
return JSON.parse(fs.readFileSync(deploymentsPath));
} catch (e) {
console.log(e);
throw new Error("Error reading deployments");
}
}
/*
* getEvmContract - returns the EVM contract
* @param {string} abiPath - the path to the EVM contract abi
* @returns {object} - the EVM contract
* @throws {Error} - if there is an error reading the EVM contract
*/
function getEvmContract(abiPath) {
try {
const result = {
abi: null,
bytecode: null
};
const contract = JSON.parse(fs.readFileSync(abiPath));
result.abi = contract.abi;
result.bytecode = contract.bytecode;
return result;
} catch (e) {
console.log(e);
throw new Error("Error reading EVM contract info");
}
}
/*
* getDappContract - returns the Dapp contract
* @returns {object} - the Dapp contract
* @throws {Error} - if there is an error reading the Dapp contract
*/
function getDappContract() {
return getEvmContract(solPath);
}
/*
* getXcallContract - returns the Xcall contract
* @returns {object} - the Xcall contract
* @throws {Error} - if there is an error reading the Xcall contract
*/
function getXcallContract() {
return getEvmContract(xcallAbiPath);
}
/*
* getIconDappDeploymentsParams - returns the params for the Icon contract
* @param {string} label - the label of the network
* @param {string} dappContract - the address of the Dapp contract
* @returns {object} - the params for the Icon contract
* @throws {Error} - if there is an error getting the params
*/
function getIconDappDeploymentsParams(label, dappContract) {
const result = {
_sourceXCallContract: XCALL_PRIMARY,
_destinationBtpAddress: getBtpAddress(label, dappContract)
};
return result;
}
/*
* getBtpAddress - returns the BTP address
* @param {string} label - the label of the network
* @param {string} address - the address of the contract
* @returns {string} - the BTP address
* @throws {Error} - if there is an error getting the BTP address
*/
function getBtpAddress(label, address) {
return `btp://${label}/${address}`;
}
/*
* deployIconContract - deploys the contract on ICON
* @param {object} params - the params for the Icon contract
* @returns {object} - the result of the transaction
* @throws {Error} - if there is an error deploying the contract
*/
async function deployIconContract(params) {
try {
const content = getIconContractByteCode();
const payload = new IconBuilder.DeployTransactionBuilder()
.contentType("application/java")
.content(`0x${content}`)
.params(params)
.from(ICON_WALLET.getAddress())
.to(contract.icon.chain)
.nid(NID)
.version(3)
.timestamp(new Date().getTime() * 1000)
.stepLimit(IconConverter.toBigNumber(2500000000))
.build();
const signedTx = new SignedTransaction(payload, ICON_WALLET);
return await ICON_SERVICE.sendTransaction(signedTx).execute();
} catch (e) {
console.log("error deploying contract", e);
throw new Error("Error deploying contract");
}
}
/*
* getScoreApi - returns the abi of the contract
* @param {string} contract - the address of the contract
* @returns {object} - the abi of the contract
* @throws {Error} - if there is an error getting the abi
*/
async function getScoreApi(contract) {
try {
return await ICON_SERVICE.getScoreApi(contract).execute();
} catch (e) {
console.log("error getting abi", e);
throw new Error("Error getting abi");
}
}
/*
* filterEventICON - filters the event logs
* @param {object} eventlogs - the event logs
* @param {string} sig - the signature of the event
* @param {string} address - the address of the contract
* @returns {object} - the filtered event logs
* @throws {Error} - if there is an error filtering the event logs
*/
async function filterEventICON(eventlogs, sig, address) {
return eventlogs.filter(event => {
return (
event.indexed &&
event.indexed[0] === sig &&
(!address || address === event.scoreAddress)
);
});
}
/*
* filterCallMessageSentEvent - filters the CallMessageSent event logs
* @param {object} eventlogs - the event logs
* @returns {object} - the filtered event logs
* @throws {Error} - if there is an error filtering the event logs
*/
async function filterCallMessageSentEvent(eventlogs) {
return filterEventICON(
eventlogs,
"CallMessageSent(Address,str,int,int)",
XCALL_PRIMARY
);
}
/*
* parseCallMessageSentEvent - parses the CallMessageSent event logs
* @param {object} event - the event logs
* @returns {object} - the parsed event logs
* @throws {Error} - if there is an error parsing the event logs
*/
async function parseCallMessageSentEvent(event) {
const indexed = event[0].indexed || [];
const data = event[0].data || [];
return {
_from: indexed[1],
_to: indexed[2],
_sn: BigNumber.from(indexed[3]),
_nsn: BigNumber.from(data[0])
};
}
/*
* sleep - sleeps for the specified time
* @param {number} ms - the time to sleep
* @returns {object} - async function
*/
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/*
* getTxResult - gets the transaction result
* @param {string} txHash - the transaction hash
* @returns {object} - the transaction result
* @throws {Error} - if there is an error getting the transaction result
*/
async function getTxResult(txHash) {
const maxLoops = 10;
let loop = 0;
while (loop < maxLoops) {
try {
return await ICON_SERVICE.getTransactionResult(txHash).execute();
} catch (e) {
console.log(`txResult (pass ${loop}): ${e}`);
loop++;
await sleep(1000);
}
}
}
/*
* filterCallExecutedEventEvm - filters the CallExecuted event logs
* @param {string} id - the id of the event
* @returns {object} - the filtered event logs
* @throws {Error} - if there is an error filtering the event logs
*/
function filterCallExecutedEventEvm(id) {
const xcallContract = getXcallContractEVM();
const callMessageFilters = xcallContract.filters.CallExecuted(id);
return callMessageFilters;
}
/*
* filterCallMessageEventEvm - filters the CallMessage event logs
* @param {string} iconDappAddress - the address of the ICON dapp
* @param {string} evmDappAddress - the address of the EVM dapp
* @param {string} sn - the serial number cross chain transaction
* @returns {object} - the filtered event logs
* @throws {Error} - if there is an error filtering the event logs
*/
function filterCallMessageEventEvm(iconDappAddress, evmDappAddress, sn) {
const btpAddressSource = getBtpAddress(
NETWORK_LABEL_PRIMARY,
iconDappAddress
);
const xcallContract = getXcallContractEVM();
const callMessageFilters = xcallContract.filters.CallMessage(
btpAddressSource,
evmDappAddress,
sn
);
return callMessageFilters;
}
/*
* getVotesFromEVM - gets the votes from the EVM chain
* @param {string} contractAddress - the address of the contract
* @returns {object} - the votes
* @throws {Error} - if there is an error getting the votes
*/
async function getVotesFromEVM(contractAddress) {
const contractObject = getDappContractObject(contractAddress);
return await contractObject.getVotes();
}
/*
* getDappContractObject - gets the dapp contract object
* @param {string} contractAddress - the address of the contract
* @returns {object} - the dapp contract object
* @throws {Error} - if there is an error getting the dapp contract object
*/
function getDappContractObject(contractAddress) {
try {
const { abi } = getDappContract();
return getContractObjectEVM(abi, contractAddress);
} catch (e) {
console.log(e);
throw new Error("Error getting dapp contract");
}
}
/*
* getXcallContractEVM - gets the xcall contract object
* @returns {object} - the xcall contract object
* @throws {Error} - if there is an error getting the xcall contract object
*/
function getXcallContractEVM() {
try {
const { abi } = getXcallContract();
return getContractObjectEVM(abi, XCALL_SECONDARY);
} catch (e) {
console.log(e);
throw new Error("Error getting Xcall contract");
}
}
/*
* getContractObjectEVM - gets the contract object
* @param {object} abi - the abi of the contract
* @param {string} address - the address of the contract
* @returns {object} - the contract object
* @throws {Error} - if there is an error getting the contract object
*/
function getContractObjectEVM(abi, address) {
try {
const provider = new ethers.providers.JsonRpcProvider(EVM_RPC_URL);
const signer = new ethers.Wallet(PK_SEPOLIA, provider);
const contractObject = new ethers.Contract(address, abi, signer);
return contractObject;
} catch (e) {
console.log(e);
throw new Error("Error getting contract object");
}
}
/*
* waitEventEVM - waits for the event to be emitted
* @param {object} filterCM - the filter for the event
* @returns {object} - the event logs
* @throws {Error} - if there is an error waiting for the event
*/
async function waitEventEVM(filterCM) {
const contract = getXcallContractEVM();
let height = await contract.provider.getBlockNumber();
let next = height + 1;
console.log("block height", height);
while (true) {
if (height == next) {
await sleep(1000);
next = (await contract.provider.getBlockNumber()) + 1;
continue;
}
for (; height < next; height++) {
console.log(`waitEventEvmChain: ${height} -> ${next}`);
const events = await contract.queryFilter(filterCM, height);
if (events.length > 0) {
return events;
}
}
}
}
/*
* executeCallEvm - calls the executeCall method of the xcall contract
* @param {string} id - the id of the cross chain transaction
* @param {string} data - the data of the cross chain transaction
* @returns {object} - the transaction receipt
* @throws {Error} - if there is an error executing the call
*/
async function executeCallEvm(id, data) {
try {
const contract = getXcallContractEVM();
return await sendSignedTxEVM(contract, "executeCall", id, data);
} catch (e) {
console.log(e);
throw new Error("Error executing call");
}
}
/*
* sendSignedTxEVM - sends the signed transaction
* @param {object} contract - the contract object
* @param {string} method - the method to call
* @param {any[]} args - the arguments of the method
* @returns {object} - the transaction receipt
* @throws {Error} - if there is an error sending the signed transaction
*/
async function sendSignedTxEVM(contract, method, ...args) {
const txParams = { gasLimit: 15000000 };
const tx = await contract[method](...args, txParams);
const receipt = await tx.wait(1);
return receipt;
}
/*
* callDappContractMethod - calls the dapp contract method
* @param {string} method - the method to call
* @param {string} contract - the address of the contract
* @param {boolean} useRollback - whether to use rollback
* @returns {object} - the transaction receipt
* @throws {Error} - if there is an error calling the dapp contract method
*/
async function callDappContractMethod(method, contract, useRollback = false) {
try {
const fee = await getFeeFromIcon(useRollback);
const txObj = new CallTransactionBuilder()
.from(ICON_WALLET.getAddress())
.to(contract)
.stepLimit(IconConverter.toBigNumber(20000000))
.nid(IconConverter.toBigNumber(NID))
.nonce(IconConverter.toBigNumber(1))
.version(IconConverter.toBigNumber(3))
.timestamp(new Date().getTime() * 1000)
.method(method)
.value(fee)
.build();
const signedTx = new SignedTransaction(txObj, ICON_WALLET);
return await ICON_SERVICE.sendTransaction(signedTx).execute();
} catch (e) {
console.log(e);
throw new Error("Error calling contract method");
}
}
/*
* voteYesFromIcon - calls the voteYes method of the dapp contract
* @param {string} contract - the address of the contract
* @param {boolean} useRollback - whether to use rollback
* @returns {object} - the transaction receipt
* @throws {Error} - if there is an error voting yes
*/
async function voteYesFromIcon(contract, useRollback = false) {
try {
return await callDappContractMethod("voteYes", contract, useRollback);
} catch (e) {
console.log(e);
throw new Error("Error voting yes");
}
}
/*
* voteNoFromIcon - calls the voteNo method of the dapp contract
* @param {string} contract - the address of the contract
* @param {boolean} useRollback - whether to use rollback
* @returns {object} - the transaction receipt
* @throws {Error} - if there is an error voting no
*/
async function voteNoFromIcon(contract, useRollback = false) {
try {
return await callDappContractMethod("voteNo", contract, useRollback);
} catch (e) {
console.log(e);
throw new Error("Error voting no");
}
}
/*
* getFeeFromIcon - calls the getFee method of the xcall contract
* @param {boolean} useRollback - whether to use rollback
* @returns {object} - the transaction receipt
* @throws {Error} - if there is an error getting the fee
*/
async function getFeeFromIcon(useRollback = false) {
try {
const params = {
_net: NETWORK_LABEL_SECONDARY,
_rollback: useRollback ? "0x1" : "0x0"
};
const txObj = new CallBuilder()
.to(XCALL_PRIMARY)
.method("getFee")
.params(params)
.build();
return await ICON_SERVICE.call(txObj).execute();
} catch (e) {
console.log("error getting fee", e);
throw new Error("Error getting fee");
}
}
/*
* deployEvm - deploys the dapp contract on the EVM chain
* @returns {string} - the address of the deployed contract
* @throws {Error} - if there is an error deploying the contract
*/
async function deployEvm() {
try {
console.log("\n # Deploying contract on EVM chain...");
const { abi, bytecode } = getDappContract();
const contract = new EVM_SERVICE.eth.Contract(abi);
// contract.options.data = bytecode;
const deployTx = contract.deploy({
data: bytecode,
arguments: [XCALL_SECONDARY]
});
const deployedContract = await deployTx
.send({
from: EVM_WALLET.address,
gas: await deployTx.estimateGas()
})
.once("transactionHash", txHash => {
console.log("Mining deployment transaction...");
console.log("txHash", txHash);
});
return deployedContract.options.address;
} catch (e) {
console.log(e);
throw new Error("Error deploying contract on EVM chain");
}
}
/*
* deployIcon - deploys the dapp contract on the ICON chain
* @param {string} evmDappContract - the address of the EVM dapp contract
* @returns {string} - the address of the deployed contract
* @throws {Error} - if there is an error deploying the contract
*/
async function deployIcon(evmDappContract) {
try {
console.log("\n # Deploying contract on ICON chain...");
const params = getIconDappDeploymentsParams(
NETWORK_LABEL_SECONDARY,
evmDappContract
);
console.log("\n# Params for contract deployment on ICON:", params);
const receipt = await deployIconContract(params);
console.log("\n# Receipt for contract deployment on ICON:", receipt);
const txResult = await getTxResult(receipt);
console.log("\n# TxResult for contract deployment on ICON:", txResult);
const scoreAddress = txResult["scoreAddress"];
const scoreApi = await getScoreApi(scoreAddress);
const abi = scoreApi.getList();
console.log(
"\n# ScoreApi for contract deployment on ICON:",
JSON.stringify(abi)
);
return scoreAddress;
} catch (e) {
console.log(e);
throw new Error("Error deploying contract ICON chain");
}
}
/*
* strToHex - converts a string to hex
* @param {string} str - the string to convert
* @returns {string} - the hex string
* @throws {Error} - if there is an error converting the string
*/
function strToHex(str) {
var hex = "";
for (var i = 0; i < str.length; i++) {
hex += "" + str.charCodeAt(i).toString(16);
}
return "0x" + hex;
}
/*
* strToHexPadded - converts a string to hex and pads it
* @param {string} str - the string to convert
* @returns {string} - the hex string
* @throws {Error} - if there is an error converting the string
*/
function strToHexPadded(str) {
var hex = "";
for (var i = 0; i < str.length; i++) {
hex +=
"" +
str
.charCodeAt(i)
.toString(16)
.padStart(2, "0");
}
return "0x" + hex;
}
const lib = {
deployIconContract,
getTxResult,
config,
getScoreApi,
getIconDappDeploymentsParams,
deployIcon,
deployEvm,
isDeployed,
saveDeployments,
getDeployments,
strToHex,
strToHexPadded,
voteYesFromIcon,
voteNoFromIcon,
filterCallMessageSentEvent,
parseCallMessageSentEvent,
filterCallMessageEventEvm,
waitEventEVM,
executeCallEvm,
filterCallExecutedEventEvm,
getVotesFromEVM
};
module.exports = lib;