|
1 | 1 | import { callback, loadXudClient } from '../command'; |
2 | 2 | import { Arguments } from 'yargs'; |
3 | | -import { GetInfoRequest } from '../../proto/xudrpc_pb'; |
| 3 | +import Table, { VerticalTable } from 'cli-table3'; |
| 4 | +import colors from 'colors/safe'; |
| 5 | +import { GetInfoRequest, GetInfoResponse, LndInfo, RaidenInfo } from '../../proto/xudrpc_pb'; |
| 6 | + |
| 7 | +type generalInfo = { |
| 8 | + version: string; |
| 9 | + numPeers: number; |
| 10 | + numPairs: number; |
| 11 | + nodePubKey: string; |
| 12 | + orders: {own: number, peer: number} | undefined |
| 13 | +}; |
| 14 | + |
| 15 | +const displayChannels = (channels: any, asset: string) => { |
| 16 | + const table = new Table() as VerticalTable; |
| 17 | + Object.keys(channels).forEach((key: any) => { |
| 18 | + table.push({ |
| 19 | + [colors.blue(key)] : channels[key], |
| 20 | + }); |
| 21 | + }); |
| 22 | + console.log(colors.underline(colors.bold(`\nLnd ${asset} channels:`))); |
| 23 | + console.log(table.toString(), '\n'); |
| 24 | +}; |
| 25 | + |
| 26 | +const displayChainsList = (list: any[], asset: string) => { |
| 27 | + const table = new Table() as VerticalTable; |
| 28 | + list.forEach((asset, i) => { |
| 29 | + if (asset) { |
| 30 | + table.push({ [colors.blue(`${i + 1}.`)]: `${asset.chain}-${asset.network}` }); |
| 31 | + } |
| 32 | + }); |
| 33 | + if (table.length !== 0) { |
| 34 | + console.log(colors.underline(colors.bold(`\nLnd ${asset} chains:`))); |
| 35 | + console.log(table.toString(), '\n'); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const displayUriList = (uris: string[], asset: string) => { |
| 40 | + const table = new Table() as VerticalTable; |
| 41 | + uris.forEach((uri, i) => table.push({ [`${i + 1}.`]: uri })); |
| 42 | + console.log(colors.underline(colors.bold(`\nLnd ${asset} uris:`))); |
| 43 | + console.log(table.toString(), '\n'); |
| 44 | +}; |
| 45 | + |
| 46 | +const displayLndInfo = (asset: string, info: LndInfo.AsObject) => { |
| 47 | + const basicInfotable = new Table() as VerticalTable; |
| 48 | + basicInfotable.push( |
| 49 | + { [colors.blue('Error')]: info.error }, |
| 50 | + ); |
| 51 | + if (info.blockheight) { |
| 52 | + basicInfotable.push({ [colors.blue('Block Height')]: info.blockheight }); |
| 53 | + } |
| 54 | + if (info.version) { |
| 55 | + basicInfotable.push({ [colors.blue('Version')]: info.version }); |
| 56 | + } |
| 57 | + if (info.alias) { |
| 58 | + basicInfotable.push({ [colors.blue('Alias')] : info.alias }); |
| 59 | + } |
| 60 | + |
| 61 | + console.log(colors.underline(colors.bold(`\nLnd ${asset} info:`))); |
| 62 | + console.log(basicInfotable.toString(), '\n'); |
| 63 | + |
| 64 | + if (info.channels) { |
| 65 | + displayChannels(info.channels, asset); |
| 66 | + } |
| 67 | + |
| 68 | + if (!info.error) { |
| 69 | + displayChainsList(info.chainsList, asset); |
| 70 | + } |
| 71 | + |
| 72 | + if (info.urisList.length > 0) { |
| 73 | + displayUriList(info.urisList, asset); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +const displayGeneral = (info: generalInfo) => { |
| 78 | + const table = new Table() as VerticalTable; |
| 79 | + table.push( |
| 80 | + { [colors.blue('Version')]: info.version }, |
| 81 | + { [colors.blue('Pairs')]: info.numPairs }, |
| 82 | + { [colors.blue('Peers')]: info.numPeers }, |
| 83 | + { [colors.blue('Node key')]: info.nodePubKey }, |
| 84 | + ); |
| 85 | + if (info.orders) { |
| 86 | + table.push( |
| 87 | + { [colors.blue('Own orders')]: info.orders.own }, |
| 88 | + { [colors.blue('Peer orders')]: info.orders.peer }, |
| 89 | + ); |
| 90 | + } |
| 91 | + console.log(colors.underline(colors.bold('\nGeneral XUD Info'))); |
| 92 | + console.log(table.toString(), '\n'); |
| 93 | +}; |
| 94 | + |
| 95 | +const displayRaiden = (info: RaidenInfo.AsObject) => { |
| 96 | + const table = new Table() as VerticalTable; |
| 97 | + table.push( |
| 98 | + { [colors.blue('Version')]: info.version }, |
| 99 | + { [colors.blue('Address')]: info.address }, |
| 100 | + { [colors.blue('Channels')]: info.channels }, |
| 101 | + { [colors.blue('Error')]: info.error }, |
| 102 | + ); |
| 103 | + console.log(colors.underline(colors.bold('\nRaiden info:'))); |
| 104 | + console.log(table.toString(), '\n'); |
| 105 | +}; |
| 106 | + |
| 107 | +const displayGetInfo = (response: GetInfoResponse.AsObject) => { |
| 108 | + displayGeneral({ |
| 109 | + nodePubKey: response.nodePubKey, |
| 110 | + numPairs: response.numPairs, |
| 111 | + numPeers: response.numPeers, |
| 112 | + version: response.version, |
| 113 | + orders: response.orders, |
| 114 | + }); |
| 115 | + if (response.raiden) { |
| 116 | + displayRaiden(response.raiden); |
| 117 | + } |
| 118 | + |
| 119 | + response.lndMap.forEach(asset => displayLndInfo(asset[0], asset[1])); |
| 120 | +}; |
4 | 121 |
|
5 | 122 | export const command = 'getinfo'; |
6 | 123 |
|
7 | 124 | export const describe = 'get general info from the xud node'; |
8 | 125 |
|
9 | 126 | export const handler = (argv: Arguments) => { |
10 | | - loadXudClient(argv).getInfo(new GetInfoRequest(), callback(argv)); |
| 127 | + loadXudClient(argv).getInfo(new GetInfoRequest(), callback(argv, displayGetInfo)); |
11 | 128 | }; |
0 commit comments