Skip to content

Commit 6fe8594

Browse files
authored
fix(lazer): Support int/string/null/missing jrpc id field in success/error responses
2 parents f5355d2 + 19cd15b commit 6fe8594

File tree

6 files changed

+118
-17
lines changed

6 files changed

+118
-17
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lazer/contracts/solana/programs/pyth-lazer-solana-contract/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ no-log-ix-name = []
1919
idl-build = ["anchor-lang/idl-build"]
2020

2121
[dependencies]
22-
pyth-lazer-protocol = { path = "../../../../sdk/rust/protocol", version = "0.17.0" }
22+
pyth-lazer-protocol = { path = "../../../../sdk/rust/protocol", version = "0.18.0" }
2323

2424
anchor-lang = "0.31.1"
2525
bytemuck = { version = "1.20.0", features = ["derive"] }

lazer/publisher_sdk/rust/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[package]
22
name = "pyth-lazer-publisher-sdk"
3-
version = "0.15.0"
3+
version = "0.16.0"
44
edition = "2021"
55
description = "Pyth Lazer Publisher SDK types."
66
license = "Apache-2.0"
77
repository = "https://github.com/pyth-network/pyth-crosschain"
88

99
[dependencies]
10-
pyth-lazer-protocol = { version = "0.17.0", path = "../../sdk/rust/protocol" }
10+
pyth-lazer-protocol = { version = "0.18.0", path = "../../sdk/rust/protocol" }
1111
anyhow = "1.0.98"
1212
protobuf = "3.7.2"
1313
serde_json = "1.0.140"

lazer/sdk/rust/client/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "pyth-lazer-client"
3-
version = "8.3.0"
3+
version = "8.4.0"
44
edition = "2021"
55
description = "A Rust client for Pyth Lazer"
66
license = "Apache-2.0"
77

88
[dependencies]
9-
pyth-lazer-protocol = { path = "../protocol", version = "0.17.0" }
9+
pyth-lazer-protocol = { path = "../protocol", version = "0.18.0" }
1010
tokio = { version = "1", features = ["full"] }
1111
tokio-tungstenite = { version = "0.20", features = ["native-tls"] }
1212
futures-util = "0.3"

lazer/sdk/rust/protocol/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-lazer-protocol"
3-
version = "0.17.0"
3+
version = "0.18.0"
44
edition = "2021"
55
description = "Pyth Lazer SDK - protocol types."
66
license = "Apache-2.0"

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

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ pub enum JrpcResponse<T> {
9191
pub struct JrpcSuccessResponse<T> {
9292
pub jsonrpc: JsonRpcVersion,
9393
pub result: T,
94-
pub id: i64,
94+
pub id: JrpcId,
9595
}
9696

9797
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
9898
pub struct JrpcErrorResponse {
9999
pub jsonrpc: JsonRpcVersion,
100100
pub error: JrpcErrorObject,
101-
pub id: Option<i64>,
101+
pub id: JrpcId,
102102
}
103103

104104
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
@@ -523,7 +523,37 @@ mod tests {
523523
message: "Internal error".to_string(),
524524
data: None,
525525
},
526-
id: Some(2),
526+
id: JrpcId::Int(2),
527+
}
528+
);
529+
}
530+
531+
#[test]
532+
fn test_response_format_error_string_id() {
533+
let response = serde_json::from_str::<JrpcErrorResponse>(
534+
r#"
535+
{
536+
"jsonrpc": "2.0",
537+
"id": "62b627dc-5599-43dd-b2c2-9c4d30f4fdb4",
538+
"error": {
539+
"message": "Internal error",
540+
"code": -32603
541+
}
542+
}
543+
"#,
544+
)
545+
.unwrap();
546+
547+
assert_eq!(
548+
response,
549+
JrpcErrorResponse {
550+
jsonrpc: JsonRpcVersion::V2,
551+
error: JrpcErrorObject {
552+
code: -32603,
553+
message: "Internal error".to_string(),
554+
data: None,
555+
},
556+
id: JrpcId::String("62b627dc-5599-43dd-b2c2-9c4d30f4fdb4".to_string())
527557
}
528558
);
529559
}
@@ -546,7 +576,30 @@ mod tests {
546576
JrpcSuccessResponse::<String> {
547577
jsonrpc: JsonRpcVersion::V2,
548578
result: "success".to_string(),
549-
id: 2,
579+
id: JrpcId::Int(2),
580+
}
581+
);
582+
}
583+
584+
#[test]
585+
pub fn test_response_format_success_string_id() {
586+
let response = serde_json::from_str::<JrpcSuccessResponse<String>>(
587+
r#"
588+
{
589+
"jsonrpc": "2.0",
590+
"id": "62b627dc-5599-43dd-b2c2-9c4d30f4fdb4",
591+
"result": "success"
592+
}
593+
"#,
594+
)
595+
.unwrap();
596+
597+
assert_eq!(
598+
response,
599+
JrpcSuccessResponse::<String> {
600+
jsonrpc: JsonRpcVersion::V2,
601+
result: "success".to_string(),
602+
id: JrpcId::String("62b627dc-5599-43dd-b2c2-9c4d30f4fdb4".to_string()),
550603
}
551604
);
552605
}
@@ -568,7 +621,7 @@ mod tests {
568621
JrpcResponse::Success(JrpcSuccessResponse::<String> {
569622
jsonrpc: JsonRpcVersion::V2,
570623
result: "success".to_string(),
571-
id: 2,
624+
id: JrpcId::Int(2),
572625
})
573626
);
574627

@@ -594,7 +647,55 @@ mod tests {
594647
message: "Internal error".to_string(),
595648
data: None,
596649
},
597-
id: Some(3),
650+
id: JrpcId::Int(3),
651+
})
652+
);
653+
}
654+
655+
#[test]
656+
pub fn test_parse_response_string_id() {
657+
let success_response = serde_json::from_str::<JrpcResponse<String>>(
658+
r#"
659+
{
660+
"jsonrpc": "2.0",
661+
"id": "id-2",
662+
"result": "success"
663+
}"#,
664+
)
665+
.unwrap();
666+
667+
assert_eq!(
668+
success_response,
669+
JrpcResponse::Success(JrpcSuccessResponse::<String> {
670+
jsonrpc: JsonRpcVersion::V2,
671+
result: "success".to_string(),
672+
id: JrpcId::String("id-2".to_string()),
673+
})
674+
);
675+
676+
let error_response = serde_json::from_str::<JrpcResponse<String>>(
677+
r#"
678+
{
679+
"jsonrpc": "2.0",
680+
"id": "id-3",
681+
"error": {
682+
"code": -32603,
683+
"message": "Internal error"
684+
}
685+
}"#,
686+
)
687+
.unwrap();
688+
689+
assert_eq!(
690+
error_response,
691+
JrpcResponse::Error(JrpcErrorResponse {
692+
jsonrpc: JsonRpcVersion::V2,
693+
error: JrpcErrorObject {
694+
code: -32603,
695+
message: "Internal error".to_string(),
696+
data: None,
697+
},
698+
id: JrpcId::String("id-3".to_string()),
598699
})
599700
);
600701
}

0 commit comments

Comments
 (0)