Skip to content

feat(lazer): improve time types #2851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .github/workflows/ci-lazer-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ jobs:
- name: Clippy check
run: cargo clippy -p pyth-lazer-protocol -p pyth-lazer-client -p pyth-lazer-publisher-sdk --all-targets -- --deny warnings
if: success() || failure()
- name: Clippy check with mry
run: cargo clippy -F mry -p pyth-lazer-protocol --all-targets -- --deny warnings
if: success() || failure()
- name: test
run: cargo test -p pyth-lazer-protocol -p pyth-lazer-client -p pyth-lazer-publisher-sdk
if: success() || failure()
- name: test with mry
run: cargo test -F mry -p pyth-lazer-protocol
if: success() || failure()
40 changes: 40 additions & 0 deletions Cargo.lock

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

13 changes: 10 additions & 3 deletions lazer/contracts/solana/Cargo.lock

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

4 changes: 2 additions & 2 deletions lazer/publisher_sdk/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::{bail, ensure, Context};
use humantime::format_duration;
use protobuf::dynamic_value::{dynamic_value, DynamicValue};
use pyth_lazer_protocol::jrpc::{FeedUpdateParams, UpdateParams};
use pyth_lazer_protocol::router::TimestampUs;
use pyth_lazer_protocol::time::TimestampUs;

pub mod transaction_envelope {
pub use crate::protobuf::transaction_envelope::*;
Expand Down Expand Up @@ -141,7 +141,7 @@ impl TryFrom<DynamicValue> for serde_value::Value {
}
dynamic_value::Value::TimestampValue(ts) => {
let ts = TimestampUs::try_from(&ts)?;
Ok(serde_value::Value::U64(ts.0))
Ok(serde_value::Value::U64(ts.as_micros()))
}
dynamic_value::Value::List(list) => {
let mut output = Vec::new();
Expand Down
8 changes: 2 additions & 6 deletions lazer/sdk/rust/client/examples/subscribe_price_feeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ async fn main() -> anyhow::Result<()> {
delivery_format: DeliveryFormat::Json,
json_binary_encoding: JsonBinaryEncoding::Base64,
parsed: true,
channel: Channel::FixedRate(
FixedRate::from_ms(200).expect("unsupported update rate"),
),
channel: Channel::FixedRate(FixedRate::RATE_200_MS),
ignore_invalid_feed_ids: false,
})
.expect("invalid subscription params"),
Expand All @@ -66,9 +64,7 @@ async fn main() -> anyhow::Result<()> {
delivery_format: DeliveryFormat::Binary,
json_binary_encoding: JsonBinaryEncoding::Base64,
parsed: false,
channel: Channel::FixedRate(
FixedRate::from_ms(50).expect("unsupported update rate"),
),
channel: Channel::FixedRate(FixedRate::RATE_50_MS),
ignore_invalid_feed_ids: false,
})
.expect("invalid subscription params"),
Expand Down
2 changes: 2 additions & 0 deletions lazer/sdk/rust/protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ itertools = "0.13.0"
rust_decimal = "1.36.0"
protobuf = "3.7.2"
humantime-serde = "1.1.1"
mry = { version = "0.13.0", features = ["serde"], optional = true }
chrono = "0.4.41"

[dev-dependencies]
bincode = "1.3.3"
Expand Down
5 changes: 3 additions & 2 deletions lazer/sdk/rust/protocol/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize};

use crate::router::{
Channel, Format, JsonBinaryEncoding, JsonUpdate, PriceFeedId, PriceFeedProperty, TimestampUs,
use crate::{
router::{Channel, Format, JsonBinaryEncoding, JsonUpdate, PriceFeedId, PriceFeedProperty},
time::TimestampUs,
};

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
11 changes: 6 additions & 5 deletions lazer/sdk/rust/protocol/src/jrpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::router::{Channel, Price, PriceFeedId, Rate, TimestampUs};
use crate::router::{Channel, Price, PriceFeedId, Rate};
use crate::symbol_state::SymbolState;
use crate::time::TimestampUs;
use serde::{Deserialize, Serialize};
use std::time::Duration;

Expand Down Expand Up @@ -157,7 +158,7 @@ mod tests {
jsonrpc: JsonRpcVersion::V2,
params: PushUpdate(FeedUpdateParams {
feed_id: PriceFeedId(1),
source_timestamp: TimestampUs(124214124124),
source_timestamp: TimestampUs::from_micros(124214124124),
update: UpdateParams::PriceUpdate {
price: Price::from_integer(1234567890, 0).unwrap(),
best_bid_price: Some(Price::from_integer(1234567891, 0).unwrap()),
Expand Down Expand Up @@ -196,7 +197,7 @@ mod tests {
jsonrpc: JsonRpcVersion::V2,
params: PushUpdate(FeedUpdateParams {
feed_id: PriceFeedId(1),
source_timestamp: TimestampUs(124214124124),
source_timestamp: TimestampUs::from_micros(124214124124),
update: UpdateParams::PriceUpdate {
price: Price::from_integer(1234567890, 0).unwrap(),
best_bid_price: None,
Expand Down Expand Up @@ -236,7 +237,7 @@ mod tests {
jsonrpc: JsonRpcVersion::V2,
params: PushUpdate(FeedUpdateParams {
feed_id: PriceFeedId(1),
source_timestamp: TimestampUs(124214124124),
source_timestamp: TimestampUs::from_micros(124214124124),
update: UpdateParams::FundingRateUpdate {
price: Some(Price::from_integer(1234567890, 0).unwrap()),
rate: Rate::from_integer(1234567891, 0).unwrap(),
Expand Down Expand Up @@ -273,7 +274,7 @@ mod tests {
jsonrpc: JsonRpcVersion::V2,
params: PushUpdate(FeedUpdateParams {
feed_id: PriceFeedId(1),
source_timestamp: TimestampUs(124214124124),
source_timestamp: TimestampUs::from_micros(124214124124),
update: UpdateParams::FundingRateUpdate {
price: None,
rate: Rate::from_integer(1234567891, 0).unwrap(),
Expand Down
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 @@ -11,6 +11,7 @@ mod serde_price_as_i64;
mod serde_str;
pub mod subscription;
pub mod symbol_state;
pub mod time;

#[test]
fn magics_in_big_endian() {
Expand Down
15 changes: 9 additions & 6 deletions lazer/sdk/rust/protocol/src/payload.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Types representing binary encoding of signable payloads and signature envelopes.

use {
super::router::{PriceFeedId, PriceFeedProperty, TimestampUs},
crate::router::{ChannelId, Price, Rate},
super::router::{PriceFeedId, PriceFeedProperty},
crate::{
router::{ChannelId, Price, Rate},
time::TimestampUs,
},
anyhow::bail,
byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt, BE, LE},
serde::{Deserialize, Serialize},
Expand Down Expand Up @@ -103,7 +106,7 @@ impl PayloadData {

pub fn serialize<BO: ByteOrder>(&self, mut writer: impl Write) -> anyhow::Result<()> {
writer.write_u32::<BO>(PAYLOAD_FORMAT_MAGIC)?;
writer.write_u64::<BO>(self.timestamp_us.0)?;
writer.write_u64::<BO>(self.timestamp_us.as_micros())?;
writer.write_u8(self.channel_id.0)?;
writer.write_u8(self.feeds.len().try_into()?)?;
for feed in &self.feeds {
Expand Down Expand Up @@ -162,7 +165,7 @@ impl PayloadData {
if magic != PAYLOAD_FORMAT_MAGIC {
bail!("magic mismatch");
}
let timestamp_us = TimestampUs(reader.read_u64::<BO>()?);
let timestamp_us = TimestampUs::from_micros(reader.read_u64::<BO>()?);
let channel_id = ChannelId(reader.read_u8()?);
let num_feeds = reader.read_u8()?;
let mut feeds = Vec::with_capacity(num_feeds.into());
Expand Down Expand Up @@ -252,7 +255,7 @@ fn write_option_timestamp<BO: ByteOrder>(
match value {
Some(value) => {
writer.write_u8(1)?;
writer.write_u64::<BO>(value.0)
writer.write_u64::<BO>(value.as_micros())
}
None => {
writer.write_u8(0)?;
Expand All @@ -266,7 +269,7 @@ fn read_option_timestamp<BO: ByteOrder>(
) -> std::io::Result<Option<TimestampUs>> {
let present = reader.read_u8()? != 0;
if present {
Ok(Some(TimestampUs(reader.read_u64::<BO>()?)))
Ok(Some(TimestampUs::from_micros(reader.read_u64::<BO>()?)))
} else {
Ok(None)
}
Expand Down
19 changes: 10 additions & 9 deletions lazer/sdk/rust/protocol/src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//! eliminating WebSocket overhead.

use {
super::router::{Price, PriceFeedId, Rate, TimestampUs},
super::router::{Price, PriceFeedId, Rate},
crate::time::TimestampUs,
derive_more::derive::From,
serde::{Deserialize, Serialize},
};
Expand Down Expand Up @@ -101,8 +102,8 @@ fn price_feed_data_v1_serde() {

let expected = PriceFeedDataV1 {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
source_timestamp_us: TimestampUs::from_micros(2),
publisher_timestamp_us: TimestampUs::from_micros(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())),
Expand All @@ -123,8 +124,8 @@ fn price_feed_data_v1_serde() {
];
let expected2 = PriceFeedDataV1 {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
source_timestamp_us: TimestampUs::from_micros(2),
publisher_timestamp_us: TimestampUs::from_micros(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: None,
best_ask_price: None,
Expand All @@ -150,8 +151,8 @@ fn price_feed_data_v2_serde() {

let expected = PriceFeedDataV2 {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
source_timestamp_us: TimestampUs::from_micros(2),
publisher_timestamp_us: TimestampUs::from_micros(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())),
Expand All @@ -174,8 +175,8 @@ fn price_feed_data_v2_serde() {
];
let expected2 = PriceFeedDataV2 {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
source_timestamp_us: TimestampUs::from_micros(2),
publisher_timestamp_us: TimestampUs::from_micros(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: None,
best_ask_price: None,
Expand Down
Loading