-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
248 lines (196 loc) · 7.98 KB
/
cli.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
const {Command} = require('commander');
const {fastForwardBlock, wallet, api, client, provider} = require("../src");
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let options, args;
let store, keyPair, address;
async function setupKeyPair() {
if (('passphrase' in options) && ('keyStore' in options)) {
store = await wallet.manager.read(options.passphrase, options.keyStore)
keyPair = store.getKeyPair()
address = keyPair.address
provider.setKeyPair(keyPair)
}
}
async function setupNode() {
provider.setClient(client.newClient(options.url))
}
const helpText = `Commands
plasma.get
plasma.list
plasma.fuse beneficiary amount
plasma.cancel id
listen.momentums
listen.allAccountBlocks
wallet.decrypt
wallet.new [name]
wallet.list`
async function main() {
const program = new Command();
program
.name('znn-cli.js')
.description('CLI for interacting with Zenon ecosystem')
.option(`-p, --passphrase <passphrase>`, 'use this passphrase for the keyStore')
.option(`-k, --keyStore <keyStore>`, 'select the local keyStore')
.option(`-u, --url <url>`, 'websocket/http znnd connection URL with a port', 'ws://139.177.178.226:35998')
.addHelpText('after', `\n${helpText}`);
program.parse();
options = program.opts()
args = program.args;
await setupNode()
await setupKeyPair()
let response, entry, id;
switch (args[0]) {
case 'accelerator.all':
if (args.length !== 1) {
throw "invalid usage; accelerator.all"
}
response = await api.embedded.accelerator.getAll(0, 1000)
for (let project of response.list) {
console.log(`id:${project.id} name:${project.name}`)
}
break;
case `accelerator.details`:
if (args.length !== 2) {
throw "invalid usage; accelerator.details ID"
}
id = args[1]
const project = await api.embedded.accelerator.getProjectById(id)
const projectStatusName = [ "voting", "active", "paid", "closed", "completed"]
console.log("Project Details")
console.log(`${project.name}\n${project.description}\nStatus: "${projectStatusName[project.status]}"`)
console.log(`Phases: ${project.phaseIds}`)
console.log("")
const allPillars = await api.embedded.pillar.getAll(0, 200)
const voteName = ['yes', 'no', 'abstain']
console.log("Pillar Votes")
for (let pillar of allPillars.list) {
response = await api.embedded.accelerator.getPillarVotes(pillar.name, [id])
if (response[0]) {
console.log(`${voteName[response[0].vote]} ${response[0].name}`)
}
}
break;
case `accelerator.phaseDetails`: {
if (args.length !== 2) {
throw "invalid usage; accelerator.phaseDetails ID"
}
id = args[1]
const breakdown = (await api.embedded.accelerator.getPhaseById(id))
const phase = breakdown.phase
const votes = breakdown.votes
const projectStatusName = [ "voting", "active", "paid", "closed", "completed"]
console.log("Phase Details")
console.log(phase.name)
console.log(phase.description)
console.log(`Status: ${projectStatusName[phase.status]}`)
console.log(`Yes: ${votes.yes} No: ${votes.no} Abstain: ${votes.total - votes.yes - votes.no}`)
const allPillars = await api.embedded.pillar.getAll(0, 200)
const voteName = ['yes', 'no', 'abstain']
console.log("Pillar Votes")
for (let pillar of allPillars.list) {
response = await api.embedded.accelerator.getPillarVotes(pillar.name, [id])
if (response[0]) {
console.log(`${voteName[response[0].vote]} ${response[0].name}`)
}
}
break;
}
case 'plasma.get':
if (args.length !== 1) {
throw "invalid usage; plasma.get"
}
response = await api.embedded.plasma.get(address)
console.log(`Plasma for account ${address.toString()}, ${response.currentPlasma}`);
break;
case 'plasma.list':
if (args.length !== 1) {
throw "invalid usage; plasma.list"
}
response = await api.embedded.plasma.getEntriesByAddress(address, 0, 10)
console.log(`Plasma entries for account ${address.toString()} - number ${JSON.stringify(response.count)}`);
for (entry of response.list) {
console.log(entry.beneficiary, entry.qsrAmount, entry.id)
}
break;
case 'plasma.fuse':
if (args.length !== 3) {
throw "invalid usage; plasma.fuse beneficiary amount"
}
let beneficiary = args[3];
let amount = args[4]
response = await fastForwardBlock(api.embedded.plasma.fuse({beneficiary, amount: parseInt(amount)}))
console.log(`Fused plasma for ${beneficiary}; ${JSON.stringify(response)}`);
break;
case 'plasma.cancel':
if (args.length !== 2) {
throw "invalid usage; plasma.fuse id"
}
id = args[3];
response = await fastForwardBlock(api.embedded.plasma.cancel({id}))
console.log(`Cancel fuse entry with ID ${id}; ${JSON.stringify(response)}`);
break;
case 'listen.momentums':
if (args.length !== 1) {
throw "invalid usage; listen.momentums"
}
response = await api.subscribe.toMomentums()
response.onNotification((data) => {
console.log(`Received new Momentum! Height:${data[0].height} Hash:${data[0].hash} CurrentTime:${new Date()}`)
})
// never return since the
for (; ;) {
await sleep(1000)
}
case 'listen.allAccountBlocks':
if (args.length !== 1) {
throw "invalid usage; listen.allAccountBlocks"
}
response = await api.subscribe.toAllAccountBlocks()
response.onNotification((data) => {
for (const block of data) {
console.log(`Received new Account Block! Address:${block.address} Height:${block.height} Hash:${block.hash} CurrentTime:${new Date()}`)
}
})
// never return since the
for (; ;) {
await sleep(1000)
}
case 'wallet.decrypt':
if (args.length !== 1) {
throw "invalid usage; decrypt"
}
console.log(`Decrypted key-file with address ${address.toString()}`);
break;
case 'wallet.new':
if (args.length !== 1 && args.length !== 2) {
throw "invalid usage; wallet.new [name]"
}
let password = options.passphrase
let name = args[1]
store = wallet.KeyStore.Random()
// set a name if none was provided
if (name === undefined) {
name = store.baseAddress.toString()
}
await wallet.manager.save(store, password, name)
console.log(`Created a new key-file with address ${store.baseAddress.toString()} and name ${name}`);
break;
case 'wallet.list':
if (args.length !== 1) {
throw "invalid usage; wallet.list"
}
console.log(wallet.manager.list())
break;
default:
console.log(`unknown command "${args[0]}"\n`);
console.log(helpText);
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});