Skip to content

Commit 6361828

Browse files
committed
add error response formats
1 parent a42a994 commit 6361828

File tree

1 file changed

+107
-25
lines changed

1 file changed

+107
-25
lines changed

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

Lines changed: 107 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::router::{Price, PriceFeedId, Rate, TimestampUs};
2-
use serde::{Deserialize, Serialize};
31
use std::time::Duration;
2+
use crate::router::{Channel, Price, PriceFeedId, Rate, TimestampUs};
3+
use serde::{Deserialize, Serialize};
4+
use crate::symbol_state::SymbolState;
45

56
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
67
pub struct PythLazerAgentJrpcV1 {
@@ -41,13 +42,13 @@ pub enum UpdateParams {
4142

4243
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
4344
pub struct Filter {
44-
name: Option<String>,
45-
asset_type: Option<String>,
45+
pub name: Option<String>,
46+
pub asset_type: Option<String>,
4647
}
4748

4849
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
4950
pub struct GetMetadataParams {
50-
filters: Option<Vec<Filter>>,
51+
pub filters: Option<Vec<Filter>>,
5152
}
5253

5354
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
@@ -57,42 +58,78 @@ pub enum JsonRpcVersion {
5758
}
5859

5960
#[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> {
6168
pub jsonrpc: JsonRpcVersion,
6269
pub result: T,
6370
pub id: i64,
6471
}
6572

6673
#[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,
6883
pub message: String,
84+
#[serde(skip_serializing_if = "Option::is_none")]
85+
pub data: Option<serde_json::Value>,
6986
}
7087

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,
82114
pub name: String,
83-
pub pyth_lazer_id: i64,
84-
pub schedule: String,
85-
pub state: String,
86115
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>,
87127
}
88128

89129
#[cfg(test)]
90130
mod tests {
91131
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};
96133
use crate::router::{Price, PriceFeedId, Rate, TimestampUs};
97134

98135
#[test]
@@ -235,4 +272,49 @@ mod tests {
235272
expected
236273
);
237274
}
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+
}
238320
}

0 commit comments

Comments
 (0)