-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.ts
More file actions
518 lines (463 loc) · 16.3 KB
/
cli.ts
File metadata and controls
518 lines (463 loc) · 16.3 KB
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
// Copyright (C) 2021 Edge Network Technologies Limited
// Use of this source code is governed by a GNU GPL-style license
// that can be found in the LICENSE.md file. All rights reserved.
import * as index from '@edge/index-utils'
import * as walletCLI from '../wallet/cli'
import * as xe from '@edge/xe-utils'
import { Command } from 'commander'
import { Network } from '../main'
import { ask } from '../input'
import { checkVersionHandler } from '../update/cli'
import { withFile } from '../wallet/storage'
import { askToSignTx, handleCreateTxResult } from '../transaction'
import { errorHandler, getVerboseOption } from '../edge/cli'
import { findOne, types } from '.'
import { formatXE, withNetwork as xeWithNetwork } from '../transaction/xe'
import { printData, printTrunc, toDays, toUpperCaseFirst } from '../helpers'
const formatTime = (t: number): string => {
const d = new Date(t)
const [yyyy, mm, dd, h, m, s] = [
d.getUTCFullYear(),
(1 + d.getUTCMonth()).toString().padStart(2, '0'),
(1 + d.getUTCDate()).toString().padStart(2, '0'),
d.getUTCHours().toString().padStart(2, '0'),
d.getUTCMinutes().toString().padStart(2, '0'),
d.getUTCSeconds().toString().padStart(2, '0')
]
return `${yyyy}-${mm}-${dd} ${h}:${m}:${s}`
}
// wrapper for xe.vars; returns the original error in --verbose CLI, otherwise generic error message
const onChainVars = async (verbose: boolean, host: string) => {
try {
return await xe.vars(host)
}
catch (err) {
if (verbose) throw err
throw new Error('staking is currently unavailable. Please try again later.')
}
}
const createAction = (parent: Command, createCmd: Command, network: Network) => async (nodeType: string) => {
if (!types.includes(nodeType)) throw new Error(`invalid node type "${nodeType}"`)
const opts = {
...getVerboseOption(parent),
...walletCLI.getWalletOption(parent, network),
...walletCLI.getPassphraseOption(createCmd),
...(() => {
const { yes } = createCmd.opts<{ yes: boolean }>()
return { yes }
})()
}
const storage = withFile(opts.wallet)
const api = xeWithNetwork(network)
const onChainWallet = await api.walletWithNextNonce(await storage.address())
const vars = await onChainVars(opts.verbose, network.blockchain.baseURL)
// fallback 0 is just for typing - nodeType is checked at top of func, so it should never be used
const amount =
nodeType === 'host' ? vars.host_stake_amount :
nodeType === 'gateway' ? vars.gateway_stake_amount :
nodeType === 'stargate' ? vars.stargate_stake_amount : 0
const resultBalance = onChainWallet.balance - amount
// eslint-disable-next-line max-len
if (resultBalance < 0) throw new Error(`insufficient balance to stake ${nodeType}: your wallet only contains ${formatXE(onChainWallet.balance)} (${formatXE(amount)} required)`)
if (!opts.yes) {
// eslint-disable-next-line max-len
console.log(`You are staking ${formatXE(amount)} to run a ${toUpperCaseFirst(nodeType)}.`)
console.log(
`${formatXE(amount)} will be deducted from your available balance.`,
`You will have ${formatXE(resultBalance)} remaining.`
)
console.log()
let confirm = ''
const ynRegexp = /^[yn]$/
while (confirm.length === 0) {
const input = await ask('Proceed with staking? [yn] ')
if (ynRegexp.test(input)) confirm = input
else console.log('Please enter y or n.')
}
if (confirm === 'n') return
console.log()
}
await askToSignTx(opts)
const wallet = await storage.read(opts.passphrase as string)
const tx = xe.tx.sign({
timestamp: Date.now(),
sender: wallet.address,
recipient: wallet.address,
amount,
data: {
action: 'create_stake',
memo: `Create ${toUpperCaseFirst(nodeType)} Stake`
},
nonce: onChainWallet.nonce
}, wallet.privateKey)
const result = await api.createTransaction(tx)
if (!handleCreateTxResult(network, result)) process.exitCode = 1
}
const createHelp = (network: Network) => [
'\n',
'This command will create a stake on the blockchain.\n\n',
'A stake enables your device to participate as a node in the network, providing capacity in exchange for XE.\n\n',
`Run '${network.appName} device add --help' for more information.`
].join('')
const historyAction = (parent: Command, historyCmd: Command, network: Network) => async (id: string) => {
const opts = {
...walletCLI.getWalletOption(parent, network),
...getJsonOption(historyCmd)
}
const storage = withFile(opts.wallet)
const { results: stakes } = await index.stake.stakes(network.index.baseURL, await storage.address(), { limit: 999 })
const stake = findOne(stakes, id)
const data = await index.stake.history(network.index.baseURL, stake.id)
if (opts.json) {
console.log(JSON.stringify(data, undefined, 2))
return
}
console.log('WIP')
console.log(JSON.stringify(data, undefined, 2))
// data.results.forEach()
}
const historyHelp = [
'\n',
'Display a stake\'s history of actions.'
].join('')
const infoAction = (parent: Command, infoCmd: Command, network: Network) => async () => {
const opts = {
...getVerboseOption(parent),
...getJsonOption(infoCmd)
}
const vars = await onChainVars(opts.verbose, network.blockchain.baseURL)
if (opts.json) {
const someVars = {
host_stake_amount: vars.host_stake_amount,
gateway_stake_amount: vars.gateway_stake_amount,
stargate_stake_amount: vars.stargate_stake_amount
}
console.log(JSON.stringify(someVars, undefined, 2))
return
}
const amounts = [
vars.host_stake_amount,
vars.gateway_stake_amount,
vars.stargate_stake_amount
].map(mxe => formatXE(mxe))
const longest = amounts.reduce((l, s) => Math.max(l, s.length), 0)
const [hostAmt, gatewayAmt, stargateAmt] = amounts.map(a => a.padStart(longest, ' '))
console.log('Current staking amounts:')
console.log(` Stargate: ${stargateAmt}`)
console.log(` Gateway: ${gatewayAmt}`)
console.log(` Host: ${hostAmt}`)
}
const infoHelp = '\nDisplays current staking amounts.'
const listAction = (parent: Command, listCmd: Command, network: Network) => async () => {
const opts = {
...walletCLI.getWalletOption(parent, network),
...getJsonOption(listCmd),
...(() => {
const { fullIds } = listCmd.opts<{ fullIds: boolean }>()
return { fullIds }
})()
}
const printID = printTrunc(!opts.fullIds, 8)
const storage = withFile(opts.wallet)
const { results: stakes } = await index.stake.stakes(network.index.baseURL, await storage.address())
if (opts.json) {
console.log(JSON.stringify(stakes, undefined, 2))
return
}
Object.values(stakes).forEach(stake => {
const data: Record<string, string> = {
ID: printID(stake.id),
Hash: printID(stake.hash),
Tx: stake.transaction,
Created: formatTime(stake.created),
...(() => {
if (stake.device !== undefined) {
return {
Device: stake.device,
Assigned: formatTime(stake.deviceAssigned as number)
}
}
})(),
Amount: formatXE(stake.amount),
Type: toUpperCaseFirst(stake.type)
}
if (stake.released !== undefined) data.Status = 'Released'
else if (stake.unlockRequested !== undefined) {
const unlockAt = stake.unlockRequested + stake.unlockPeriod
if (unlockAt > Date.now()) data.Status = `Unlocking (unlocks at ${formatTime(unlockAt)})`
else data.Status = 'Unlocked'
}
else data.Status = 'Active'
console.log(printData(data))
console.log()
})
}
const listHelp = [
'\n',
'Displays all stakes associated with your wallet.\n\n',
'The default output is simplified for legibility. Provide the --json option to display full detail.'
].join('')
const releaseAction = (parent: Command, releaseCmd: Command, network: Network) => async (id: string) => {
const opts = {
...getVerboseOption(parent),
...walletCLI.getWalletOption(parent, network),
...walletCLI.getPassphraseOption(releaseCmd),
...(() => {
const { express, yes } = releaseCmd.opts<{ express: boolean, yes: boolean }>()
return { express, yes }
})()
}
const storage = withFile(opts.wallet)
const { results: stakes } = await index.stake.stakes(network.index.baseURL, await storage.address())
const stake = findOne(stakes, id)
if (stake.released !== undefined) {
console.log('This stake has already been released.')
return
}
if (stake.unlockRequested === undefined) {
console.log('This stake must be unlocked before it can be released.')
return
}
const unlockAt = stake.unlockRequested + stake.unlockPeriod
const needUnlock = unlockAt > Date.now()
if (needUnlock && !opts.express) {
const { stake_express_release_fee } = await onChainVars(opts.verbose, network.blockchain.baseURL)
const releaseFee = stake_express_release_fee * stake.amount
const releasePc = stake_express_release_fee * 100
console.log(`This stake has not unlocked yet. It unlocks at ${formatTime(unlockAt)}.`)
console.log(`You can release it instantly for a ${releasePc}% express release fee (${formatXE(releaseFee)}).`)
console.log()
console.log('To do so, execute this command again with the --express flag.')
return
}
if (!opts.yes) {
// eslint-disable-next-line max-len
console.log(`You are releasing a ${toUpperCaseFirst(stake.type)} stake.`)
if (needUnlock) {
const { stake_express_release_fee } = await onChainVars(opts.verbose, network.blockchain.baseURL)
const releaseFee = stake_express_release_fee * stake.amount
const releasePc = stake_express_release_fee * 100
console.log([
`${formatXE(stake.amount - releaseFee)} will be returned to your available balance after paying `,
`a ${releasePc}% express release fee (${formatXE(releaseFee)}).`
].join(''))
}
else console.log(`${formatXE(stake.amount)} will be returned to your available balance.`)
console.log()
let confirm = ''
const ynRegexp = /^[yn]$/
while (confirm.length === 0) {
const input = await ask('Proceed with release? [yn] ')
if (ynRegexp.test(input)) confirm = input
else console.log('Please enter y or n.')
}
if (confirm === 'n') return
console.log()
}
await askToSignTx(opts)
const wallet = await storage.read(opts.passphrase as string)
const api = xeWithNetwork(network)
const onChainWallet = await api.walletWithNextNonce(wallet.address)
const data: xe.tx.TxData = {
action: 'release_stake',
memo: 'Release Stake',
stake: stake.hash
}
if (needUnlock) data.express = true
const tx = xe.tx.sign({
timestamp: Date.now(),
sender: wallet.address,
recipient: wallet.address,
amount: 0,
data,
nonce: onChainWallet.nonce
}, wallet.privateKey)
const result = await api.createTransaction(tx)
if (!handleCreateTxResult(network, result)) process.exitCode = 1
}
const releaseHelp = [
'\n',
'Release a stake.\n\n',
'The --express option instructs the blockchain to take a portion of your stake in return for an immediate ',
'release of funds, rather than waiting for the unlock period to conclude.'
].join('')
const unlockAction = (parent: Command, unlockCmd: Command, network: Network) => async (id: string) => {
const opts = {
...walletCLI.getWalletOption(parent, network),
...walletCLI.getPassphraseOption(unlockCmd),
...(() => {
const { yes } = unlockCmd.opts<{ yes: boolean }>()
return { yes }
})()
}
const storage = withFile(opts.wallet)
const { results: stakes } = await index.stake.stakes(network.index.baseURL, await storage.address())
const stake = findOne(stakes, id)
if (stake.unlockRequested !== undefined) {
if (stake.unlockRequested + stake.unlockPeriod > Date.now()) {
console.log('Unlock has already been requested.')
console.log(`This stake will unlock at ${formatTime(stake.unlockRequested)}`)
}
else console.log('This stake is already unlocked.')
return
}
if (!opts.yes) {
// eslint-disable-next-line max-len
console.log(`You are requesting to unlock a ${toUpperCaseFirst(stake.type)} stake.`)
console.log([
`After the unlock wait period of ${toDays(stake.unlockPeriod)} days, `,
`you will be able to release the stake and return ${formatXE(stake.amount)} to your available balance.`
].join(''))
console.log()
let confirm = ''
const ynRegexp = /^[yn]$/
while (confirm.length === 0) {
const input = await ask('Proceed with unlock? [yn] ')
if (ynRegexp.test(input)) confirm = input
else console.log('Please enter y or n.')
}
if (confirm === 'n') return
console.log()
}
await askToSignTx(opts)
const wallet = await storage.read(opts.passphrase as string)
const api = xeWithNetwork(network)
const onChainWallet = await api.walletWithNextNonce(wallet.address)
const tx = xe.tx.sign({
timestamp: Date.now(),
sender: wallet.address,
recipient: wallet.address,
amount: 0,
data: {
action: 'unlock_stake',
memo: 'Unlock Stake',
stake: stake.hash
},
nonce: onChainWallet.nonce
}, wallet.privateKey)
const result = await api.createTransaction(tx)
if (!handleCreateTxResult(network, result)) process.exitCode = 1
}
const unlockHelp = [
'\n',
'Unlock a stake.'
].join('')
const getJsonOption = (cmd: Command) => {
type JsonOption = { json: boolean }
const { json } = cmd.opts<JsonOption>()
return <JsonOption>{ json }
}
export const withProgram = (parent: Command, network: Network): void => {
const stakeCLI = new Command('stake')
.description('manage stakes')
// edge stake create
const create = new Command('create')
.argument('<type>', `node type (${types.join('|')})`)
.description('create a new stake')
.addHelpText('after', createHelp(network))
.addOption(walletCLI.passphraseOption())
.addOption(walletCLI.passphraseFileOption())
.option('-y, --yes', 'do not ask for confirmation')
create.action(
errorHandler(
parent,
checkVersionHandler(
parent,
network,
createAction(parent, create, network)
)
)
)
const history = new Command('history')
.argument('<id>', 'stake ID')
.description('display stake history')
.addHelpText('after', historyHelp)
.option('--json', 'display history as json')
history.action(
errorHandler(
parent,
checkVersionHandler(
parent,
network,
historyAction(parent, history, network)
)
)
)
// edge stake info
const info = new Command('info')
.description('get on-chain staking information')
.addHelpText('after', infoHelp)
.option('--json', 'display info as json')
info.action(
errorHandler(
parent,
checkVersionHandler(
parent,
network,
infoAction(parent, info, network)
)
)
)
// edge stake ls
const list = new Command('list')
.alias('ls')
.description('list all stakes')
.addHelpText('after', listHelp)
.option('-D, --full-ids', 'display full-length IDs (ignored if --json)')
.option('--json', 'display stakes as json')
list.action(
errorHandler(
parent,
checkVersionHandler(
parent,
network,
listAction(parent, list, network)
)
)
)
// edge stake release
const release = new Command('release')
.argument('<id>', 'stake ID')
.description('release a stake')
.addHelpText('after', releaseHelp)
.option('-e, --express', 'express release')
.addOption(walletCLI.passphraseOption())
.addOption(walletCLI.passphraseFileOption())
.option('-y, --yes', 'do not ask for confirmation')
release.action(
errorHandler(
parent,
checkVersionHandler(
parent,
network,
releaseAction(parent, release, network)
)
)
)
// edge stake unlock
const unlock = new Command('unlock')
.argument('<id>', 'stake ID')
.description('unlock a stake')
.addHelpText('after', unlockHelp)
.addOption(walletCLI.passphraseOption())
.addOption(walletCLI.passphraseFileOption())
.option('-y, --yes', 'do not ask for confirmation')
unlock.action(
errorHandler(
parent,
checkVersionHandler(
parent,
network,
unlockAction(parent, unlock, network)
)
)
)
stakeCLI
.addCommand(create)
.addCommand(history)
.addCommand(info)
.addCommand(list)
.addCommand(release)
.addCommand(unlock)
parent.addCommand(stakeCLI)
}