Skip to content

Commit 22ebbd3

Browse files
feat: modify parse functions to return HashMap internally and matching vector indices
- Modified parse_price_feed_updates_internal to build a BTreeMap internally mapping price IDs to PriceInfoReturn - Updated parse_price_feed_updates_with_config to return a vector with matching indices to input price_ids - Added price_ids parameter to parse_price_feed_updates_internal function - Maintained existing error handling and validation logic Co-Authored-By: [email protected] <[email protected]>
1 parent 9453d7b commit 22ebbd3

File tree

1 file changed

+27
-14
lines changed
  • target_chains/stylus/contracts/pyth-receiver/src

1 file changed

+27
-14
lines changed

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod integration_tests;
1212
#[cfg(test)]
1313
mod test_data;
1414

15-
use alloc::vec::Vec;
15+
use alloc::{vec::Vec, collections::BTreeMap};
1616
use stylus_sdk::{alloy_primitives::{U16, U32, U256, U64, I32, I64, FixedBytes, Address},
1717
prelude::*,
1818
storage::{StorageAddress, StorageVec, StorageMap, StorageUint, StorageBool, StorageU256, StorageU16, StorageFixedBytes},
@@ -215,12 +215,21 @@ impl PythReceiver {
215215
check_update_data_is_minimal: bool,
216216
store_updates_if_fresh: bool,
217217
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
218-
218+
self.parse_price_feed_updates_internal(
219+
update_data,
220+
price_ids,
221+
min_allowed_publish_time,
222+
max_allowed_publish_time,
223+
check_uniqueness,
224+
check_update_data_is_minimal,
225+
store_updates_if_fresh,
226+
)
219227
}
220228

221229
fn parse_price_feed_updates_internal(
222230
&mut self,
223231
update_data: Vec<u8>,
232+
price_ids: Vec<[u8; 32]>,
224233
min_allowed_publish_time: u64,
225234
max_allowed_publish_time: u64,
226235
check_uniqueness: bool,
@@ -242,14 +251,14 @@ impl PythReceiver {
242251

243252
let update_data = AccumulatorUpdateData::try_from_slice(&update_data_array).map_err(|_| PythReceiverError::InvalidAccumulatorMessage)?;
244253

245-
let mut price_feeds: Vec<PriceInfoReturn> = Vec::new();
246-
254+
let mut price_feeds: BTreeMap<[u8; 32], PriceInfoReturn> = BTreeMap::new();
255+
247256
match update_data.proof {
248257
Proof::WormholeMerkle { vaa, updates } => {
249258
let wormhole: IWormholeContract = IWormholeContract::new(self.wormhole.get());
250259
let config = Call::new();
251260
wormhole.parse_and_verify_vm(config, Vec::from(vaa.clone())).map_err(|_| PythReceiverError::InvalidWormholeMessage)?;
252-
261+
253262
let vaa = Vaa::read(&mut Vec::from(vaa.clone()).as_slice()).map_err(|_| PythReceiverError::VaaVerificationFailed)?;
254263

255264
let cur_emitter_address: &[u8; 32] = vaa
@@ -275,7 +284,7 @@ impl PythReceiver {
275284
let total_fee = self.get_total_fee(num_updates);
276285

277286
let value = self.vm().msg_value();
278-
287+
279288
if value < total_fee {
280289
return Err(PythReceiverError::InsufficientFee);
281290
}
@@ -306,13 +315,7 @@ impl PythReceiver {
306315
recent_price_info.ema_conf.get(),
307316
);
308317

309-
// Find the index of the price_id in the input price_ids vector
310-
// if let Some(idx) = price_ids.iter().position(|id| *id == price_feed_message.feed_id) {
311-
// if price_feeds.len() <= idx {
312-
// price_feeds.resize(idx + 1, Default::default());
313-
// }
314-
// price_feeds[idx] = price_info_return;
315-
// }
318+
price_feeds.insert(price_feed_message.feed_id, price_info_return);
316319
},
317320
_ => {
318321
return Err(PythReceiverError::InvalidAccumulatorMessageType);
@@ -322,7 +325,17 @@ impl PythReceiver {
322325
}
323326
};
324327

325-
Ok(price_feeds)
328+
let mut result: Vec<PriceInfoReturn> = Vec::with_capacity(price_ids.len());
329+
330+
for price_id in price_ids {
331+
if let Some(price_info) = price_feeds.get(&price_id) {
332+
result.push(*price_info);
333+
} else {
334+
result.push((U64::from(0), I32::from_be_bytes([0, 0, 0, 0]), I64::from_be_bytes([0, 0, 0, 0, 0, 0, 0, 0]), U64::from(0), I64::from_be_bytes([0, 0, 0, 0, 0, 0, 0, 0]), U64::from(0)));
335+
}
336+
}
337+
338+
Ok(result)
326339
}
327340

328341
pub fn parse_twap_price_feed_updates(

0 commit comments

Comments
 (0)