This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
350 lines (321 loc) · 9.83 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
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
/* global: console */
let rp = require("request-promise");
let program = require("commander");
let fs = require("fs");
let api = require("./api");
let axios = require("axios");
let _ = require("lodash");
var config = require("./config.json");
let provider = config.provider;
console.log("provider", provider);
let Web3 = require("web3");
let web3 = new Web3(provider, null, {});
let lastCommit = -1;
let lastReveal = -1;
let lastProposal = -1;
let lastElection = -1;
let lastVerification = -1;
let data = [];
let prevBlockNumber = undefined;
console.log("web3 version", web3.version);
program.version("0.0.3").description("Razor network");
program
.command("stake <amount> <address> <password>")
.alias("s")
.description("Stake some schells")
.action(async function (amount, address, password) {
try {
await api.login(address, password);
let res = await api.stake(amount, address).catch(console.log);
if (res) {
console.log("succesfully staked ", amount, " schells");
}
} catch (e) {
console.error(e);
}
process.exit(0);
});
program
.command("unstake <accountId>")
.alias("u")
.description("Unstake all schells")
.action(async function (accountId) {
try {
let account = (await web3.eth.personal.getAccounts())[Number(accountId)];
let res = await api.unstake(account).catch(console.log);
if (res) console.log("succesfully unstaked all schells");
} catch (e) {
console.error(e);
}
process.exit(0);
});
program
.command("withdraw <accountId>")
.alias("w")
.description(
"Withdraw all schells. Make sure schells are unstaked and unlocked"
)
.action(async function (accountId) {
try {
let account = (await web3.eth.personal.getAccounts())[Number(accountId)];
let res = await api.withdraw(account).catch(console.log);
if (res) console.log("succesfully withdrew all schells");
} catch (e) {
console.error(e);
}
process.exit(0);
});
program
.command("vote <account> <password>")
.alias("v")
.description(
"Start monitoring contract, commit, vote, propose and dispute automatically"
)
.action(async function (account, password) {
try {
await api.login(account, password);
// priceApi = Number(priceApi)
await main(account);
} catch (e) {
console.error(e);
}
});
program
.command("transfer <to> <amount> <from> <password>")
.alias("t")
.description("transfer schells")
.action(async function (to, amount, from, password) {
try {
await api.login(from, password);
let res = await api.transfer(to, amount, from).catch(console.log);
if (res) console.log("succesfully transferred");
} catch (e) {
console.error(e);
}
process.exit(0);
});
program
.command("create <password>")
.alias("c")
.description("create wallet with given password")
.action(async function (password) {
try {
let wallet = await web3.eth.accounts.create();
let walletEnc = await web3.eth.accounts.encrypt(
wallet.privateKey,
password
);
let json = JSON.stringify(walletEnc);
fs.writeFileSync(
"keys/" + wallet.address + ".json",
json,
"utf8",
function () {}
);
console.log(
wallet.address,
"created succesfully. fund this account with ETH and SCH before staking"
);
} catch (e) {
console.error(e);
}
process.exit(0);
});
program
.command("demo")
.alias("d")
.description("sample urls")
.action(async function () {
console.log(
"node index.js j 'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo' 'Global Quote[\"05. price\"]' true 1 0x0519cA2C7B556fa3699107EC8348cA2573e90A75 1",
"https://api.gemini.com/v1/pubticker/ethusd",
"last"
);
process.exit(0);
});
program
.command(
"createJob <url> <selector> <name> <repeat> <fee> <account> <password>"
)
.alias("j")
.description("create oracle query job")
.action(async function (url, selector, name, repeat, fee, account, password) {
try {
await api.login(account, password);
let res = await api
.createJob(url, selector, name, repeat === "true", fee, account)
.catch(console.log);
console.log(res);
if (res) console.log("succesfully created job");
} catch (e) {
console.error(e);
}
process.exit(0);
});
program.parse(process.argv);
let isWatchingEvents = false;
async function main(account) {
setInterval(async () => {
try {
const blockNumber = await web3.eth.getBlockNumber();
if (prevBlockNumber !== blockNumber) {
prevBlockNumber = blockNumber;
handleBlock({ number: blockNumber }, account);
}
} catch (error) {
console.error(error);
}
}, 2000);
console.log("subscribed");
isWatchingEvents = true;
}
async function handleBlock(blockHeader, account) {
try {
let state = await api.getDelayedState();
let epoch = await api.getEpoch();
let yourId = await api.getStakerId(account);
let balance = Number(await api.getStake(yourId)) / 1e18;
let ethBalance = Number(await web3.eth.getBalance(account)) / 1e18;
console.log(
"🔲 Block",
String(blockHeader.number),
"⌛ Epoch",
String(epoch),
"⏱️ State",
String(state),
"📒",
String(account),
"👤 Staker ID",
String(yourId),
"💰Stake",
String(balance),
"Ξ",
String(ethBalance)
);
if (balance < (await api.getMinStake()) / 1e18)
throw new Error("Stake is below minimum required. Cannot vote.");
if (state === 0) {
if (lastCommit < epoch) {
lastCommit = epoch;
let input = await web3.utils.soliditySha3(account, epoch);
let sig = await api.sign(input, account);
// console.log('sig', sig)
// console.log('sig.signature', sig.signature)
let secret = await web3.utils.soliditySha3(sig);
let jobs = await api.getActiveJobs();
console.log("jobs", jobs);
data = [];
let response;
let datum;
for (let i = 0; i < jobs.length; i++) {
// if (jobs[i].url === '') break
// console.log('i, job', i, jobs[i])
try {
response = await axios.get(jobs[i].url, { timeout: 60000 });
datum = _.get(response.data, jobs[i].selector);
if (isNaN(Number(datum)))
throw (
("Result is not a number",
"jobs[i].url",
"jobs[i].selector",
datum,
typeof datum)
);
datum = Math.floor(Number(datum) * 100000000);
data.push(datum);
} catch (e) {
console.error(e);
data.push(0);
}
}
if (data.length === 0) data = [0];
let tx = await api.commit(data, secret, account);
console.log(tx.events);
}
} else if (state === 1) {
if (lastReveal < epoch && data !== undefined) {
console.log("last revealed in epoch", lastReveal);
lastReveal = epoch;
let yourId = await api.getStakerId(account);
let staker = await api.getStaker(yourId);
console.log(
"stakerepochLastCommitted",
Number(staker.epochLastCommitted)
);
if (Number(staker.epochLastCommitted) !== epoch) {
console.log(
"Commitment for this epoch not found on network. Aborting reveal"
);
} else {
console.log(
"stakerepochLastRevealed",
Number(staker.epochLastRevealed)
);
let input = await web3.utils.soliditySha3(account, epoch);
let sig = await api.sign(input, account);
let secret = await web3.utils.soliditySha3(sig);
let tx = await api.reveal(data, secret, account, account);
console.log(tx);
}
}
} else if (state === 2) {
if (lastElection < epoch) {
lastElection = epoch;
if (lastProposal < epoch) {
console.log("Proposing block...");
lastProposal = epoch;
let tx = await api.propose(account);
console.log(tx);
}
}
} else if (state === 3) {
if (lastVerification < epoch) {
lastVerification = epoch;
let numProposedBlocks = Number(await api.getNumProposedBlocks(epoch));
if (numProposedBlocks > 5) numProposedBlocks = 5;
for (let i = 0; i < numProposedBlocks; i++) {
let proposedBlock = await api.getProposedBlock(epoch, i);
let medians = proposedBlock[1].map(Number);
let lowerCutoffs = proposedBlock[2].map(Number);
let higherCutoffs = proposedBlock[3].map(Number);
console.log(
"values proposed in block",
medians,
lowerCutoffs,
higherCutoffs
);
let block = await api.makeBlock();
console.log(
"Locally calculated median:",
block[0],
" lowerCutoff: ",
block[1],
" higherCutoff: ",
block[2]
);
if (
JSON.stringify(medians) !== JSON.stringify(block[0]) ||
JSON.stringify(lowerCutoffs) !== JSON.stringify(block[1]) ||
JSON.stringify(higherCutoffs) !== JSON.stringify(block[2])
) {
console.log(
"WARNING: BLOCK NOT MATCHING WITH LOCAL CALCULATIONS. local values:" +
block +
"block values:",
medians,
lowerCutoffs,
higherCutoffs
);
await api.dispute(account);
} else {
console.log(
"Proposed median matches with local calculations. Will not open dispute."
);
}
}
}
}
} catch (e) {
console.error(e);
}
}