Skip to content

Commit 433ebdf

Browse files
committed
feat(lazer): improve time types (wip)
1 parent ac204f8 commit 433ebdf

File tree

13 files changed

+561
-103
lines changed

13 files changed

+561
-103
lines changed

Cargo.lock

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

lazer/contracts/solana/Cargo.lock

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

lazer/publisher_sdk/rust/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::{bail, ensure, Context};
77
use humantime::format_duration;
88
use protobuf::dynamic_value::{dynamic_value, DynamicValue};
99
use pyth_lazer_protocol::jrpc::{FeedUpdateParams, UpdateParams};
10-
use pyth_lazer_protocol::router::TimestampUs;
10+
use pyth_lazer_protocol::time::TimestampUs;
1111

1212
pub mod transaction_envelope {
1313
pub use crate::protobuf::transaction_envelope::*;
@@ -141,7 +141,7 @@ impl TryFrom<DynamicValue> for serde_value::Value {
141141
}
142142
dynamic_value::Value::TimestampValue(ts) => {
143143
let ts = TimestampUs::try_from(&ts)?;
144-
Ok(serde_value::Value::U64(ts.0))
144+
Ok(serde_value::Value::U64(ts.as_micros()))
145145
}
146146
dynamic_value::Value::List(list) => {
147147
let mut output = Vec::new();

lazer/sdk/rust/client/examples/subscribe_price_feeds.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ async fn main() -> anyhow::Result<()> {
4444
delivery_format: DeliveryFormat::Json,
4545
json_binary_encoding: JsonBinaryEncoding::Base64,
4646
parsed: true,
47-
channel: Channel::FixedRate(
48-
FixedRate::from_ms(200).expect("unsupported update rate"),
49-
),
47+
channel: Channel::FixedRate(FixedRate::RATE_200_MS),
5048
ignore_invalid_feed_ids: false,
5149
})
5250
.expect("invalid subscription params"),
@@ -66,9 +64,7 @@ async fn main() -> anyhow::Result<()> {
6664
delivery_format: DeliveryFormat::Binary,
6765
json_binary_encoding: JsonBinaryEncoding::Base64,
6866
parsed: false,
69-
channel: Channel::FixedRate(
70-
FixedRate::from_ms(50).expect("unsupported update rate"),
71-
),
67+
channel: Channel::FixedRate(FixedRate::RATE_50_MS),
7268
ignore_invalid_feed_ids: false,
7369
})
7470
.expect("invalid subscription params"),

lazer/sdk/rust/protocol/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ itertools = "0.13.0"
1616
rust_decimal = "1.36.0"
1717
protobuf = "3.7.2"
1818
humantime-serde = "1.1.1"
19+
mry = { version = "0.13.0", features = ["serde"], optional = true }
20+
chrono = "0.4.41"
21+
22+
# tmp
23+
cadd = { path = "../../../../../../p/cadd" }
1924

2025
[dev-dependencies]
2126
bincode = "1.3.3"

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::router::{
4-
Channel, Format, JsonBinaryEncoding, JsonUpdate, PriceFeedId, PriceFeedProperty, TimestampUs,
3+
use crate::{
4+
router::{Channel, Format, JsonBinaryEncoding, JsonUpdate, PriceFeedId, PriceFeedProperty},
5+
time::TimestampUs,
56
};
67

78
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::router::{Channel, Price, PriceFeedId, Rate, TimestampUs};
1+
use crate::router::{Channel, Price, PriceFeedId, Rate};
22
use crate::symbol_state::SymbolState;
3+
use crate::time::TimestampUs;
34
use serde::{Deserialize, Serialize};
45
use std::time::Duration;
56

@@ -157,7 +158,7 @@ mod tests {
157158
jsonrpc: JsonRpcVersion::V2,
158159
params: PushUpdate(FeedUpdateParams {
159160
feed_id: PriceFeedId(1),
160-
source_timestamp: TimestampUs(124214124124),
161+
source_timestamp: TimestampUs::from_micros(124214124124),
161162
update: UpdateParams::PriceUpdate {
162163
price: Price::from_integer(1234567890, 0).unwrap(),
163164
best_bid_price: Some(Price::from_integer(1234567891, 0).unwrap()),
@@ -196,7 +197,7 @@ mod tests {
196197
jsonrpc: JsonRpcVersion::V2,
197198
params: PushUpdate(FeedUpdateParams {
198199
feed_id: PriceFeedId(1),
199-
source_timestamp: TimestampUs(124214124124),
200+
source_timestamp: TimestampUs::from_micros(124214124124),
200201
update: UpdateParams::PriceUpdate {
201202
price: Price::from_integer(1234567890, 0).unwrap(),
202203
best_bid_price: None,
@@ -236,7 +237,7 @@ mod tests {
236237
jsonrpc: JsonRpcVersion::V2,
237238
params: PushUpdate(FeedUpdateParams {
238239
feed_id: PriceFeedId(1),
239-
source_timestamp: TimestampUs(124214124124),
240+
source_timestamp: TimestampUs::from_micros(124214124124),
240241
update: UpdateParams::FundingRateUpdate {
241242
price: Some(Price::from_integer(1234567890, 0).unwrap()),
242243
rate: Rate::from_integer(1234567891, 0).unwrap(),
@@ -273,7 +274,7 @@ mod tests {
273274
jsonrpc: JsonRpcVersion::V2,
274275
params: PushUpdate(FeedUpdateParams {
275276
feed_id: PriceFeedId(1),
276-
source_timestamp: TimestampUs(124214124124),
277+
source_timestamp: TimestampUs::from_micros(124214124124),
277278
update: UpdateParams::FundingRateUpdate {
278279
price: None,
279280
rate: Rate::from_integer(1234567891, 0).unwrap(),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod serde_price_as_i64;
1111
mod serde_str;
1212
pub mod subscription;
1313
pub mod symbol_state;
14+
pub mod time;
1415

1516
#[test]
1617
fn magics_in_big_endian() {

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! Types representing binary encoding of signable payloads and signature envelopes.
22
33
use {
4-
super::router::{PriceFeedId, PriceFeedProperty, TimestampUs},
5-
crate::router::{ChannelId, Price, Rate},
4+
super::router::{PriceFeedId, PriceFeedProperty},
5+
crate::{
6+
router::{ChannelId, Price, Rate},
7+
time::TimestampUs,
8+
},
69
anyhow::bail,
710
byteorder::{ByteOrder, ReadBytesExt, WriteBytesExt, BE, LE},
811
serde::{Deserialize, Serialize},
@@ -103,7 +106,7 @@ impl PayloadData {
103106

104107
pub fn serialize<BO: ByteOrder>(&self, mut writer: impl Write) -> anyhow::Result<()> {
105108
writer.write_u32::<BO>(PAYLOAD_FORMAT_MAGIC)?;
106-
writer.write_u64::<BO>(self.timestamp_us.0)?;
109+
writer.write_u64::<BO>(self.timestamp_us.as_micros())?;
107110
writer.write_u8(self.channel_id.0)?;
108111
writer.write_u8(self.feeds.len().try_into()?)?;
109112
for feed in &self.feeds {
@@ -162,7 +165,7 @@ impl PayloadData {
162165
if magic != PAYLOAD_FORMAT_MAGIC {
163166
bail!("magic mismatch");
164167
}
165-
let timestamp_us = TimestampUs(reader.read_u64::<BO>()?);
168+
let timestamp_us = TimestampUs::from_micros(reader.read_u64::<BO>()?);
166169
let channel_id = ChannelId(reader.read_u8()?);
167170
let num_feeds = reader.read_u8()?;
168171
let mut feeds = Vec::with_capacity(num_feeds.into());
@@ -252,7 +255,7 @@ fn write_option_timestamp<BO: ByteOrder>(
252255
match value {
253256
Some(value) => {
254257
writer.write_u8(1)?;
255-
writer.write_u64::<BO>(value.0)
258+
writer.write_u64::<BO>(value.as_micros())
256259
}
257260
None => {
258261
writer.write_u8(0)?;
@@ -266,7 +269,7 @@ fn read_option_timestamp<BO: ByteOrder>(
266269
) -> std::io::Result<Option<TimestampUs>> {
267270
let present = reader.read_u8()? != 0;
268271
if present {
269-
Ok(Some(TimestampUs(reader.read_u64::<BO>()?)))
272+
Ok(Some(TimestampUs::from_micros(reader.read_u64::<BO>()?)))
270273
} else {
271274
Ok(None)
272275
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
//! eliminating WebSocket overhead.
44
55
use {
6-
super::router::{Price, PriceFeedId, Rate, TimestampUs},
6+
super::router::{Price, PriceFeedId, Rate},
7+
crate::time::TimestampUs,
78
derive_more::derive::From,
89
serde::{Deserialize, Serialize},
910
};
@@ -101,8 +102,8 @@ fn price_feed_data_v1_serde() {
101102

102103
let expected = PriceFeedDataV1 {
103104
price_feed_id: PriceFeedId(1),
104-
source_timestamp_us: TimestampUs(2),
105-
publisher_timestamp_us: TimestampUs(3),
105+
source_timestamp_us: TimestampUs::from_micros(2),
106+
publisher_timestamp_us: TimestampUs::from_micros(3),
106107
price: Some(Price(4.try_into().unwrap())),
107108
best_bid_price: Some(Price(5.try_into().unwrap())),
108109
best_ask_price: Some(Price((2 * 256 + 6).try_into().unwrap())),
@@ -123,8 +124,8 @@ fn price_feed_data_v1_serde() {
123124
];
124125
let expected2 = PriceFeedDataV1 {
125126
price_feed_id: PriceFeedId(1),
126-
source_timestamp_us: TimestampUs(2),
127-
publisher_timestamp_us: TimestampUs(3),
127+
source_timestamp_us: TimestampUs::from_micros(2),
128+
publisher_timestamp_us: TimestampUs::from_micros(3),
128129
price: Some(Price(4.try_into().unwrap())),
129130
best_bid_price: None,
130131
best_ask_price: None,
@@ -150,8 +151,8 @@ fn price_feed_data_v2_serde() {
150151

151152
let expected = PriceFeedDataV2 {
152153
price_feed_id: PriceFeedId(1),
153-
source_timestamp_us: TimestampUs(2),
154-
publisher_timestamp_us: TimestampUs(3),
154+
source_timestamp_us: TimestampUs::from_micros(2),
155+
publisher_timestamp_us: TimestampUs::from_micros(3),
155156
price: Some(Price(4.try_into().unwrap())),
156157
best_bid_price: Some(Price(5.try_into().unwrap())),
157158
best_ask_price: Some(Price((2 * 256 + 6).try_into().unwrap())),
@@ -174,8 +175,8 @@ fn price_feed_data_v2_serde() {
174175
];
175176
let expected2 = PriceFeedDataV2 {
176177
price_feed_id: PriceFeedId(1),
177-
source_timestamp_us: TimestampUs(2),
178-
publisher_timestamp_us: TimestampUs(3),
178+
source_timestamp_us: TimestampUs::from_micros(2),
179+
publisher_timestamp_us: TimestampUs::from_micros(3),
179180
price: Some(Price(4.try_into().unwrap())),
180181
best_bid_price: None,
181182
best_ask_price: None,

0 commit comments

Comments
 (0)