Skip to content

Commit 800bff0

Browse files
committed
feat(pyth-lazer-protocol): remove TMP_EXPONENT
1 parent 1b43f81 commit 800bff0

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

lazer/sdk/rust/protocol/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-lazer-protocol"
3-
version = "0.1.3"
3+
version = "0.2.0"
44
edition = "2021"
55
description = "Pyth Lazer SDK - protocol types."
66
license = "Apache-2.0"

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ impl TimestampUs {
4646
pub struct Price(pub NonZeroI64);
4747

4848
impl Price {
49-
// TODO: define exponent in price feed metadata instead
50-
pub const TMP_EXPONENT: u32 = 8;
51-
5249
pub fn from_integer(value: i64, exponent: u32) -> anyhow::Result<Price> {
5350
let coef = 10i64.checked_pow(exponent).context("overflow")?;
5451
let value = value.checked_mul(coef).context("overflow")?;
@@ -77,13 +74,20 @@ impl Price {
7774
pub fn into_inner(self) -> NonZeroI64 {
7875
self.0
7976
}
80-
}
8177

82-
impl TryInto<f64> for Price {
83-
type Error = anyhow::Error;
78+
pub fn to_f64(self, exponent: u32) -> anyhow::Result<f64> {
79+
Ok(self.0.get() as f64 / 10i64.checked_pow(exponent).context("overflow")? as f64)
80+
}
81+
82+
pub fn mul(self, rhs: Price, rhs_exponent: u32) -> anyhow::Result<Price> {
83+
let left_value = i128::from(self.0.get());
84+
let right_value = i128::from(rhs.0.get());
8485

85-
fn try_into(self) -> Result<f64, Self::Error> {
86-
Ok(self.0.get() as f64 / 10i64.checked_pow(Self::TMP_EXPONENT).context("overflow")? as f64)
86+
let value = left_value * right_value / 10i128.pow(rhs_exponent);
87+
let value = value.try_into()?;
88+
NonZeroI64::new(value)
89+
.context("zero price is unsupported")
90+
.map(Self)
8791
}
8892
}
8993

@@ -121,21 +125,6 @@ impl Div<i64> for Price {
121125
}
122126
}
123127

124-
impl Mul<Price> for Price {
125-
type Output = Option<Price>;
126-
fn mul(self, rhs: Price) -> Self::Output {
127-
let left_value = i128::from(self.0.get());
128-
let right_value = i128::from(rhs.0.get());
129-
130-
let value = left_value * right_value / 10i128.pow(Price::TMP_EXPONENT);
131-
let value = match value.try_into() {
132-
Ok(value) => value,
133-
Err(_) => return None,
134-
};
135-
NonZeroI64::new(value).map(Self)
136-
}
137-
}
138-
139128
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
140129
#[serde(rename_all = "camelCase")]
141130
pub enum PriceFeedProperty {

0 commit comments

Comments
 (0)