forked from FidelVe/crosschain-voting-dapp-with-rollback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
215 lines (192 loc) · 6.66 KB
/
index.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
// Imports
const {
deployIcon,
deployEvm,
isDeployed,
getDeployments,
saveDeployments,
voteYesFromIcon,
getTxResult,
filterCallMessageSentEvent,
parseCallMessageSentEvent,
filterCallMessageEventEvm,
waitEventEVM,
executeCallEvm,
filterCallExecutedEventEvm,
getVotesFromEVM,
getVotesCapFromEVM,
waitResponseMessageEventICON,
waitRollbackMessageEventICON,
getVotesFromICON,
executeRollbackICON
} = require("./utils/lib");
/*
* Deploy script
*/
async function deploy() {
try {
const contracts = {
primary: null,
secondary: null
};
// deploying EVM contract
const evmContractAddress = await deployEvm();
console.log("\n# deployed EVM contract address:", evmContractAddress);
// deploying ICON contract
const iconContractAddress = await deployIcon(evmContractAddress);
console.log("\n# deployed ICON contract address:", iconContractAddress);
contracts.secondary = evmContractAddress;
contracts.primary = iconContractAddress;
return contracts;
} catch (e) {
console.log("error running deployments", e);
}
}
/*
* Tests
* @param {Object} contracts
* @param {string} contracts.primary - ICON contract address
* @param {string} contracts.secondary - EVM contract address
* @returns {Promise<void>}
*/
async function tests(contracts, rollback = false) {
try {
// vote yes from icon
const voteYesFromIconResult = await voteYesFromIcon(contracts.primary);
console.log("\n# vote yes from icon result:", voteYesFromIconResult);
// get tx result
const txResult = await getTxResult(voteYesFromIconResult);
console.log("\n# tx result for calling voteYes:", txResult.txHash);
// filter CallMessageSent event
const callMessageSentEvent = await filterCallMessageSentEvent(
txResult.eventLogs
);
console.log("\n# CallMessageSent event:", callMessageSentEvent);
// parse CallMessageSent event
const parsedCallMessageSentEvent = await parseCallMessageSentEvent(
callMessageSentEvent
);
console.log(
"\n# parsed CallMessageSent event:",
parsedCallMessageSentEvent
);
// _sn from source
const snFromSource = parsedCallMessageSentEvent._sn;
// filter CallMessage event evm
const callMessageEventEvmFilters = filterCallMessageEventEvm(
contracts.primary,
contracts.secondary,
snFromSource
);
console.log(
"\n# CallMessage event evm filters:",
callMessageEventEvmFilters
);
// wait for CallMessage event evm
const eventsEvm = await waitEventEVM(callMessageEventEvmFilters);
// fetch messageId from CallMessage event evm
const messageId = eventsEvm[0].args._reqId;
console.log("\n# Message ID:", messageId);
// fetch data from CallMessage event evm
const data = eventsEvm[0].args._data;
console.log("\n# events params:");
console.log(JSON.stringify(eventsEvm[0].args));
// invoke execute call on destination chain
console.log("\n# invoking execute call on destination chain");
const executeCallTxHash = await executeCallEvm(messageId, data);
console.log("\n# execute call tx hash:", executeCallTxHash.transactionHash);
// filter CallExecuted event evm
const callExecutedEventEvmFilters = filterCallExecutedEventEvm(messageId);
console.log(
"\n# callExecuted event evm filters:",
callExecutedEventEvmFilters
);
// wait for CallExecuted event evm
const eventsEvm2 = await waitEventEVM(callExecutedEventEvmFilters);
console.log("\n# events params:");
console.log(JSON.stringify(eventsEvm2[0].args));
if (rollback) {
// execute logic with rollback because current votes is equal or grater than votes cap
// fetch ResponseMessage event on origin chain
const responseMessageEvent = await waitResponseMessageEventICON(
snFromSource
);
console.log("\n# ResponseMessage event:", responseMessageEvent);
// fetch RollbackMessage event on origin chain
const rollbackMessageEvent = await waitRollbackMessageEventICON(
snFromSource
);
console.log("\n# RollbackMessage event:", rollbackMessageEvent);
// fetch votes from origin chain before rollback
const votesFromICONBeforeRollback = await getVotesFromICON(
contracts.primary
);
console.log(
"\n# votes from ICON before rollback:",
votesFromICONBeforeRollback
);
// call the payable method executeRollback on the xcall contract of the origin chain
console.log("\n# invoking executeRollback call on origin chain");
const executeRollbackTxHash = await executeRollbackICON(
rollbackMessageEvent.indexed[1]
);
// get tx result
const executeRollbackTxResult = await getTxResult(executeRollbackTxHash);
console.log(
"\n# tx result for calling executeRollback:",
executeRollbackTxResult.txHash
);
// fetch votes from origin chain after rollback
const votesFromICONAfterRollback = await getVotesFromICON(
contracts.primary
);
console.log(
"\n# votes from ICON after rollback:",
votesFromICONAfterRollback
);
// fetch votes from destination chain after rollback
const votesFromEVM = await getVotesFromEVM(contracts.secondary);
console.log("\n# votes from EVM:", votesFromEVM);
}
} catch (e) {
console.log("error running tests", e);
}
}
/*
* Main
* @returns {Promise<void>}
*/
async function main() {
try {
// check if contracts have been deployed already
let contracts = null;
if (isDeployed()) {
console.log("\n# using deployed contracts");
contracts = getDeployments();
} else {
console.log("\n# deploying contracts");
contracts = await deploy();
saveDeployments(contracts);
}
if (contracts !== null) {
console.log("\n# deployed contracts:", contracts);
// check votes ledger on destination chain
const votesFromEVM = await getVotesFromEVM(contracts.secondary);
const votesCap = await getVotesCapFromEVM(contracts.secondary);
const votesCapParsed = votesCap.toString();
console.log("\n# votes cap from EVM:", votesCapParsed);
const sum = votesFromEVM[0].add(votesFromEVM[1]).toString();
console.log("\n# votes from EVM:", votesFromEVM);
console.log("\n# sum of votes from EVM:", sum);
for (let i = Number(sum); i < Number(votesCapParsed); i++) {
// vote until votes cap is reached
await tests(contracts);
}
// execute logic with rollback because current votes is equal or grater than votes cap
await tests(contracts, true);
}
} catch (e) {
console.log("error running main", e);
}
}
main();