Ver código fonte

fix(lazer): Support int/string/null/missing jrpc id field in success/error responses

Mike Rolish 1 mês atrás
pai
commit
6fe85941b2

+ 5 - 5
Cargo.lock

@@ -5693,7 +5693,7 @@ dependencies = [
 
 [[package]]
 name = "pyth-lazer-client"
-version = "8.3.0"
+version = "8.4.0"
 dependencies = [
  "alloy-primitives 0.8.25",
  "anyhow",
@@ -5711,7 +5711,7 @@ dependencies = [
  "hex",
  "humantime-serde",
  "libsecp256k1 0.7.2",
- "pyth-lazer-protocol 0.17.0",
+ "pyth-lazer-protocol 0.18.0",
  "reqwest 0.12.23",
  "serde",
  "serde_json",
@@ -5746,7 +5746,7 @@ dependencies = [
 
 [[package]]
 name = "pyth-lazer-protocol"
-version = "0.17.0"
+version = "0.18.0"
 dependencies = [
  "alloy-primitives 0.8.25",
  "anyhow",
@@ -5786,13 +5786,13 @@ dependencies = [
 
 [[package]]
 name = "pyth-lazer-publisher-sdk"
-version = "0.15.0"
+version = "0.16.0"
 dependencies = [
  "anyhow",
  "fs-err",
  "protobuf",
  "protobuf-codegen",
- "pyth-lazer-protocol 0.17.0",
+ "pyth-lazer-protocol 0.18.0",
  "serde_json",
 ]
 

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

@@ -19,7 +19,7 @@ no-log-ix-name = []
 idl-build = ["anchor-lang/idl-build"]
 
 [dependencies]
-pyth-lazer-protocol = { path = "../../../../sdk/rust/protocol", version = "0.17.0" }
+pyth-lazer-protocol = { path = "../../../../sdk/rust/protocol", version = "0.18.0" }
 
 anchor-lang = "0.31.1"
 bytemuck = { version = "1.20.0", features = ["derive"] }

+ 2 - 2
lazer/publisher_sdk/rust/Cargo.toml

@@ -1,13 +1,13 @@
 [package]
 name = "pyth-lazer-publisher-sdk"
-version = "0.15.0"
+version = "0.16.0"
 edition = "2021"
 description = "Pyth Lazer Publisher SDK types."
 license = "Apache-2.0"
 repository = "https://github.com/pyth-network/pyth-crosschain"
 
 [dependencies]
-pyth-lazer-protocol = { version = "0.17.0", path = "../../sdk/rust/protocol" }
+pyth-lazer-protocol = { version = "0.18.0", path = "../../sdk/rust/protocol" }
 anyhow = "1.0.98"
 protobuf = "3.7.2"
 serde_json = "1.0.140"

+ 2 - 2
lazer/sdk/rust/client/Cargo.toml

@@ -1,12 +1,12 @@
 [package]
 name = "pyth-lazer-client"
-version = "8.3.0"
+version = "8.4.0"
 edition = "2021"
 description = "A Rust client for Pyth Lazer"
 license = "Apache-2.0"
 
 [dependencies]
-pyth-lazer-protocol = { path = "../protocol", version = "0.17.0" }
+pyth-lazer-protocol = { path = "../protocol", version = "0.18.0" }
 tokio = { version = "1", features = ["full"] }
 tokio-tungstenite = { version = "0.20", features = ["native-tls"] }
 futures-util = "0.3"

+ 1 - 1
lazer/sdk/rust/protocol/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "pyth-lazer-protocol"
-version = "0.17.0"
+version = "0.18.0"
 edition = "2021"
 description = "Pyth Lazer SDK - protocol types."
 license = "Apache-2.0"

+ 107 - 6
lazer/sdk/rust/protocol/src/jrpc.rs

@@ -91,14 +91,14 @@ pub enum JrpcResponse<T> {
 pub struct JrpcSuccessResponse<T> {
     pub jsonrpc: JsonRpcVersion,
     pub result: T,
-    pub id: i64,
+    pub id: JrpcId,
 }
 
 #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
 pub struct JrpcErrorResponse {
     pub jsonrpc: JsonRpcVersion,
     pub error: JrpcErrorObject,
-    pub id: Option<i64>,
+    pub id: JrpcId,
 }
 
 #[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
@@ -523,7 +523,37 @@ mod tests {
                     message: "Internal error".to_string(),
                     data: None,
                 },
-                id: Some(2),
+                id: JrpcId::Int(2),
+            }
+        );
+    }
+
+    #[test]
+    fn test_response_format_error_string_id() {
+        let response = serde_json::from_str::<JrpcErrorResponse>(
+            r#"
+            {
+              "jsonrpc": "2.0",
+              "id": "62b627dc-5599-43dd-b2c2-9c4d30f4fdb4",
+              "error": {
+                "message": "Internal error",
+                "code": -32603
+              }
+            }
+            "#,
+        )
+        .unwrap();
+
+        assert_eq!(
+            response,
+            JrpcErrorResponse {
+                jsonrpc: JsonRpcVersion::V2,
+                error: JrpcErrorObject {
+                    code: -32603,
+                    message: "Internal error".to_string(),
+                    data: None,
+                },
+                id: JrpcId::String("62b627dc-5599-43dd-b2c2-9c4d30f4fdb4".to_string())
             }
         );
     }
@@ -546,7 +576,30 @@ mod tests {
             JrpcSuccessResponse::<String> {
                 jsonrpc: JsonRpcVersion::V2,
                 result: "success".to_string(),
-                id: 2,
+                id: JrpcId::Int(2),
+            }
+        );
+    }
+
+    #[test]
+    pub fn test_response_format_success_string_id() {
+        let response = serde_json::from_str::<JrpcSuccessResponse<String>>(
+            r#"
+            {
+              "jsonrpc": "2.0",
+              "id": "62b627dc-5599-43dd-b2c2-9c4d30f4fdb4",
+              "result": "success"
+            }
+            "#,
+        )
+        .unwrap();
+
+        assert_eq!(
+            response,
+            JrpcSuccessResponse::<String> {
+                jsonrpc: JsonRpcVersion::V2,
+                result: "success".to_string(),
+                id: JrpcId::String("62b627dc-5599-43dd-b2c2-9c4d30f4fdb4".to_string()),
             }
         );
     }
@@ -568,7 +621,7 @@ mod tests {
             JrpcResponse::Success(JrpcSuccessResponse::<String> {
                 jsonrpc: JsonRpcVersion::V2,
                 result: "success".to_string(),
-                id: 2,
+                id: JrpcId::Int(2),
             })
         );
 
@@ -594,7 +647,55 @@ mod tests {
                     message: "Internal error".to_string(),
                     data: None,
                 },
-                id: Some(3),
+                id: JrpcId::Int(3),
+            })
+        );
+    }
+
+    #[test]
+    pub fn test_parse_response_string_id() {
+        let success_response = serde_json::from_str::<JrpcResponse<String>>(
+            r#"
+            {
+              "jsonrpc": "2.0",
+              "id": "id-2",
+              "result": "success"
+            }"#,
+        )
+        .unwrap();
+
+        assert_eq!(
+            success_response,
+            JrpcResponse::Success(JrpcSuccessResponse::<String> {
+                jsonrpc: JsonRpcVersion::V2,
+                result: "success".to_string(),
+                id: JrpcId::String("id-2".to_string()),
+            })
+        );
+
+        let error_response = serde_json::from_str::<JrpcResponse<String>>(
+            r#"
+            {
+              "jsonrpc": "2.0",
+              "id": "id-3",
+              "error": {
+                "code": -32603,
+                "message": "Internal error"
+              }
+            }"#,
+        )
+        .unwrap();
+
+        assert_eq!(
+            error_response,
+            JrpcResponse::Error(JrpcErrorResponse {
+                jsonrpc: JsonRpcVersion::V2,
+                error: JrpcErrorObject {
+                    code: -32603,
+                    message: "Internal error".to_string(),
+                    data: None,
+                },
+                id: JrpcId::String("id-3".to_string()),
             })
         );
     }