Skip to content

Commit 48690ec

Browse files
feat(pyth-lazer-protocol): remove TMP_EXPONENT (#2221)
* feat(pyth-lazer-protocol): remove TMP_EXPONENT * fix
1 parent 1b43f81 commit 48690ec

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

lazer/Cargo.lock

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

lazer/contracts/solana/programs/pyth-lazer-solana-contract/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ no-log-ix-name = []
1919
idl-build = ["anchor-lang/idl-build"]
2020

2121
[dependencies]
22-
pyth-lazer-protocol = { version = "0.1.2", path = "../../../../sdk/rust/protocol" }
22+
pyth-lazer-protocol = "0.1.2"
2323

2424
anchor-lang = "0.30.1"
2525
bytemuck = "1.20.0"

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: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use {
88
serde::{de::Error, Deserialize, Serialize},
99
std::{
1010
num::NonZeroI64,
11-
ops::{Add, Deref, DerefMut, Div, Mul, Sub},
11+
ops::{Add, Deref, DerefMut, Div, Sub},
1212
time::{SystemTime, UNIX_EPOCH},
1313
},
1414
};
@@ -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)