|
| 1 | +import { Arguments, Argv } from 'yargs'; |
| 2 | +import { XudClient } from '../../proto/xudrpc_grpc_pb'; |
| 3 | +import * as xudrpc from '../../proto/xudrpc_pb'; |
| 4 | +import { loadXudClient } from '../command'; |
| 5 | +import { AlertType, ChannelSide } from '../../constants/enums'; |
| 6 | +import { onStreamError, waitForClient } from '../utils'; |
| 7 | +import moment from 'moment'; |
| 8 | + |
| 9 | +export const command = 'streamalerts'; |
| 10 | + |
| 11 | +export const describe = 'stream alert notifications from xud'; |
| 12 | + |
| 13 | +export const builder = (argv: Argv) => argv |
| 14 | + .option('pretty', { |
| 15 | + type: 'boolean', |
| 16 | + }) |
| 17 | + .example('$0 streamalerts -j', 'prints alert payload in a JSON structure') |
| 18 | + .example('$0 streamalerts', 'prints alert message only'); |
| 19 | + |
| 20 | +export const handler = async (argv: Arguments) => { |
| 21 | + await ensureConnection(argv, true); |
| 22 | +}; |
| 23 | + |
| 24 | +let client: XudClient; |
| 25 | + |
| 26 | +const ensureConnection = async (argv: Arguments, printError?: boolean) => { |
| 27 | + if (!client) { |
| 28 | + client = await loadXudClient(argv); |
| 29 | + } |
| 30 | + |
| 31 | + waitForClient(client, argv, ensureConnection, streamalerts, printError); |
| 32 | +}; |
| 33 | + |
| 34 | +const structAlertJson = (alertObject: xudrpc.Alert.AsObject) => { |
| 35 | + const result: {type: string, payload: { |
| 36 | + totalBalance?: number, |
| 37 | + side?: string, |
| 38 | + bound?: number, |
| 39 | + sideBalance?: number, |
| 40 | + channelPoint?: string, |
| 41 | + currency?: string, |
| 42 | + } | undefined } = { |
| 43 | + type: AlertType[alertObject.type], |
| 44 | + payload: undefined, |
| 45 | + }; |
| 46 | + |
| 47 | + if (alertObject.type === xudrpc.Alert.AlertType.LOW_TRADING_BALANCE) { |
| 48 | + result.payload = { |
| 49 | + totalBalance: alertObject.balanceAlert?.totalBalance, |
| 50 | + side: ChannelSide[alertObject.balanceAlert?.side || 0], |
| 51 | + sideBalance: alertObject.balanceAlert?.sideBalance, |
| 52 | + bound: alertObject.balanceAlert?.bound, |
| 53 | + currency: alertObject.balanceAlert?.currency, |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + return result; |
| 58 | +}; |
| 59 | + |
| 60 | +const streamalerts = (argv: Arguments<any>) => { |
| 61 | + const request = new xudrpc.SubscribeAlertsRequest(); |
| 62 | + const alertsSubscription = client.subscribeAlerts(request); |
| 63 | + |
| 64 | + alertsSubscription.on('data', (alert: xudrpc.Alert) => { |
| 65 | + if (argv.json) { |
| 66 | + console.log(JSON.stringify(structAlertJson(alert.toObject()), undefined, 2)); |
| 67 | + } else { |
| 68 | + console.log(`(${moment()}) ${AlertType[alert.getType()]}: ${alert.getMessage()}`); |
| 69 | + } |
| 70 | + }); |
| 71 | + alertsSubscription.on('end', reconnect.bind(undefined, argv)); |
| 72 | + alertsSubscription.on('error', onStreamError.bind(undefined, ensureConnection.bind(undefined, argv))); |
| 73 | +}; |
| 74 | + |
| 75 | +const reconnect = async (argv: Arguments) => { |
| 76 | + console.log('Stream has closed, trying to reconnect'); |
| 77 | + await ensureConnection(argv, false); |
| 78 | +}; |
0 commit comments