1
1
import { loadXudClient } from '../command' ;
2
2
import { Arguments } from 'yargs' ;
3
3
import * as xudrpc from '../../proto/xudrpc_pb' ;
4
+ import { XudClient } from '../../proto/xudrpc_grpc_pb' ;
4
5
5
6
export const command = 'streamorders [existing]' ;
6
7
@@ -15,20 +16,58 @@ export const builder = {
15
16
} ;
16
17
17
18
export const handler = ( argv : Arguments ) => {
19
+ ensureConnection ( argv , true ) ;
20
+ } ;
21
+
22
+ let xud : XudClient ;
23
+
24
+ const ensureConnection = ( argv : Arguments , printError ?: boolean ) => {
25
+ if ( ! xud ) xud = loadXudClient ( argv ) ;
26
+ xud . waitForReady ( Number . POSITIVE_INFINITY , ( error : Error | null ) => {
27
+ if ( error ) {
28
+ if ( printError ) console . error ( `${ error . name } : ${ error . message } ` ) ;
29
+ setTimeout ( ensureConnection . bind ( undefined , argv ) , 3000 ) ;
30
+ } else {
31
+ console . log ( 'Successfully connected, subscribing for orders' ) ;
32
+ subscribeOrders ( argv ) ;
33
+ }
34
+ } ) ;
35
+ } ;
36
+
37
+ const subscribeOrders = ( argv : Arguments ) => {
18
38
const addedOrdersRequest = new xudrpc . SubscribeAddedOrdersRequest ( ) ;
19
39
addedOrdersRequest . setExisting ( argv . existing ) ;
20
40
const addedOrdersSubscription = loadXudClient ( argv ) . subscribeAddedOrders ( addedOrdersRequest ) ;
21
41
addedOrdersSubscription . on ( 'data' , ( order : xudrpc . Order ) => {
22
42
console . log ( `Order added: ${ JSON . stringify ( order . toObject ( ) ) } ` ) ;
23
43
} ) ;
24
44
45
+ // adding end, close, error events only once,
46
+ // since they'll be thrown for three of subscriptions in the corresponding cases, catching once is enough.
47
+ addedOrdersSubscription . on ( 'end' , reconnect . bind ( undefined , argv ) ) ;
48
+ addedOrdersSubscription . on ( 'error' , ( err : Error ) => {
49
+ console . log ( `Unexpected error occured: ${ JSON . stringify ( err ) } , trying to reconnect` ) ;
50
+ ensureConnection ( argv ) ;
51
+ } ) ;
52
+
25
53
const removedOrdersSubscription = loadXudClient ( argv ) . subscribeRemovedOrders ( new xudrpc . SubscribeRemovedOrdersRequest ( ) ) ;
26
54
removedOrdersSubscription . on ( 'data' , ( orderRemoval : xudrpc . OrderRemoval ) => {
27
55
console . log ( `Order removed: ${ JSON . stringify ( orderRemoval . toObject ( ) ) } ` ) ;
28
56
} ) ;
29
57
58
+ // prevent exiting and do nothing, it's already caught above.
59
+ removedOrdersSubscription . on ( 'error' , ( ) => { } ) ;
60
+
30
61
const swapsSubscription = loadXudClient ( argv ) . subscribeSwaps ( new xudrpc . SubscribeSwapsRequest ( ) ) ;
31
62
swapsSubscription . on ( 'data' , ( swapResult : xudrpc . SwapResult ) => {
32
63
console . log ( `Order swapped: ${ JSON . stringify ( swapResult . toObject ( ) ) } ` ) ;
33
64
} ) ;
65
+
66
+ // prevent exiting and do nothing, it's already caught above.
67
+ swapsSubscription . on ( 'error' , ( ) => { } ) ;
68
+ } ;
69
+
70
+ const reconnect = ( argv : Arguments ) => {
71
+ console . log ( 'Stream has closed, trying to reconnect' ) ;
72
+ ensureConnection ( argv , false ) ;
34
73
} ;
0 commit comments