Skip to content

Commit f72080c

Browse files
committed
add impl for timestampus
1 parent 5285283 commit f72080c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lazer/Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lazer/sdk/rust/protocol/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-lazer-protocol"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
edition = "2021"
55
description = "Pyth Lazer SDK - protocol types."
66
license = "Apache-2.0"
@@ -15,6 +15,7 @@ derive_more = { version = "1.0.0", features = ["from"] }
1515
itertools = "0.13.0"
1616
rust_decimal = "1.36.0"
1717
base64 = "0.22.1"
18+
protobuf = "3.7.2"
1819

1920
[dev-dependencies]
2021
bincode = "1.3.3"

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use {
44
crate::payload::AggregatedPriceFeedData,
55
anyhow::{bail, Context},
66
itertools::Itertools,
7+
protobuf::{well_known_types::timestamp::Timestamp, MessageField},
78
rust_decimal::{prelude::FromPrimitive, Decimal},
89
serde::{de::Error, Deserialize, Serialize},
910
std::{
@@ -26,6 +27,20 @@ pub struct ChannelId(pub u8);
2627
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
2728
pub struct TimestampUs(pub u64);
2829

30+
impl TryFrom<&MessageField<Timestamp>> for TimestampUs {
31+
type Error = anyhow::Error;
32+
33+
fn try_from(timestamp_field: &MessageField<Timestamp>) -> anyhow::Result<Self> {
34+
if let Some(timestamp) = timestamp_field.clone().into_option() {
35+
let seconds_in_micros: u64 = (timestamp.seconds * 1_000_000).try_into()?;
36+
let nanos_in_micros: u64 = (timestamp.nanos / 1_000).try_into()?;
37+
Ok(TimestampUs(seconds_in_micros + nanos_in_micros))
38+
} else {
39+
bail!("timestamp field was empty")
40+
}
41+
}
42+
}
43+
2944
impl TimestampUs {
3045
pub fn now() -> Self {
3146
let value = SystemTime::now()

0 commit comments

Comments
 (0)