1
- use crate :: router:: { Price , PriceFeedId , Rate , TimestampUs } ;
2
- use serde:: { Deserialize , Serialize } ;
3
1
use std:: time:: Duration ;
2
+ use crate :: router:: { Channel , Price , PriceFeedId , Rate , TimestampUs } ;
3
+ use serde:: { Deserialize , Serialize } ;
4
+ use crate :: symbol_state:: SymbolState ;
4
5
5
6
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
6
7
pub struct PythLazerAgentJrpcV1 {
@@ -41,13 +42,13 @@ pub enum UpdateParams {
41
42
42
43
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
43
44
pub struct Filter {
44
- name : Option < String > ,
45
- asset_type : Option < String > ,
45
+ pub name : Option < String > ,
46
+ pub asset_type : Option < String > ,
46
47
}
47
48
48
49
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
49
50
pub struct GetMetadataParams {
50
- filters : Option < Vec < Filter > > ,
51
+ pub filters : Option < Vec < Filter > > ,
51
52
}
52
53
53
54
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
@@ -57,42 +58,78 @@ pub enum JsonRpcVersion {
57
58
}
58
59
59
60
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
60
- pub struct JrpcResponse < T > {
61
+ pub enum JrpcResponse < T > {
62
+ Success ( JrpcSuccessResponse < T > ) ,
63
+ Error ( JrpcErrorResponse ) ,
64
+ }
65
+
66
+ #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
67
+ pub struct JrpcSuccessResponse < T > {
61
68
pub jsonrpc : JsonRpcVersion ,
62
69
pub result : T ,
63
70
pub id : i64 ,
64
71
}
65
72
66
73
#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
67
- pub struct ErrorResponse {
74
+ pub struct JrpcErrorResponse {
75
+ pub jsonrpc : JsonRpcVersion ,
76
+ pub error : JrpcErrorObject ,
77
+ pub id : Option < i64 > ,
78
+ }
79
+
80
+ #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
81
+ pub struct JrpcErrorObject {
82
+ pub code : i64 ,
68
83
pub message : String ,
84
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
85
+ pub data : Option < serde_json:: Value > ,
69
86
}
70
87
71
- #[ derive( Serialize , Deserialize ) ]
72
- struct SymbolMetadata {
73
- pub asset_type : String ,
74
- pub cmc_id : i64 ,
75
- pub description : String ,
76
- pub exponent : i64 ,
77
- pub hermes_id : String ,
78
- #[ serde( default , with = "humantime_serde" , alias = "interval" ) ]
79
- pub interval : Option < Duration > ,
80
- pub min_channel : String ,
81
- pub min_publishers : i64 ,
88
+ #[ derive( Debug , Eq , PartialEq ) ]
89
+ pub enum JrpcError {
90
+ ParseError ,
91
+ InternalError ,
92
+ }
93
+
94
+ impl From < JrpcError > for JrpcErrorObject {
95
+ fn from ( error : JrpcError ) -> Self {
96
+ match error {
97
+ JrpcError :: ParseError => JrpcErrorObject {
98
+ code : -32700 ,
99
+ message : "Parse error" . to_string ( ) ,
100
+ data : None ,
101
+ } ,
102
+ JrpcError :: InternalError => JrpcErrorObject {
103
+ code : -32603 ,
104
+ message : "Internal error" . to_string ( ) ,
105
+ data : None ,
106
+ } ,
107
+ }
108
+ }
109
+ }
110
+
111
+ #[ derive( Serialize , Deserialize , Clone , Debug , PartialEq , Eq , Hash ) ]
112
+ pub struct SymbolMetadata {
113
+ pub pyth_lazer_id : PriceFeedId ,
82
114
pub name : String ,
83
- pub pyth_lazer_id : i64 ,
84
- pub schedule : String ,
85
- pub state : String ,
86
115
pub symbol : String ,
116
+ pub description : String ,
117
+ pub asset_type : String ,
118
+ pub exponent : i16 ,
119
+ pub cmc_id : Option < u32 > ,
120
+ #[ serde( default , with = "humantime_serde" , alias = "interval" ) ]
121
+ pub funding_rate_interval : Option < Duration > ,
122
+ pub min_publishers : u16 ,
123
+ pub min_channel : Channel ,
124
+ pub state : SymbolState ,
125
+ pub hermes_id : Option < String > ,
126
+ pub quote_currency : Option < String > ,
87
127
}
88
128
89
129
#[ cfg( test) ]
90
130
mod tests {
91
131
use crate :: jrpc:: JrpcParams :: { GetMetadata , SendUpdates } ;
92
- use crate :: jrpc:: {
93
- FeedUpdateParams , Filter , GetMetadataParams , JsonRpcVersion , PythLazerAgentJrpcV1 ,
94
- UpdateParams ,
95
- } ;
132
+ use crate :: jrpc:: { FeedUpdateParams , Filter , GetMetadataParams , JrpcErrorObject , JrpcErrorResponse , JrpcSuccessResponse , JsonRpcVersion , PythLazerAgentJrpcV1 , UpdateParams } ;
96
133
use crate :: router:: { Price , PriceFeedId , Rate , TimestampUs } ;
97
134
98
135
#[ test]
@@ -235,4 +272,49 @@ mod tests {
235
272
expected
236
273
) ;
237
274
}
275
+
276
+ #[ test]
277
+ fn test_response_format_error ( ) {
278
+ let response = serde_json:: from_str :: < JrpcErrorResponse > (
279
+ r#"
280
+ {
281
+ "jsonrpc": "2.0",
282
+ "id": 2,
283
+ "error": {
284
+ "message": "Internal error",
285
+ "code": -32603
286
+ }
287
+ }
288
+ "#
289
+ ) . unwrap ( ) ;
290
+
291
+ assert_eq ! ( response, JrpcErrorResponse {
292
+ jsonrpc: JsonRpcVersion :: V2 ,
293
+ error: JrpcErrorObject {
294
+ code: -32603 ,
295
+ message: "Internal error" . to_string( ) ,
296
+ data: None ,
297
+ } ,
298
+ id: Some ( 2 ) ,
299
+ } ) ;
300
+ }
301
+
302
+ #[ test]
303
+ pub fn test_response_format_success ( ) {
304
+ let response = serde_json:: from_str :: < JrpcSuccessResponse < String > > (
305
+ r#"
306
+ {
307
+ "jsonrpc": "2.0",
308
+ "id": 2,
309
+ "result": "success"
310
+ }
311
+ "#
312
+ ) . unwrap ( ) ;
313
+
314
+ assert_eq ! ( response, JrpcSuccessResponse :: <String > {
315
+ jsonrpc: JsonRpcVersion :: V2 ,
316
+ result: "success" . to_string( ) ,
317
+ id: 2 ,
318
+ } ) ;
319
+ }
238
320
}
0 commit comments