@@ -12,12 +12,14 @@ const ClientMessageSchema: Joi.Schema = Joi.object({
12
12
. items ( Joi . string ( ) . regex ( / ^ ( 0 x ) ? [ a - f 0 - 9 ] { 64 } $ / ) )
13
13
. required ( ) ,
14
14
verbose : Joi . boolean ( ) ,
15
+ binary : Joi . boolean ( ) ,
15
16
} ) . required ( ) ;
16
17
17
18
export type ClientMessage = {
18
19
type : "subscribe" | "unsubscribe" ;
19
20
ids : HexString [ ] ;
20
21
verbose ?: boolean ;
22
+ binary ?: boolean ;
21
23
} ;
22
24
23
25
export type ServerResponse = {
@@ -31,12 +33,20 @@ export type ServerPriceUpdate = {
31
33
price_feed : any ;
32
34
} ;
33
35
36
+ export type PriceFeedConfig = {
37
+ verbose : boolean ;
38
+ binary : boolean ;
39
+ } ;
40
+
34
41
export type ServerMessage = ServerResponse | ServerPriceUpdate ;
35
42
36
43
export class WebSocketAPI {
37
44
private wsCounter : number ;
38
45
private priceFeedClients : Map < HexString , Set < WebSocket > > ;
39
- private priceFeedClientsVerbosity : Map < HexString , Map < WebSocket , boolean > > ;
46
+ private priceFeedClientsConfig : Map <
47
+ HexString ,
48
+ Map < WebSocket , PriceFeedConfig >
49
+ > ;
40
50
private aliveClients : Set < WebSocket > ;
41
51
private wsId : Map < WebSocket , number > ;
42
52
private priceFeedVaaInfo : PriceStore ;
@@ -45,7 +55,7 @@ export class WebSocketAPI {
45
55
constructor ( priceFeedVaaInfo : PriceStore , promClient ?: PromClient ) {
46
56
this . priceFeedVaaInfo = priceFeedVaaInfo ;
47
57
this . priceFeedClients = new Map ( ) ;
48
- this . priceFeedClientsVerbosity = new Map ( ) ;
58
+ this . priceFeedClientsConfig = new Map ( ) ;
49
59
this . aliveClients = new Set ( ) ;
50
60
this . wsCounter = 0 ;
51
61
this . wsId = new Map ( ) ;
@@ -55,13 +65,14 @@ export class WebSocketAPI {
55
65
private addPriceFeedClient (
56
66
ws : WebSocket ,
57
67
id : HexString ,
58
- verbose : boolean = false
68
+ verbose : boolean = false ,
69
+ binary : boolean = false
59
70
) {
60
71
if ( ! this . priceFeedClients . has ( id ) ) {
61
72
this . priceFeedClients . set ( id , new Set ( ) ) ;
62
- this . priceFeedClientsVerbosity . set ( id , new Map ( [ [ ws , verbose ] ] ) ) ;
73
+ this . priceFeedClientsConfig . set ( id , new Map ( [ [ ws , { verbose, binary } ] ] ) ) ;
63
74
} else {
64
- this . priceFeedClientsVerbosity . get ( id ) ! . set ( ws , verbose ) ;
75
+ this . priceFeedClientsConfig . get ( id ) ! . set ( ws , { verbose, binary } ) ;
65
76
}
66
77
this . priceFeedClients . get ( id ) ! . add ( ws ) ;
67
78
}
@@ -71,7 +82,7 @@ export class WebSocketAPI {
71
82
return ;
72
83
}
73
84
this . priceFeedClients . get ( id ) ! . delete ( ws ) ;
74
- this . priceFeedClientsVerbosity . get ( id ) ! . delete ( ws ) ;
85
+ this . priceFeedClientsConfig . get ( id ) ! . delete ( ws ) ;
75
86
}
76
87
77
88
dispatchPriceFeedUpdate ( priceInfo : PriceInfo ) {
@@ -96,27 +107,30 @@ export class WebSocketAPI {
96
107
for ( const client of clients . values ( ) ) {
97
108
this . promClient ?. addWebSocketInteraction ( "server_update" , "ok" ) ;
98
109
99
- const verbose = this . priceFeedClientsVerbosity
110
+ const config = this . priceFeedClientsConfig
100
111
. get ( priceInfo . priceFeed . id ) !
101
112
. get ( client ) ;
102
113
103
- const priceUpdate : ServerPriceUpdate = verbose
104
- ? {
105
- type : "price_update" ,
106
- price_feed : {
107
- ...priceInfo . priceFeed . toJson ( ) ,
108
- metadata : {
109
- emitter_chain : priceInfo . emitterChainId ,
110
- attestation_time : priceInfo . attestationTime ,
111
- sequence_number : priceInfo . seqNum ,
112
- price_service_receive_time : priceInfo . priceServiceReceiveTime ,
113
- } ,
114
+ const verbose = config ?. verbose ;
115
+ const binary = config ?. binary ;
116
+
117
+ const priceUpdate : ServerPriceUpdate = {
118
+ type : "price_update" ,
119
+ price_feed : {
120
+ ...priceInfo . priceFeed . toJson ( ) ,
121
+ ...( verbose && {
122
+ metadata : {
123
+ emitter_chain : priceInfo . emitterChainId ,
124
+ attestation_time : priceInfo . attestationTime ,
125
+ sequence_number : priceInfo . seqNum ,
126
+ price_service_receive_time : priceInfo . priceServiceReceiveTime ,
114
127
} ,
115
- }
116
- : {
117
- type : "price_update" ,
118
- price_feed : priceInfo . priceFeed . toJson ( ) ,
119
- } ;
128
+ } ) ,
129
+ ...( binary && {
130
+ vaa : priceInfo . vaa . toString ( "base64" ) ,
131
+ } ) ,
132
+ } ,
133
+ } ;
120
134
121
135
client . send ( JSON . stringify ( priceUpdate ) ) ;
122
136
}
@@ -161,7 +175,12 @@ export class WebSocketAPI {
161
175
162
176
if ( message . type === "subscribe" ) {
163
177
message . ids . forEach ( ( id ) =>
164
- this . addPriceFeedClient ( ws , id , message . verbose === true )
178
+ this . addPriceFeedClient (
179
+ ws ,
180
+ id ,
181
+ message . verbose === true ,
182
+ message . binary === true
183
+ )
165
184
) ;
166
185
} else {
167
186
message . ids . forEach ( ( id ) => this . delPriceFeedClient ( ws , id ) ) ;
0 commit comments