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
16 changes: 15 additions & 1 deletion lazer/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
pyth-lazer-protocol = { version = "0.1.2", path = "../../../../sdk/rust/protocol" }
pyth-lazer-protocol = "0.1.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we only use 0.1.2 for the contracts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think at the time of the implementation this was the latest.


anchor-lang = "0.30.1"
bytemuck = "1.20.0"
Expand Down
2 changes: 1 addition & 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.3"
version = "0.2.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When should we be doing a minor version upgrade like here? Not sure why we are going to 2.0 instead of 1.4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the traits is considered a breaking change.

edition = "2021"
description = "Pyth Lazer SDK - protocol types."
license = "Apache-2.0"
Expand Down
37 changes: 13 additions & 24 deletions lazer/sdk/rust/protocol/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
serde::{de::Error, Deserialize, Serialize},
std::{
num::NonZeroI64,
ops::{Add, Deref, DerefMut, Div, Mul, Sub},
ops::{Add, Deref, DerefMut, Div, Sub},
time::{SystemTime, UNIX_EPOCH},
},
};
Expand Down Expand Up @@ -46,9 +46,6 @@ impl TimestampUs {
pub struct Price(pub NonZeroI64);

impl Price {
// TODO: define exponent in price feed metadata instead
pub const TMP_EXPONENT: u32 = 8;

pub fn from_integer(value: i64, exponent: u32) -> anyhow::Result<Price> {
let coef = 10i64.checked_pow(exponent).context("overflow")?;
let value = value.checked_mul(coef).context("overflow")?;
Expand Down Expand Up @@ -77,13 +74,20 @@ impl Price {
pub fn into_inner(self) -> NonZeroI64 {
self.0
}
}

impl TryInto<f64> for Price {
type Error = anyhow::Error;
pub fn to_f64(self, exponent: u32) -> anyhow::Result<f64> {
Ok(self.0.get() as f64 / 10i64.checked_pow(exponent).context("overflow")? as f64)
}

pub fn mul(self, rhs: Price, rhs_exponent: u32) -> anyhow::Result<Price> {
let left_value = i128::from(self.0.get());
let right_value = i128::from(rhs.0.get());

fn try_into(self) -> Result<f64, Self::Error> {
Ok(self.0.get() as f64 / 10i64.checked_pow(Self::TMP_EXPONENT).context("overflow")? as f64)
let value = left_value * right_value / 10i128.pow(rhs_exponent);
let value = value.try_into()?;
NonZeroI64::new(value)
.context("zero price is unsupported")
.map(Self)
Comment on lines +78 to +90
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason you removed the traits?

Copy link
Contributor Author

@keyvankhademi keyvankhademi Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the need to pass an exponent to the mul and f64 cast functionalities, the traits are no longer suitable. So it has been changed to functions.

}
}

Expand Down Expand Up @@ -121,21 +125,6 @@ impl Div<i64> for Price {
}
}

impl Mul<Price> for Price {
type Output = Option<Price>;
fn mul(self, rhs: Price) -> Self::Output {
let left_value = i128::from(self.0.get());
let right_value = i128::from(rhs.0.get());

let value = left_value * right_value / 10i128.pow(Price::TMP_EXPONENT);
let value = match value.try_into() {
Ok(value) => value,
Err(_) => return None,
};
NonZeroI64::new(value).map(Self)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PriceFeedProperty {
Expand Down
Loading