Skip to content

Commit 1e01ca0

Browse files
committed
update error response
1 parent d62a045 commit 1e01ca0

File tree

1 file changed

+107
-12
lines changed

1 file changed

+107
-12
lines changed

lazer/sdk/rust/protocol/src/jrpc.rs

Lines changed: 107 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct PythLazerAgentJrpcV1 {
99
pub jsonrpc: JsonRpcVersion,
1010
#[serde(flatten)]
1111
pub params: JrpcCall,
12-
pub id: i64,
12+
pub id: Option<i64>,
1313
}
1414

1515
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
@@ -20,14 +20,14 @@ pub enum JrpcCall {
2020
GetMetadata(GetMetadataParams),
2121
}
2222

23-
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
23+
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
2424
pub struct FeedUpdateParams {
2525
pub feed_id: PriceFeedId,
2626
pub source_timestamp: TimestampUs,
2727
pub update: UpdateParams,
2828
}
2929

30-
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
30+
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
3131
#[serde(tag = "type")]
3232
pub enum UpdateParams {
3333
#[serde(rename = "price")]
@@ -59,6 +59,7 @@ pub enum JsonRpcVersion {
5959
}
6060

6161
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
62+
#[serde(untagged)]
6263
pub enum JrpcResponse<T> {
6364
Success(JrpcSuccessResponse<T>),
6465
Error(JrpcErrorResponse),
@@ -89,7 +90,8 @@ pub struct JrpcErrorObject {
8990
#[derive(Debug, Eq, PartialEq)]
9091
pub enum JrpcError {
9192
ParseError(String),
92-
InternalError,
93+
InternalError(String),
94+
SendUpdateError(FeedUpdateParams),
9395
}
9496

9597
// 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 {
101103
message: "Parse error".to_string(),
102104
data: Some(error_message.into()),
103105
},
104-
JrpcError::InternalError => JrpcErrorObject {
106+
JrpcError::InternalError(error_message) => JrpcErrorObject {
105107
code: -32603,
106108
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()),
108115
},
109116
}
110117
}
@@ -165,7 +172,47 @@ mod tests {
165172
best_ask_price: Some(Price::from_integer(1234567892, 0).unwrap()),
166173
},
167174
}),
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,
169216
};
170217

171218
assert_eq!(
@@ -204,7 +251,7 @@ mod tests {
204251
best_ask_price: None,
205252
},
206253
}),
207-
id: 1,
254+
id: Some(1),
208255
};
209256

210257
assert_eq!(
@@ -243,7 +290,7 @@ mod tests {
243290
rate: Rate::from_integer(1234567891, 0).unwrap(),
244291
},
245292
}),
246-
id: 1,
293+
id: Some(1),
247294
};
248295

249296
assert_eq!(
@@ -280,7 +327,7 @@ mod tests {
280327
rate: Rate::from_integer(1234567891, 0).unwrap(),
281328
},
282329
}),
283-
id: 1,
330+
id: Some(1),
284331
};
285332

286333
assert_eq!(
@@ -309,7 +356,7 @@ mod tests {
309356
names: Some(vec!["BTC/USD".to_string()]),
310357
asset_types: Some(vec!["crypto".to_string()]),
311358
}),
312-
id: 1,
359+
id: Some(1),
313360
};
314361

315362
assert_eq!(
@@ -335,7 +382,7 @@ mod tests {
335382
names: None,
336383
asset_types: None,
337384
}),
338-
id: 1,
385+
id: Some(1),
339386
};
340387

341388
assert_eq!(
@@ -396,4 +443,52 @@ mod tests {
396443
}
397444
);
398445
}
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+
}
399494
}

0 commit comments

Comments
 (0)