Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lazer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lazer/sdk/rust/protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyth-lazer-protocol"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
description = "Pyth Lazer SDK - protocol types."
license = "Apache-2.0"
Expand All @@ -13,3 +13,6 @@ serde = { version = "1.0.210", features = ["derive"] }
derive_more = { version = "1.0.0", features = ["from"] }
itertools = "0.13.0"
rust_decimal = "1.36.0"

[dev-dependencies]
bincode = "1.3.3"
1 change: 1 addition & 0 deletions lazer/sdk/rust/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub mod message;
pub mod payload;
pub mod publisher;
pub mod router;
mod serde_price_as_i64;
mod serde_str;
pub mod subscription;
53 changes: 52 additions & 1 deletion lazer/sdk/rust/protocol/src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,64 @@ pub struct PriceFeedData {
pub source_timestamp_us: TimestampUs,
/// Timestamp of the last update provided by the publisher.
pub publisher_timestamp_us: TimestampUs,
/// Last known value of the "main" (?) price of this price feed.
/// Last known value of the best executable price of this price feed.
/// `None` if no value is currently available.
#[serde(with = "crate::serde_price_as_i64")]
pub price: Option<Price>,
/// Last known value of the best bid price of this price feed.
/// `None` if no value is currently available.
#[serde(with = "crate::serde_price_as_i64")]
pub best_bid_price: Option<Price>,
/// Last known value of the best ask price of this price feed.
/// `None` if no value is currently available.
#[serde(with = "crate::serde_price_as_i64")]
pub best_ask_price: Option<Price>,
}

#[test]
fn price_feed_data_serde() {
let data = [
1, 0, 0, 0, // price_feed_id
2, 0, 0, 0, 0, 0, 0, 0, // source_timestamp_us
3, 0, 0, 0, 0, 0, 0, 0, // publisher_timestamp_us
4, 0, 0, 0, 0, 0, 0, 0, // price
5, 0, 0, 0, 0, 0, 0, 0, // best_bid_price
6, 2, 0, 0, 0, 0, 0, 0, // best_ask_price
];

let expected = PriceFeedData {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: Some(Price(5.try_into().unwrap())),
best_ask_price: Some(Price((2 * 256 + 6).try_into().unwrap())),
};
assert_eq!(
bincode::deserialize::<PriceFeedData>(&data).unwrap(),
expected
);
assert_eq!(bincode::serialize(&expected).unwrap(), data);

let data2 = [
1, 0, 0, 0, // price_feed_id
2, 0, 0, 0, 0, 0, 0, 0, // source_timestamp_us
3, 0, 0, 0, 0, 0, 0, 0, // publisher_timestamp_us
4, 0, 0, 0, 0, 0, 0, 0, // price
0, 0, 0, 0, 0, 0, 0, 0, // best_bid_price
0, 0, 0, 0, 0, 0, 0, 0, // best_ask_price
];
let expected2 = PriceFeedData {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: None,
best_ask_price: None,
};
assert_eq!(
bincode::deserialize::<PriceFeedData>(&data2).unwrap(),
expected2
);
assert_eq!(bincode::serialize(&expected2).unwrap(), data2);
}
22 changes: 22 additions & 0 deletions lazer/sdk/rust/protocol/src/serde_price_as_i64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use {
crate::router::Price,
serde::{Deserialize, Deserializer, Serialize, Serializer},
std::num::NonZeroI64,
};

pub fn serialize<S>(value: &Option<Price>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
value
.map_or(0i64, |price| price.0.get())
.serialize(serializer)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Price>, D::Error>
where
D: Deserializer<'de>,
{
let value = i64::deserialize(deserializer)?;
Ok(NonZeroI64::new(value).map(Price))
}
Loading