Skip to content

Commit 9e144ff

Browse files
committed
implemented parse_price_feed_updates_unique
1 parent 02014ab commit 9e144ff

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-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
@@ -18,6 +18,7 @@ pub enum PythReceiverError {
1818
InvalidEmitterAddress,
1919
TooManyUpdates,
2020
PriceFeedNotFoundWithinRange,
21+
NoFreshUpdate,
2122
}
2223

2324
impl core::fmt::Debug for PythReceiverError {
@@ -45,6 +46,7 @@ impl From<PythReceiverError> for Vec<u8> {
4546
PythReceiverError::InvalidEmitterAddress => 14,
4647
PythReceiverError::TooManyUpdates => 15,
4748
PythReceiverError::PriceFeedNotFoundWithinRange => 16,
49+
PythReceiverError::NoFreshUpdate => 17,
4850
}]
4951
}
5052
}

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,30 @@ impl PythReceiver {
193193

194194
pub fn update_price_feeds_if_necessary(
195195
&mut self,
196-
_update_data: Vec<Vec<u8>>,
197-
_price_ids: Vec<[u8; 32]>,
198-
_publish_times: Vec<u64>,
199-
) {
200-
// dummy implementation
196+
update_data: Vec<Vec<u8>>,
197+
price_ids: Vec<[u8; 32]>,
198+
publish_times: Vec<u64>,
199+
) -> Result<(), PythReceiverError> {
200+
if (price_ids.len() != publish_times.len())
201+
|| (price_ids.is_empty() && publish_times.is_empty())
202+
{
203+
return Err(PythReceiverError::InvalidUpdateData);
204+
}
205+
206+
for i in 0..price_ids.len() {
207+
if (self.latest_price_info_publish_time(price_ids[i]) < publish_times[i]) {
208+
self.update_price_feeds(update_data.clone())?;
209+
return Ok(());
210+
}
211+
}
212+
213+
return Err(PythReceiverError::NoFreshUpdate);
214+
}
215+
216+
fn latest_price_info_publish_time(&self, price_id: [u8; 32]) -> u64 {
217+
let price_id_fb: FixedBytes<32> = FixedBytes::from(price_id);
218+
let recent_price_info = self.latest_price_info.get(price_id_fb);
219+
recent_price_info.publish_time.get().to::<u64>()
201220
}
202221

203222
fn update_price_feeds_internal(
@@ -449,12 +468,21 @@ impl PythReceiver {
449468

450469
pub fn parse_price_feed_updates_unique(
451470
&mut self,
452-
_update_data: Vec<Vec<u8>>,
453-
_price_ids: Vec<[u8; 32]>,
454-
_min_publish_time: u64,
455-
_max_publish_time: u64,
456-
) -> Vec<PriceInfoReturn> {
457-
Vec::new()
471+
update_data: Vec<Vec<u8>>,
472+
price_ids: Vec<[u8; 32]>,
473+
min_publish_time: u64,
474+
max_publish_time: u64,
475+
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
476+
let price_feeds = self.parse_price_feed_updates_with_config(
477+
update_data,
478+
price_ids,
479+
min_publish_time,
480+
max_publish_time,
481+
true,
482+
false,
483+
false,
484+
);
485+
price_feeds
458486
}
459487

460488
fn is_no_older_than(&self, publish_time: U64, max_age: u64) -> bool {

0 commit comments

Comments
 (0)