Skip to content

Commit db98bef

Browse files
committed
Refactor the code according to reviews
1 parent ee70994 commit db98bef

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

pyth-sdk-solana/src/state.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use bytemuck::{
1212
PodCastError,
1313
Zeroable,
1414
};
15-
use pyth_sdk::PriceIdentifier;
15+
use pyth_sdk::{
16+
PriceIdentifier,
17+
UnixTimestamp,
18+
};
1619
use solana_program::pubkey::Pubkey;
1720
use std::mem::size_of;
1821

@@ -344,38 +347,38 @@ unsafe impl Pod for PriceAccount {
344347
}
345348

346349
impl PriceAccount {
350+
pub fn get_publish_time(&self) -> UnixTimestamp {
351+
match self.agg.status {
352+
PriceStatus::Trading => self.timestamp,
353+
_ => self.prev_timestamp,
354+
}
355+
}
356+
347357
pub fn to_price_feed(&self, price_key: &Pubkey) -> PriceFeed {
348358
let status = self.agg.status;
349359

350-
let price = if matches!(status, PriceStatus::Trading) {
351-
Price {
360+
let price = match status {
361+
PriceStatus::Trading => Price {
352362
conf: self.agg.conf,
353363
expo: self.expo,
354364
price: self.agg.price,
355-
publish_time: self.timestamp,
356-
}
357-
} else {
358-
Price {
365+
publish_time: self.get_publish_time(),
366+
},
367+
_ => Price {
359368
conf: self.prev_conf,
360369
expo: self.expo,
361370
price: self.prev_price,
362-
publish_time: self.prev_timestamp,
363-
}
371+
publish_time: self.get_publish_time(),
372+
},
364373
};
365374

366-
let mut ema_price = Price {
375+
let ema_price = Price {
367376
conf: self.ema_conf.val as u64,
368377
expo: self.expo,
369378
price: self.ema_price.val,
370-
publish_time: self.timestamp,
379+
publish_time: self.get_publish_time(),
371380
};
372381

373-
// Ema price only gets updated if status is Trading. So it's update timestamp will be
374-
// prev_timestamp when the status is not Trading.
375-
if !matches!(status, PriceStatus::Trading) {
376-
ema_price.publish_time = self.prev_timestamp;
377-
}
378-
379382
PriceFeed::new(PriceIdentifier::new(price_key.to_bytes()), price, ema_price)
380383
}
381384
}

0 commit comments

Comments
 (0)