|
@@ -12,9 +12,9 @@ use std::io::Read;
|
|
|
use std::iter::Iterator;
|
|
use std::iter::Iterator;
|
|
|
use std::mem;
|
|
use std::mem;
|
|
|
|
|
|
|
|
-use pyth_client::{
|
|
|
|
|
|
|
+use pyth_sdk_solana::state::{
|
|
|
CorpAction,
|
|
CorpAction,
|
|
|
- Ema,
|
|
|
|
|
|
|
+ Rational,
|
|
|
PriceStatus,
|
|
PriceStatus,
|
|
|
PriceType,
|
|
PriceType,
|
|
|
};
|
|
};
|
|
@@ -68,8 +68,8 @@ pub struct PriceAttestation {
|
|
|
pub price_type: PriceType,
|
|
pub price_type: PriceType,
|
|
|
pub price: i64,
|
|
pub price: i64,
|
|
|
pub expo: i32,
|
|
pub expo: i32,
|
|
|
- pub twap: Ema,
|
|
|
|
|
- pub twac: Ema,
|
|
|
|
|
|
|
+ pub twap: Rational,
|
|
|
|
|
+ pub twac: Rational,
|
|
|
pub confidence_interval: u64,
|
|
pub confidence_interval: u64,
|
|
|
pub status: PriceStatus,
|
|
pub status: PriceStatus,
|
|
|
pub corp_act: CorpAction,
|
|
pub corp_act: CorpAction,
|
|
@@ -199,21 +199,21 @@ impl BatchPriceAttestation {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-pub fn serialize_ema(ema: &Ema) -> Vec<u8> {
|
|
|
|
|
|
|
+pub fn serialize_rational(rational: &Rational) -> Vec<u8> {
|
|
|
let mut v = vec![];
|
|
let mut v = vec![];
|
|
|
// val
|
|
// val
|
|
|
- v.extend(&ema.val.to_be_bytes()[..]);
|
|
|
|
|
|
|
+ v.extend(&rational.val.to_be_bytes()[..]);
|
|
|
|
|
|
|
|
// numer
|
|
// numer
|
|
|
- v.extend(&ema.numer.to_be_bytes()[..]);
|
|
|
|
|
|
|
+ v.extend(&rational.numer.to_be_bytes()[..]);
|
|
|
|
|
|
|
|
// denom
|
|
// denom
|
|
|
- v.extend(&ema.denom.to_be_bytes()[..]);
|
|
|
|
|
|
|
+ v.extend(&rational.denom.to_be_bytes()[..]);
|
|
|
|
|
|
|
|
v
|
|
v
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-pub fn deserialize_ema(mut bytes: impl Read) -> Result<Ema, ErrBox> {
|
|
|
|
|
|
|
+pub fn deserialize_rational(mut bytes: impl Read) -> Result<Rational, ErrBox> {
|
|
|
let mut val_vec = vec![0u8; mem::size_of::<i64>()];
|
|
let mut val_vec = vec![0u8; mem::size_of::<i64>()];
|
|
|
bytes.read_exact(val_vec.as_mut_slice())?;
|
|
bytes.read_exact(val_vec.as_mut_slice())?;
|
|
|
let val = i64::from_be_bytes(val_vec.as_slice().try_into()?);
|
|
let val = i64::from_be_bytes(val_vec.as_slice().try_into()?);
|
|
@@ -226,7 +226,7 @@ pub fn deserialize_ema(mut bytes: impl Read) -> Result<Ema, ErrBox> {
|
|
|
bytes.read_exact(denom_vec.as_mut_slice())?;
|
|
bytes.read_exact(denom_vec.as_mut_slice())?;
|
|
|
let denom = i64::from_be_bytes(denom_vec.as_slice().try_into()?);
|
|
let denom = i64::from_be_bytes(denom_vec.as_slice().try_into()?);
|
|
|
|
|
|
|
|
- Ok(Ema { val, numer, denom })
|
|
|
|
|
|
|
+ Ok(Rational { val, numer, denom })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// On-chain data types
|
|
// On-chain data types
|
|
@@ -237,15 +237,15 @@ impl PriceAttestation {
|
|
|
timestamp: UnixTimestamp,
|
|
timestamp: UnixTimestamp,
|
|
|
value: &[u8],
|
|
value: &[u8],
|
|
|
) -> Result<Self, ErrBox> {
|
|
) -> Result<Self, ErrBox> {
|
|
|
- let price = pyth_client::load_price(value)?;
|
|
|
|
|
|
|
+ let price = pyth_sdk_solana::state::load_price_account(value)?;
|
|
|
|
|
|
|
|
Ok(PriceAttestation {
|
|
Ok(PriceAttestation {
|
|
|
product_id: Pubkey::new(&price.prod.val[..]),
|
|
product_id: Pubkey::new(&price.prod.val[..]),
|
|
|
price_id,
|
|
price_id,
|
|
|
price_type: price.ptype,
|
|
price_type: price.ptype,
|
|
|
price: price.agg.price,
|
|
price: price.agg.price,
|
|
|
- twap: price.twap,
|
|
|
|
|
- twac: price.twac,
|
|
|
|
|
|
|
+ twap: price.ema_price,
|
|
|
|
|
+ twac: price.ema_conf,
|
|
|
expo: price.expo,
|
|
expo: price.expo,
|
|
|
confidence_interval: price.agg.conf,
|
|
confidence_interval: price.agg.conf,
|
|
|
status: price.agg.status,
|
|
status: price.agg.status,
|
|
@@ -297,10 +297,10 @@ impl PriceAttestation {
|
|
|
buf.extend_from_slice(&expo.to_be_bytes()[..]);
|
|
buf.extend_from_slice(&expo.to_be_bytes()[..]);
|
|
|
|
|
|
|
|
// twap
|
|
// twap
|
|
|
- buf.append(&mut serialize_ema(&twap));
|
|
|
|
|
|
|
+ buf.append(&mut serialize_rational(&twap));
|
|
|
|
|
|
|
|
// twac
|
|
// twac
|
|
|
- buf.append(&mut serialize_ema(&twac));
|
|
|
|
|
|
|
+ buf.append(&mut serialize_rational(&twac));
|
|
|
|
|
|
|
|
// confidence_interval
|
|
// confidence_interval
|
|
|
buf.extend_from_slice(&confidence_interval.to_be_bytes()[..]);
|
|
buf.extend_from_slice(&confidence_interval.to_be_bytes()[..]);
|
|
@@ -379,8 +379,8 @@ impl PriceAttestation {
|
|
|
bytes.read_exact(expo_vec.as_mut_slice())?;
|
|
bytes.read_exact(expo_vec.as_mut_slice())?;
|
|
|
let expo = i32::from_be_bytes(expo_vec.as_slice().try_into()?);
|
|
let expo = i32::from_be_bytes(expo_vec.as_slice().try_into()?);
|
|
|
|
|
|
|
|
- let twap = deserialize_ema(&mut bytes)?;
|
|
|
|
|
- let twac = deserialize_ema(&mut bytes)?;
|
|
|
|
|
|
|
+ let twap = deserialize_rational(&mut bytes)?;
|
|
|
|
|
+ let twac = deserialize_rational(&mut bytes)?;
|
|
|
|
|
|
|
|
println!("twac OK");
|
|
println!("twac OK");
|
|
|
let mut confidence_interval_vec = vec![0u8; mem::size_of::<u64>()];
|
|
let mut confidence_interval_vec = vec![0u8; mem::size_of::<u64>()];
|
|
@@ -432,8 +432,8 @@ impl PriceAttestation {
|
|
|
#[cfg(test)]
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
mod tests {
|
|
|
use super::*;
|
|
use super::*;
|
|
|
- use pyth_client::{
|
|
|
|
|
- Ema,
|
|
|
|
|
|
|
+ use pyth_sdk_solana::state::{
|
|
|
|
|
+ Rational,
|
|
|
PriceStatus,
|
|
PriceStatus,
|
|
|
PriceType,
|
|
PriceType,
|
|
|
};
|
|
};
|
|
@@ -446,12 +446,12 @@ mod tests {
|
|
|
price_id: Pubkey::new_from_array(price_id_bytes),
|
|
price_id: Pubkey::new_from_array(price_id_bytes),
|
|
|
price: (0xdeadbeefdeadbabe as u64) as i64,
|
|
price: (0xdeadbeefdeadbabe as u64) as i64,
|
|
|
price_type: PriceType::Price,
|
|
price_type: PriceType::Price,
|
|
|
- twap: Ema {
|
|
|
|
|
|
|
+ twap: Rational {
|
|
|
val: -42,
|
|
val: -42,
|
|
|
numer: 15,
|
|
numer: 15,
|
|
|
denom: 37,
|
|
denom: 37,
|
|
|
},
|
|
},
|
|
|
- twac: Ema {
|
|
|
|
|
|
|
+ twac: Rational {
|
|
|
val: 42,
|
|
val: 42,
|
|
|
numer: 1111,
|
|
numer: 1111,
|
|
|
denom: 2222,
|
|
denom: 2222,
|