diff --git a/lazer/Cargo.lock b/lazer/Cargo.lock index db00451155..3cdb3a4c03 100644 --- a/lazer/Cargo.lock +++ b/lazer/Cargo.lock @@ -3177,6 +3177,20 @@ dependencies = [ [[package]] name = "pyth-lazer-protocol" version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "694b671c6f258fdd8e5c28a5ad8cf2ce20e416417c9a5619c0d00397f400e2f8" +dependencies = [ + "anyhow", + "byteorder", + "derive_more", + "itertools 0.13.0", + "rust_decimal", + "serde", +] + +[[package]] +name = "pyth-lazer-protocol" +version = "0.2.0" dependencies = [ "anyhow", "bincode", @@ -3195,7 +3209,7 @@ dependencies = [ "bytemuck", "byteorder", "hex", - "pyth-lazer-protocol", + "pyth-lazer-protocol 0.1.3", "solana-program-test", "solana-sdk", "thiserror 2.0.3", diff --git a/lazer/contracts/solana/programs/pyth-lazer-solana-contract/Cargo.toml b/lazer/contracts/solana/programs/pyth-lazer-solana-contract/Cargo.toml index f4c43cfa95..642016757f 100644 --- a/lazer/contracts/solana/programs/pyth-lazer-solana-contract/Cargo.toml +++ b/lazer/contracts/solana/programs/pyth-lazer-solana-contract/Cargo.toml @@ -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" anchor-lang = "0.30.1" bytemuck = "1.20.0" diff --git a/lazer/sdk/rust/protocol/Cargo.toml b/lazer/sdk/rust/protocol/Cargo.toml index 363ba162b7..c5b87095d6 100644 --- a/lazer/sdk/rust/protocol/Cargo.toml +++ b/lazer/sdk/rust/protocol/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pyth-lazer-protocol" -version = "0.1.3" +version = "0.2.0" edition = "2021" description = "Pyth Lazer SDK - protocol types." license = "Apache-2.0" diff --git a/lazer/sdk/rust/protocol/src/router.rs b/lazer/sdk/rust/protocol/src/router.rs index 6ed1c83676..f6559b0f28 100644 --- a/lazer/sdk/rust/protocol/src/router.rs +++ b/lazer/sdk/rust/protocol/src/router.rs @@ -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}, }, }; @@ -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 { let coef = 10i64.checked_pow(exponent).context("overflow")?; let value = value.checked_mul(coef).context("overflow")?; @@ -77,13 +74,20 @@ impl Price { pub fn into_inner(self) -> NonZeroI64 { self.0 } -} -impl TryInto for Price { - type Error = anyhow::Error; + pub fn to_f64(self, exponent: u32) -> anyhow::Result { + 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 { + let left_value = i128::from(self.0.get()); + let right_value = i128::from(rhs.0.get()); - fn try_into(self) -> Result { - 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) } } @@ -121,21 +125,6 @@ impl Div for Price { } } -impl Mul for Price { - type Output = Option; - 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 {