Skip to content

Commit fce2f10

Browse files
committed
finished query price feeds function
1 parent 9bd142d commit fce2f10

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

target_chains/stylus/contracts/pyth-receiver/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum PythReceiverError {
1919
TooManyUpdates,
2020
PriceFeedNotFoundWithinRange,
2121
NoFreshUpdate,
22+
PriceFeedNotFound,
2223
}
2324

2425
impl core::fmt::Debug for PythReceiverError {
@@ -47,6 +48,7 @@ impl From<PythReceiverError> for Vec<u8> {
4748
PythReceiverError::TooManyUpdates => 15,
4849
PythReceiverError::PriceFeedNotFoundWithinRange => 16,
4950
PythReceiverError::NoFreshUpdate => 17,
51+
PythReceiverError::PriceFeedNotFound => 18,
5052
}]
5153
}
5254
}

target_chains/stylus/contracts/pyth-receiver/src/lib.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod test_data;
1515
#[cfg(test)]
1616
use mock_instant::global::MockClock;
1717

18-
use alloc::{collections::BTreeMap, vec::Vec};
18+
use alloc::vec::Vec;
1919
use stylus_sdk::{
2020
alloy_primitives::{Address, FixedBytes, I32, I64, U16, U256, U32, U64},
2121
call::Call,
@@ -111,6 +111,26 @@ impl PythReceiver {
111111
}
112112
}
113113

114+
pub fn query_price_feed(&self, id: [u8; 32]) -> Result<PriceFeedReturn, PythReceiverError> {
115+
let id_fb = FixedBytes::<32>::from(id);
116+
117+
let price_info = self.latest_price_info.get(id_fb);
118+
119+
if price_info.publish_time.get() == U64::ZERO {
120+
return Err(PythReceiverError::PriceUnavailable);
121+
}
122+
123+
Ok((
124+
id_fb,
125+
price_info.publish_time.get(),
126+
price_info.expo.get(),
127+
price_info.price.get(),
128+
price_info.conf.get(),
129+
price_info.ema_price.get(),
130+
price_info.ema_conf.get(),
131+
))
132+
}
133+
114134
pub fn get_price_unsafe(&self, id: [u8; 32]) -> Result<PriceReturn, PythReceiverError> {
115135
let id_fb = FixedBytes::<32>::from(id);
116136

@@ -370,7 +390,7 @@ impl PythReceiver {
370390
let accumulator_update = AccumulatorUpdateData::try_from_slice(&update_data_array)
371391
.map_err(|_| PythReceiverError::InvalidAccumulatorMessage)?;
372392

373-
let mut price_feeds: BTreeMap<[u8; 32], PriceFeedReturn> = BTreeMap::new();
393+
let mut price_feeds = Vec::new();
374394

375395
match accumulator_update.proof {
376396
Proof::WormholeMerkle { vaa, updates } => {
@@ -424,9 +444,10 @@ impl PythReceiver {
424444
return Err(PythReceiverError::PriceFeedNotFoundWithinRange);
425445
}
426446

447+
let price_id_fb =
448+
FixedBytes::<32>::from(price_feed_message.feed_id);
449+
427450
if check_uniqueness {
428-
let price_id_fb =
429-
FixedBytes::<32>::from(price_feed_message.feed_id);
430451
let prev_price_info = self.latest_price_info.get(price_id_fb);
431452
let prev_publish_time =
432453
prev_price_info.publish_time.get().to::<u64>();
@@ -439,7 +460,7 @@ impl PythReceiver {
439460
}
440461

441462
let price_info_return = (
442-
price_feed_message.feed_id,
463+
price_id_fb,
443464
U64::from(publish_time),
444465
I32::from_be_bytes(price_feed_message.exponent.to_be_bytes()),
445466
I64::from_be_bytes(price_feed_message.price.to_be_bytes()),
@@ -448,7 +469,7 @@ impl PythReceiver {
448469
U64::from(price_feed_message.ema_conf),
449470
);
450471

451-
price_feeds.insert(price_feed_message.feed_id, price_info_return);
472+
price_feeds.push(price_info_return);
452473
}
453474
_ => {
454475
return Err(PythReceiverError::InvalidAccumulatorMessageType);
@@ -458,10 +479,7 @@ impl PythReceiver {
458479
}
459480
};
460481

461-
Ok(price_feeds
462-
.into_iter()
463-
.map(|(_, price_info)| price_info)
464-
.collect())
482+
Ok(price_feeds)
465483
}
466484

467485
pub fn parse_twap_price_feed_updates(

target_chains/stylus/contracts/pyth-receiver/src/structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct PriceFeedStorage {
5858
// pub ema_conf: U64,
5959
// }
6060

61-
pub type PriceFeedReturn = ([u8; 32], U64, I32, I64, U64, I64, U64);
61+
pub type PriceFeedReturn = (FixedBytes<32>, U64, I32, I64, U64, I64, U64);
6262

6363
// (price, conf, expo, publish_time)
6464
pub type PriceReturn = (I64, U64, I32, U64);

0 commit comments

Comments
 (0)