Skip to content

Commit f5355d2

Browse files
authored
fix(lazer): Support int/string/null/missing jrpc id field
2 parents 5a132e5 + 827e74b commit f5355d2

File tree

6 files changed

+111
-19
lines changed

6 files changed

+111
-19
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.16.0" }
22+
pyth-lazer-protocol = { path = "../../../../sdk/rust/protocol", version = "0.17.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.14.0"
3+
version = "0.15.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.16.0", path = "../../sdk/rust/protocol" }
10+
pyth-lazer-protocol = { version = "0.17.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.2.2"
3+
version = "8.3.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.16.0" }
9+
pyth-lazer-protocol = { path = "../protocol", version = "0.17.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.16.0"
3+
version = "0.17.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: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ use crate::{api::Channel, price::Price};
66
use serde::{Deserialize, Serialize};
77
use std::time::Duration;
88

9+
#[derive(Serialize, Deserialize, Debug, Default, Eq, PartialEq)]
10+
#[serde(untagged)]
11+
pub enum JrpcId {
12+
String(String),
13+
Int(i64),
14+
#[default]
15+
Null,
16+
}
17+
918
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
1019
pub struct PythLazerAgentJrpcV1 {
1120
pub jsonrpc: JsonRpcVersion,
1221
#[serde(flatten)]
1322
pub params: JrpcCall,
14-
pub id: Option<i64>,
23+
#[serde(default)]
24+
pub id: JrpcId,
1525
}
1626

1727
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
@@ -184,7 +194,89 @@ mod tests {
184194
best_ask_price: Some(Price::from_integer(1234567892, 0).unwrap()),
185195
},
186196
}),
187-
id: Some(1),
197+
id: JrpcId::Int(1),
198+
};
199+
200+
assert_eq!(
201+
serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
202+
expected
203+
);
204+
}
205+
206+
#[test]
207+
fn test_push_update_price_string_id() {
208+
let json = r#"
209+
{
210+
"jsonrpc": "2.0",
211+
"method": "push_update",
212+
"params": {
213+
"feed_id": 1,
214+
"source_timestamp": 124214124124,
215+
216+
"update": {
217+
"type": "price",
218+
"price": 1234567890,
219+
"best_bid_price": 1234567891,
220+
"best_ask_price": 1234567892
221+
}
222+
},
223+
"id": "b6bb54a0-ea8d-439d-97a7-3b06befa0e76"
224+
}
225+
"#;
226+
227+
let expected = PythLazerAgentJrpcV1 {
228+
jsonrpc: JsonRpcVersion::V2,
229+
params: PushUpdate(FeedUpdateParams {
230+
feed_id: PriceFeedId(1),
231+
source_timestamp: TimestampUs::from_micros(124214124124),
232+
update: UpdateParams::PriceUpdate {
233+
price: Price::from_integer(1234567890, 0).unwrap(),
234+
best_bid_price: Some(Price::from_integer(1234567891, 0).unwrap()),
235+
best_ask_price: Some(Price::from_integer(1234567892, 0).unwrap()),
236+
},
237+
}),
238+
id: JrpcId::String("b6bb54a0-ea8d-439d-97a7-3b06befa0e76".to_string()),
239+
};
240+
241+
assert_eq!(
242+
serde_json::from_str::<PythLazerAgentJrpcV1>(json).unwrap(),
243+
expected
244+
);
245+
}
246+
247+
#[test]
248+
fn test_push_update_price_null_id() {
249+
let json = r#"
250+
{
251+
"jsonrpc": "2.0",
252+
"method": "push_update",
253+
"params": {
254+
"feed_id": 1,
255+
"source_timestamp": 124214124124,
256+
257+
"update": {
258+
"type": "price",
259+
"price": 1234567890,
260+
"best_bid_price": 1234567891,
261+
"best_ask_price": 1234567892
262+
}
263+
},
264+
"id": null
265+
}
266+
"#;
267+
268+
let expected = PythLazerAgentJrpcV1 {
269+
jsonrpc: JsonRpcVersion::V2,
270+
params: PushUpdate(FeedUpdateParams {
271+
feed_id: PriceFeedId(1),
272+
source_timestamp: TimestampUs::from_micros(124214124124),
273+
update: UpdateParams::PriceUpdate {
274+
price: Price::from_integer(1234567890, 0).unwrap(),
275+
best_bid_price: Some(Price::from_integer(1234567891, 0).unwrap()),
276+
best_ask_price: Some(Price::from_integer(1234567892, 0).unwrap()),
277+
},
278+
}),
279+
id: JrpcId::Null,
188280
};
189281

190282
assert_eq!(
@@ -224,7 +316,7 @@ mod tests {
224316
best_ask_price: Some(Price::from_integer(5432, 0).unwrap()),
225317
},
226318
}),
227-
id: None,
319+
id: JrpcId::Null,
228320
};
229321

230322
assert_eq!(
@@ -263,7 +355,7 @@ mod tests {
263355
best_ask_price: None,
264356
},
265357
}),
266-
id: Some(1),
358+
id: JrpcId::Int(1),
267359
};
268360

269361
assert_eq!(
@@ -304,7 +396,7 @@ mod tests {
304396
funding_rate_interval: Duration::from_secs(28800).into(),
305397
},
306398
}),
307-
id: Some(1),
399+
id: JrpcId::Int(1),
308400
};
309401

310402
assert_eq!(
@@ -342,7 +434,7 @@ mod tests {
342434
funding_rate_interval: None,
343435
},
344436
}),
345-
id: Some(1),
437+
id: JrpcId::Int(1),
346438
};
347439

348440
assert_eq!(
@@ -371,7 +463,7 @@ mod tests {
371463
names: Some(vec!["BTC/USD".to_string()]),
372464
asset_types: Some(vec!["crypto".to_string()]),
373465
}),
374-
id: Some(1),
466+
id: JrpcId::Int(1),
375467
};
376468

377469
assert_eq!(
@@ -397,7 +489,7 @@ mod tests {
397489
names: None,
398490
asset_types: None,
399491
}),
400-
id: Some(1),
492+
id: JrpcId::Int(1),
401493
};
402494

403495
assert_eq!(

0 commit comments

Comments
 (0)