@@ -6,12 +6,22 @@ use crate::{api::Channel, price::Price};
66use serde:: { Deserialize , Serialize } ;
77use std:: time:: Duration ;
88
9+ #[ derive( Serialize , Deserialize , Debug , Default , Eq , PartialEq ) ]
10+ #[ serde( untagged) ]
11+ pub enum JrpcId {
12+ String ( String ) ,
13+ Int ( i64 ) ,
14+ #[ default]
15+ Null ,
16+ }
17+
918#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
1019pub struct PythLazerAgentJrpcV1 {
1120 pub jsonrpc : JsonRpcVersion ,
1221 #[ serde( flatten) ]
1322 pub params : JrpcCall ,
14- pub id : Option < i64 > ,
23+ #[ serde( default ) ]
24+ pub id : JrpcId ,
1525}
1626
1727#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
@@ -184,7 +194,89 @@ mod tests {
184194 best_ask_price : Some ( Price :: from_integer ( 1234567892 , 0 ) . unwrap ( ) ) ,
185195 } ,
186196 } ) ,
187- id : Some ( 1 ) ,
197+ id : JrpcId :: Int ( 1 ) ,
198+ } ;
199+
200+ assert_eq ! (
201+ serde_json:: from_str:: <PythLazerAgentJrpcV1 >( json) . unwrap( ) ,
202+ expected
203+ ) ;
204+ }
205+
206+ #[ test]
207+ fn test_push_update_price_string_id ( ) {
208+ let json = r#"
209+ {
210+ "jsonrpc": "2.0",
211+ "method": "push_update",
212+ "params": {
213+ "feed_id": 1,
214+ "source_timestamp": 124214124124,
215+
216+ "update": {
217+ "type": "price",
218+ "price": 1234567890,
219+ "best_bid_price": 1234567891,
220+ "best_ask_price": 1234567892
221+ }
222+ },
223+ "id": "b6bb54a0-ea8d-439d-97a7-3b06befa0e76"
224+ }
225+ "# ;
226+
227+ let expected = PythLazerAgentJrpcV1 {
228+ jsonrpc : JsonRpcVersion :: V2 ,
229+ params : PushUpdate ( FeedUpdateParams {
230+ feed_id : PriceFeedId ( 1 ) ,
231+ source_timestamp : TimestampUs :: from_micros ( 124214124124 ) ,
232+ update : UpdateParams :: PriceUpdate {
233+ price : Price :: from_integer ( 1234567890 , 0 ) . unwrap ( ) ,
234+ best_bid_price : Some ( Price :: from_integer ( 1234567891 , 0 ) . unwrap ( ) ) ,
235+ best_ask_price : Some ( Price :: from_integer ( 1234567892 , 0 ) . unwrap ( ) ) ,
236+ } ,
237+ } ) ,
238+ id : JrpcId :: String ( "b6bb54a0-ea8d-439d-97a7-3b06befa0e76" . to_string ( ) ) ,
239+ } ;
240+
241+ assert_eq ! (
242+ serde_json:: from_str:: <PythLazerAgentJrpcV1 >( json) . unwrap( ) ,
243+ expected
244+ ) ;
245+ }
246+
247+ #[ test]
248+ fn test_push_update_price_null_id ( ) {
249+ let json = r#"
250+ {
251+ "jsonrpc": "2.0",
252+ "method": "push_update",
253+ "params": {
254+ "feed_id": 1,
255+ "source_timestamp": 124214124124,
256+
257+ "update": {
258+ "type": "price",
259+ "price": 1234567890,
260+ "best_bid_price": 1234567891,
261+ "best_ask_price": 1234567892
262+ }
263+ },
264+ "id": null
265+ }
266+ "# ;
267+
268+ let expected = PythLazerAgentJrpcV1 {
269+ jsonrpc : JsonRpcVersion :: V2 ,
270+ params : PushUpdate ( FeedUpdateParams {
271+ feed_id : PriceFeedId ( 1 ) ,
272+ source_timestamp : TimestampUs :: from_micros ( 124214124124 ) ,
273+ update : UpdateParams :: PriceUpdate {
274+ price : Price :: from_integer ( 1234567890 , 0 ) . unwrap ( ) ,
275+ best_bid_price : Some ( Price :: from_integer ( 1234567891 , 0 ) . unwrap ( ) ) ,
276+ best_ask_price : Some ( Price :: from_integer ( 1234567892 , 0 ) . unwrap ( ) ) ,
277+ } ,
278+ } ) ,
279+ id : JrpcId :: Null ,
188280 } ;
189281
190282 assert_eq ! (
@@ -224,7 +316,7 @@ mod tests {
224316 best_ask_price : Some ( Price :: from_integer ( 5432 , 0 ) . unwrap ( ) ) ,
225317 } ,
226318 } ) ,
227- id : None ,
319+ id : JrpcId :: Null ,
228320 } ;
229321
230322 assert_eq ! (
@@ -263,7 +355,7 @@ mod tests {
263355 best_ask_price : None ,
264356 } ,
265357 } ) ,
266- id : Some ( 1 ) ,
358+ id : JrpcId :: Int ( 1 ) ,
267359 } ;
268360
269361 assert_eq ! (
@@ -304,7 +396,7 @@ mod tests {
304396 funding_rate_interval : Duration :: from_secs ( 28800 ) . into ( ) ,
305397 } ,
306398 } ) ,
307- id : Some ( 1 ) ,
399+ id : JrpcId :: Int ( 1 ) ,
308400 } ;
309401
310402 assert_eq ! (
@@ -342,7 +434,7 @@ mod tests {
342434 funding_rate_interval : None ,
343435 } ,
344436 } ) ,
345- id : Some ( 1 ) ,
437+ id : JrpcId :: Int ( 1 ) ,
346438 } ;
347439
348440 assert_eq ! (
@@ -371,7 +463,7 @@ mod tests {
371463 names : Some ( vec ! [ "BTC/USD" . to_string( ) ] ) ,
372464 asset_types : Some ( vec ! [ "crypto" . to_string( ) ] ) ,
373465 } ) ,
374- id : Some ( 1 ) ,
466+ id : JrpcId :: Int ( 1 ) ,
375467 } ;
376468
377469 assert_eq ! (
@@ -397,7 +489,7 @@ mod tests {
397489 names : None ,
398490 asset_types : None ,
399491 } ) ,
400- id : Some ( 1 ) ,
492+ id : JrpcId :: Int ( 1 ) ,
401493 } ;
402494
403495 assert_eq ! (
0 commit comments