UNPKG

12.2 kBJavaScriptView Raw
1var fs = require('fs');
2var Api = require('../helpers/api.js');
3var aschJS = require('asch-js');
4
5var globalOptions;
6
7function getApi() {
8 return new Api({host: globalOptions.host, port: globalOptions.port, mainnet: !!globalOptions.main});
9}
10
11function pretty(obj) {
12 return JSON.stringify(obj, null, 2);
13}
14
15function openAccount(secret) {
16 getApi().post('/api/accounts/open', {secret: secret}, function (err, result) {
17 console.log(err || pretty(result.account));
18 });
19}
20
21function openAccountByPublicKey(publicKey) {
22 getApi().post('/api/accounts/open2', {publicKey: publicKey}, function (err, result) {
23 console.log(err || pretty(result.account));
24 });
25}
26
27function getHeight() {
28 getApi().get('/api/blocks/getHeight', function (err, result) {
29 console.log(err || result.height);
30 });
31}
32
33function getBlockStatus() {
34 getApi().get('/api/blocks/getStatus', function (err, result) {
35 console.log(err || pretty(result));
36 });
37}
38
39function getBalance(address) {
40 var params = {address: address};
41 getApi().get('/api/accounts/getBalance', params, function (err, result) {
42 console.log(err || result.balance);
43 });
44}
45
46function getAccount(address) {
47 var params = {address: address};
48 getApi().get('/api/accounts/', params, function (err, result) {
49 console.log(err || pretty(result.account));
50 });
51}
52
53function getVotedDelegates(address, options) {
54 var params = {
55 address: address,
56 limit: options.limit,
57 offset: options.offset
58 };
59 getApi().get('/api/accounts/delegates', params, function (err, result) {
60 console.log(err || result);
61 });
62}
63
64function getDelegates(options) {
65 var params = {
66 limit: options.limit,
67 offset: options.offset,
68 orderBy: options.sort || "rate:asc"
69 };
70 getApi().get('/api/delegates/', params, function (err, result) {
71 console.log(err || pretty(result.delegates));
72 });
73}
74
75function getDelegatesCount() {
76 getApi().get('/api/delegates/count', function (err, result) {
77 console.log(err || result.count);
78 });
79}
80
81function getVoters(publicKey) {
82 var params = {publicKey: publicKey};
83 getApi().get('/api/delegates/voters', params, function (err, result) {
84 console.log(err || pretty(result.accounts));
85 });
86}
87
88function getDelegateByPublicKey(publicKey) {
89 var params = {publicKey: publicKey};
90 getApi().get('/api/delegates/get', params, function (err, result) {
91 console.log(err || pretty(result.delegate));
92 });
93}
94
95function getDelegateByUsername(username) {
96 var params = {username: username};
97 getApi().get('/api/delegates/get', params, function (err, result) {
98 console.log(err || pretty(result.delegate));
99 });
100}
101
102function getBlocks(options) {
103 var params = {
104 limit: options.limit,
105 orderBy: options.sort,
106 offset: options.offset,
107 totalAmount: options.totalAmount,
108 totalFee: options.totalFee,
109 reward: options.reward,
110 generatorPublicKey: options.generatorPublicKey
111 };
112 getApi().get('/api/blocks/', params, function (err, result) {
113 console.log(err || pretty(result));
114 });
115}
116
117function getBlockById(id) {
118 var params = {id: id};
119 getApi().get('/api/blocks/get', params, function (err, result) {
120 console.log(err || pretty(result.block));
121 });
122}
123
124function getBlockByHeight(height) {
125 var params = {height: height};
126 getApi().get('/api/blocks/get', params, function (err, result) {
127 console.log(err || pretty(result.block));
128 });
129}
130
131function getPeers(options) {
132 var params = {
133 limit: options.limit,
134 orderBy: options.sort,
135 offset: options.offset,
136 state: options.state,
137 os: options.os,
138 port: options.port,
139 version: options.version
140 };
141 // var liskOptions = {host:'login.lisk.io', port:80};
142 getApi().get('/api/peers/', params, function (err, result) {
143 console.log(err || pretty(result.peers));
144 });
145}
146
147function getUnconfirmedTransactions(options) {
148 var params = {
149 senderPublicKey: options.key,
150 address: options.address
151 };
152 getApi().get('/api/transactions/unconfirmed', params, function (err, result) {
153 console.log(err || pretty(result.transactions));
154 });
155}
156
157function getTransactions(options) {
158 var params = {
159 blockId: options.blockId,
160 limit: options.limit,
161 orderBy: options.sort,
162 offset: options.offset,
163 type: options.type,
164 senderPublicKey: options.senderPublicKey,
165 senderId: options.senderId,
166 recipientId: options.recipientId,
167 amount: options.amount,
168 fee: options.fee
169 };
170 getApi().get('/api/transactions/', params, function (err, result) {
171 console.log(err || pretty(result.transactions));
172 });
173}
174
175function getTransaction(id) {
176 var params = {id: id};
177 getApi().get('/api/transactions/get', params, function (err, result) {
178 console.log(err || pretty(result.transaction));
179 });
180}
181
182function sendMoney(options) {
183 // var params = {
184 // secret: options.secret,
185 // secondSecret: options.secondSecret,
186 // recipientId: options.to,
187 // amount: Number(options.amount)
188 // };
189 // getApi().put('/api/transactions/', params, function (err, result) {
190 // console.log(err || result);
191 // });
192 var trs = aschJS.transaction.createTransaction(
193 options.to,
194 options.amount * 100000000,
195 options.secret,
196 options.secondSecret
197 );
198 getApi().broadcastTransaction(trs, function (err, result) {
199 console.log(err || result.success);
200 });
201}
202
203function registerDelegate(options) {
204 // var params = {
205 // secret: options.secret,
206 // username: options.username,
207 // secondSecret: options.secondSecret,
208 // };
209 // getApi().put('/api/delegates/', params, function (err, result) {
210 // console.log(err || result);
211 // });
212 var trs = aschJS.delegate.createDelegate(
213 options.secret,
214 options.username,
215 options.secondSecret
216 );
217 getApi().broadcastTransaction(trs, function (err, result) {
218 console.log(err || result.success);
219 });
220}
221
222function vote(secret, publicKeys, op, secondSecret) {
223 var votes = publicKeys.split(',').map(function (el) {
224 return op + el;
225 });
226 var trs = aschJS.vote.createVote(
227 secret,
228 votes,
229 secondSecret
230 );
231 getApi().broadcastTransaction(trs, function (err, result) {
232 console.log(err || result.success);
233 });
234}
235
236function upvote(options) {
237 vote(options.secret, options.publicKeys, '+', options.secondSecret);
238}
239
240function downvote(options) {
241 vote(options.secret, options.publicKeys, '-', options.secondSecret);
242}
243
244function setSecondSecret(options) {
245 var trs = aschJS.signature.createSignature(options.secret, options.secondSecret);
246 getApi().broadcastTransaction(trs, function (err, result) {
247 console.log(err || result.success);
248 });
249}
250
251function registerDapp(options) {
252 if (!options.metafile || !fs.existsSync(options.metafile)) {
253 console.error("Error: invalid params, dapp meta file must exists");
254 return;
255 }
256 var dapp = JSON.parse(fs.readFileSync(options.metafile, 'utf8'));
257 var trs = aschJS.dapp.createDapp(options.secret, options.secondSecret, dapp);
258 getApi().broadcastTransaction(trs, function (err, result) {
259 console.log(err || result.success);
260 });
261}
262
263module.exports = function(program) {
264 globalOptions = program;
265
266 program
267 .command("getheight")
268 .description("get block height")
269 .action(getHeight);
270
271 program
272 .command("getblockstatus")
273 .description("get block status")
274 .action(getBlockStatus);
275
276 program
277 .command("openaccount [secret]")
278 .description("open your account and get the infomation by secret")
279 .action(openAccount);
280
281 program
282 .command("openaccountbypublickey [publickey]")
283 .description("open your account and get the infomation by publickey")
284 .action(openAccountByPublicKey);
285
286 program
287 .command("getbalance [address]")
288 .description("get balance by address")
289 .action(getBalance);
290
291 program
292 .command("getaccount [address]")
293 .description("get account by address")
294 .action(getAccount);
295
296 program
297 .command("getvoteddelegates [address]")
298 .description("get delegates voted by address")
299 .option("-o, --offset <n>", "")
300 .option("-l, --limit <n>", "")
301 .action(getVotedDelegates);
302
303 program
304 .command("getdelegatescount")
305 .description("get delegates count")
306 .action(getDelegatesCount);
307
308 program
309 .command("getdelegates")
310 .description("get delegates")
311 .option("-o, --offset <n>", "")
312 .option("-l, --limit <n>", "")
313 .option("-s, --sort <field:mode>", "rate:asc, vote:desc, ...")
314 .action(getDelegates);
315
316 program
317 .command("getvoters [publicKey]")
318 .description("get voters of a delegate by public key")
319 .action(getVoters);
320
321 program
322 .command("getdelegatebypublickey [publicKey]")
323 .description("get delegate by public key")
324 .action(getDelegateByPublicKey);
325
326 program
327 .command("getdelegatebyusername [username]")
328 .description("get delegate by username")
329 .action(getDelegateByUsername);
330
331 program
332 .command("getblocks")
333 .description("get blocks")
334 .option("-o, --offset <n>", "")
335 .option("-l, --limit <n>", "")
336 .option("-r, --reward <n>", "")
337 .option("-f, --totalFee <n>", "")
338 .option("-a, --totalAmount <n>", "")
339 .option("-g, --generatorPublicKey <publicKey>", "")
340 .option("-s, --sort <field:mode>", "height:asc, totalAmount:asc, totalFee:asc")
341 .action(getBlocks);
342
343 program
344 .command("getblockbyid [id]")
345 .description("get block by id")
346 .action(getBlockById);
347
348 program
349 .command("getblockbyheight [height]")
350 .description("get block by height")
351 .action(getBlockByHeight);
352
353 program
354 .command("getpeers")
355 .description("get peers")
356 .option("-o, --offset <n>", "")
357 .option("-l, --limit <n>", "")
358 .option("-t, --state <n>", " 0 ~ 3")
359 .option("-s, --sort <field:mode>", "")
360 .option("-v, --version <version>", "")
361 .option("-p, --port <n>", "")
362 .option("--os <os>", "")
363 .action(getPeers);
364
365 program
366 .command("getunconfirmedtransactions")
367 .description("get unconfirmed transactions")
368 .option("-k, --key <sender public key>", "")
369 .option("-a, --address <address>", "")
370 .action(getUnconfirmedTransactions);
371
372 program
373 .command("gettransactions")
374 .description("get transactions")
375 .option("-b, --blockId <id>", "")
376 .option("-o, --offset <n>", "")
377 .option("-l, --limit <n>", "")
378 .option("-t, --type <n>", "transaction type")
379 .option("-s, --sort <field:mode>", "")
380 .option("-a, --amount <n>", "")
381 .option("-f, --fee <n>", "")
382 .option("--senderPublicKey <key>", "")
383 .option("--senderId <id>", "")
384 .option("--recipientId <id>", "")
385 .action(getTransactions);
386
387 program
388 .command("gettransaction [id]")
389 .description("get transactions")
390 .action(getTransaction);
391
392 program
393 .command("sendmoney")
394 .description("send money to some address")
395 .option("-e, --secret <secret>", "")
396 .option("-s, --secondSecret <secret>", "")
397 .option("-a, --amount <n>", "")
398 .option("-t, --to <address>", "")
399 .action(sendMoney);
400
401 program
402 .command("registerdelegate")
403 .description("register delegate")
404 .option("-e, --secret <secret>", "")
405 .option("-s, --secondSecret <secret>", "")
406 .option("-u, --username <username>", "")
407 .action(registerDelegate);
408
409 program
410 .command("upvote")
411 .description("vote for delegates")
412 .option("-e, --secret <secret>", "")
413 .option("-s, --secondSecret <secret>", "")
414 .option("-p, --publicKeys <public key list>", "")
415 .action(upvote);
416
417 program
418 .command("downvote")
419 .description("cancel vote for delegates")
420 .option("-e, --secret <secret>", "")
421 .option("-s, --secondSecret <secret>", "")
422 .option("-p, --publicKeys <public key list>", "")
423 .action(downvote);
424
425 program
426 .command("setsecondsecret")
427 .description("set second secret")
428 .option("-e, --secret <secret>", "")
429 .option("-s, --secondSecret <secret>", "")
430 .action(setSecondSecret);
431
432 program
433 .command("registerdapp")
434 .description("register a dapp")
435 .option("-e, --secret <secret>", "")
436 .option("-s, --secondSecret <secret>", "")
437 .option("-f, --metafile <metafile>", "dapp meta file")
438 .action(registerDapp);
439}
\No newline at end of file