pyth_sui_sdk/pyth_sdk.rs
1use af_utilities::IFixed;
2use pyth_sdk::PriceIdentifier;
3
4use crate::price_info::PriceInfoObject;
5
6impl PriceInfoObject {
7 /// Get the off chain price identifier from the Pyth sdk
8 pub fn pyth_price_id(&self) -> PriceIdentifier {
9 PriceIdentifier::new(
10 self.price_info
11 .price_feed
12 .price_identifier
13 .bytes
14 .to_vec()
15 .try_into()
16 .expect("Validated lenght onchain"),
17 )
18 }
19
20 pub fn get_pyth_price(&self) -> Result<IFixed, Error> {
21 let pyth_price = &self.price_info.price_feed.price;
22 let price = if pyth_price.price.negative {
23 return Err(Error::NegativePythPrice(pyth_price.price.magnitude as i64));
24 } else if pyth_price.expo.negative {
25 IFixed::from(pyth_price.price.magnitude)
26 / IFixed::from(u64::pow(10, pyth_price.expo.magnitude as u32))
27 } else {
28 IFixed::from(pyth_price.price.magnitude)
29 * IFixed::from(u64::pow(10, pyth_price.expo.magnitude as u32))
30 };
31
32 Ok(price)
33 }
34
35 pub const fn get_timestamp_ms(&self) -> u64 {
36 self.price_info.price_feed.price.timestamp * 1000
37 }
38}
39
40#[derive(Debug, thiserror::Error)]
41#[non_exhaustive]
42pub enum Error {
43 #[error("Pyth price is negative: -{0}")]
44 NegativePythPrice(i64),
45}