@@ -9,7 +9,7 @@ pub struct PythLazerAgentJrpcV1 {
9
9
pub jsonrpc : JsonRpcVersion ,
10
10
#[ serde( flatten) ]
11
11
pub params : JrpcCall ,
12
- pub id : i64 ,
12
+ pub id : Option < i64 > ,
13
13
}
14
14
15
15
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
@@ -20,14 +20,14 @@ pub enum JrpcCall {
20
20
GetMetadata ( GetMetadataParams ) ,
21
21
}
22
22
23
- #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
23
+ #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq , Clone ) ]
24
24
pub struct FeedUpdateParams {
25
25
pub feed_id : PriceFeedId ,
26
26
pub source_timestamp : TimestampUs ,
27
27
pub update : UpdateParams ,
28
28
}
29
29
30
- #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
30
+ #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq , Clone ) ]
31
31
#[ serde( tag = "type" ) ]
32
32
pub enum UpdateParams {
33
33
#[ serde( rename = "price" ) ]
@@ -59,6 +59,7 @@ pub enum JsonRpcVersion {
59
59
}
60
60
61
61
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
62
+ #[ serde( untagged) ]
62
63
pub enum JrpcResponse < T > {
63
64
Success ( JrpcSuccessResponse < T > ) ,
64
65
Error ( JrpcErrorResponse ) ,
@@ -89,7 +90,8 @@ pub struct JrpcErrorObject {
89
90
#[ derive( Debug , Eq , PartialEq ) ]
90
91
pub enum JrpcError {
91
92
ParseError ( String ) ,
92
- InternalError ,
93
+ InternalError ( String ) ,
94
+ SendUpdateError ( FeedUpdateParams ) ,
93
95
}
94
96
95
97
// note: error codes can be found in the rfc https://www.jsonrpc.org/specification#error_object
@@ -101,10 +103,15 @@ impl From<JrpcError> for JrpcErrorObject {
101
103
message : "Parse error" . to_string ( ) ,
102
104
data : Some ( error_message. into ( ) ) ,
103
105
} ,
104
- JrpcError :: InternalError => JrpcErrorObject {
106
+ JrpcError :: InternalError ( error_message ) => JrpcErrorObject {
105
107
code : -32603 ,
106
108
message : "Internal error" . to_string ( ) ,
107
- data : None ,
109
+ data : Some ( error_message. into ( ) ) ,
110
+ } ,
111
+ JrpcError :: SendUpdateError ( feed_update_params) => JrpcErrorObject {
112
+ code : -32000 ,
113
+ message : "Internal error" . to_string ( ) ,
114
+ data : Some ( serde_json:: to_value ( feed_update_params) . unwrap ( ) ) ,
108
115
} ,
109
116
}
110
117
}
@@ -165,7 +172,47 @@ mod tests {
165
172
best_ask_price : Some ( Price :: from_integer ( 1234567892 , 0 ) . unwrap ( ) ) ,
166
173
} ,
167
174
} ) ,
168
- id : 1 ,
175
+ id : Some ( 1 ) ,
176
+ } ;
177
+
178
+ assert_eq ! (
179
+ serde_json:: from_str:: <PythLazerAgentJrpcV1 >( json) . unwrap( ) ,
180
+ expected
181
+ ) ;
182
+ }
183
+
184
+ #[ test]
185
+ fn test_push_update_price_without_id ( ) {
186
+ let json = r#"
187
+ {
188
+ "jsonrpc": "2.0",
189
+ "method": "push_update",
190
+ "params": {
191
+ "feed_id": 1,
192
+ "source_timestamp": 745214124124,
193
+
194
+ "update": {
195
+ "type": "price",
196
+ "price": 5432,
197
+ "best_bid_price": 5432,
198
+ "best_ask_price": 5432
199
+ }
200
+ }
201
+ }
202
+ "# ;
203
+
204
+ let expected = PythLazerAgentJrpcV1 {
205
+ jsonrpc : JsonRpcVersion :: V2 ,
206
+ params : PushUpdate ( FeedUpdateParams {
207
+ feed_id : PriceFeedId ( 1 ) ,
208
+ source_timestamp : TimestampUs :: from_micros ( 745214124124 ) ,
209
+ update : UpdateParams :: PriceUpdate {
210
+ price : Price :: from_integer ( 5432 , 0 ) . unwrap ( ) ,
211
+ best_bid_price : Some ( Price :: from_integer ( 5432 , 0 ) . unwrap ( ) ) ,
212
+ best_ask_price : Some ( Price :: from_integer ( 5432 , 0 ) . unwrap ( ) ) ,
213
+ } ,
214
+ } ) ,
215
+ id : None ,
169
216
} ;
170
217
171
218
assert_eq ! (
@@ -204,7 +251,7 @@ mod tests {
204
251
best_ask_price : None ,
205
252
} ,
206
253
} ) ,
207
- id : 1 ,
254
+ id : Some ( 1 ) ,
208
255
} ;
209
256
210
257
assert_eq ! (
@@ -243,7 +290,7 @@ mod tests {
243
290
rate : Rate :: from_integer ( 1234567891 , 0 ) . unwrap ( ) ,
244
291
} ,
245
292
} ) ,
246
- id : 1 ,
293
+ id : Some ( 1 ) ,
247
294
} ;
248
295
249
296
assert_eq ! (
@@ -280,7 +327,7 @@ mod tests {
280
327
rate : Rate :: from_integer ( 1234567891 , 0 ) . unwrap ( ) ,
281
328
} ,
282
329
} ) ,
283
- id : 1 ,
330
+ id : Some ( 1 ) ,
284
331
} ;
285
332
286
333
assert_eq ! (
@@ -309,7 +356,7 @@ mod tests {
309
356
names : Some ( vec ! [ "BTC/USD" . to_string( ) ] ) ,
310
357
asset_types : Some ( vec ! [ "crypto" . to_string( ) ] ) ,
311
358
} ) ,
312
- id : 1 ,
359
+ id : Some ( 1 ) ,
313
360
} ;
314
361
315
362
assert_eq ! (
@@ -335,7 +382,7 @@ mod tests {
335
382
names : None ,
336
383
asset_types : None ,
337
384
} ) ,
338
- id : 1 ,
385
+ id : Some ( 1 ) ,
339
386
} ;
340
387
341
388
assert_eq ! (
@@ -396,4 +443,52 @@ mod tests {
396
443
}
397
444
) ;
398
445
}
446
+
447
+ #[ test]
448
+ pub fn test_parse_response ( ) {
449
+ let success_response = serde_json:: from_str :: < JrpcResponse < String > > (
450
+ r#"
451
+ {
452
+ "jsonrpc": "2.0",
453
+ "id": 2,
454
+ "result": "success"
455
+ }"# ,
456
+ )
457
+ . unwrap ( ) ;
458
+
459
+ assert_eq ! (
460
+ success_response,
461
+ JrpcResponse :: Success ( JrpcSuccessResponse :: <String > {
462
+ jsonrpc: JsonRpcVersion :: V2 ,
463
+ result: "success" . to_string( ) ,
464
+ id: 2 ,
465
+ } )
466
+ ) ;
467
+
468
+ let error_response = serde_json:: from_str :: < JrpcResponse < String > > (
469
+ r#"
470
+ {
471
+ "jsonrpc": "2.0",
472
+ "id": 3,
473
+ "error": {
474
+ "code": -32603,
475
+ "message": "Internal error"
476
+ }
477
+ }"# ,
478
+ )
479
+ . unwrap ( ) ;
480
+
481
+ assert_eq ! (
482
+ error_response,
483
+ JrpcResponse :: Error ( JrpcErrorResponse {
484
+ jsonrpc: JsonRpcVersion :: V2 ,
485
+ error: JrpcErrorObject {
486
+ code: -32603 ,
487
+ message: "Internal error" . to_string( ) ,
488
+ data: None ,
489
+ } ,
490
+ id: Some ( 3 ) ,
491
+ } )
492
+ ) ;
493
+ }
399
494
}
0 commit comments