-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new grpc call for subscribring alerts such as low balance (#864)
- Loading branch information
Showing
18 changed files
with
1,871 additions
and
351 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ServiceError, status } from 'grpc'; | ||
import { Arguments, Argv } from 'yargs'; | ||
import { XudClient } from '../../proto/xudrpc_grpc_pb'; | ||
import * as xudrpc from '../../proto/xudrpc_pb'; | ||
import { setTimeoutPromise } from '../../utils/utils'; | ||
import { loadXudClient } from '../command'; | ||
import { AlertType } from '../../constants/enums'; | ||
|
||
export const command = 'subscribealerts [--pretty]'; | ||
|
||
export const describe = 'subscribe alerts such as low balance'; | ||
|
||
export const builder = (argv: Argv) => argv | ||
.option('pretty', { | ||
type: 'boolean', | ||
}) | ||
.example('$0 subscribealerts', 'prints alert payload in a JSON structure') | ||
.example('$0 subscribealerts --pretty', 'prints alert message only'); | ||
|
||
export const handler = async (argv: Arguments) => { | ||
await ensureConnection(argv, true); | ||
}; | ||
|
||
let client: XudClient; | ||
|
||
const ensureConnection = async (argv: Arguments, printError?: boolean) => { | ||
if (!client) { | ||
client = await loadXudClient(argv); | ||
} | ||
client.waitForReady(Date.now() + 3000, (error: Error | null) => { | ||
if (error) { | ||
if (error.message === 'Failed to connect before the deadline') { | ||
console.error(`could not connect to xud at ${argv.rpchost}:${argv.rpcport}, is xud running?`); | ||
process.exit(1); | ||
} | ||
|
||
if (printError) console.error(`${error.name}: ${error.message}`); | ||
setTimeout(ensureConnection.bind(undefined, argv, printError), 3000); | ||
} else { | ||
console.log('Successfully connected, subscribing for alerts'); | ||
subscribeAlerts(argv); | ||
} | ||
}); | ||
}; | ||
|
||
const subscribeAlerts = (argv: Arguments<any>) => { | ||
const request = new xudrpc.SubscribeAlertsRequest(); | ||
const alertsSubscription = client.subscribeAlerts(request); | ||
|
||
alertsSubscription.on('data', (alert: xudrpc.Alert) => { | ||
if (argv.pretty) { | ||
console.log(`${AlertType[alert.getType()]}: ${alert.getMessage()}`); | ||
} else { | ||
console.log(JSON.stringify(alert, undefined, 2)); | ||
} | ||
}); | ||
alertsSubscription.on('end', reconnect.bind(undefined, argv)); | ||
alertsSubscription.on('error', async (err: ServiceError) => { | ||
if (err.code === status.UNIMPLEMENTED) { | ||
console.error("xud is locked, run 'xucli unlock', 'xucli create', or 'xucli restore' then try again"); | ||
process.exit(1); | ||
} | ||
console.warn(`Unexpected error occured: ${err.message}, reconnecting in 1 second`); | ||
await setTimeoutPromise(1000); | ||
await ensureConnection(argv); | ||
}); | ||
}; | ||
|
||
const reconnect = async (argv: Arguments) => { | ||
console.log('Stream has closed, trying to reconnect'); | ||
await ensureConnection(argv, false); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.