Skip to content

Commit ce8a378

Browse files
feat: modify parse functions to return map internally and handle array ordering
- parse_price_feed_updates_internal now returns Vec<([u8; 32], PriceInfoReturn)> without taking price_ids parameter - parse_price_feed_updates_with_config converts internal result to BTreeMap for efficient lookup and returns ordered vector - This enables update_price_feeds functions to use the dictionary directly as requested Co-Authored-By: [email protected] <[email protected]>
1 parent 22ebbd3 commit ce8a378

File tree

1 file changed

+19
-16
lines changed
  • target_chains/stylus/contracts/pyth-receiver/src

1 file changed

+19
-16
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,40 @@ impl PythReceiver {
215215
check_update_data_is_minimal: bool,
216216
store_updates_if_fresh: bool,
217217
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
218-
self.parse_price_feed_updates_internal(
218+
let price_pairs = self.parse_price_feed_updates_internal(
219219
update_data,
220-
price_ids,
221220
min_allowed_publish_time,
222221
max_allowed_publish_time,
223222
check_uniqueness,
224223
check_update_data_is_minimal,
225224
store_updates_if_fresh,
226-
)
225+
)?;
226+
227+
let price_map: BTreeMap<[u8; 32], PriceInfoReturn> = price_pairs.into_iter().collect();
228+
229+
// Create a vector with the same length as price_ids
230+
let mut result: Vec<PriceInfoReturn> = Vec::with_capacity(price_ids.len());
231+
232+
for price_id in price_ids {
233+
if let Some(price_info) = price_map.get(&price_id) {
234+
result.push(*price_info);
235+
} else {
236+
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)));
237+
}
238+
}
239+
240+
Ok(result)
227241
}
228242

229243
fn parse_price_feed_updates_internal(
230244
&mut self,
231245
update_data: Vec<u8>,
232-
price_ids: Vec<[u8; 32]>,
233246
min_allowed_publish_time: u64,
234247
max_allowed_publish_time: u64,
235248
check_uniqueness: bool,
236249
check_update_data_is_minimal: bool,
237250
store_updates_if_fresh: bool,
238-
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
251+
) -> Result<Vec<([u8; 32], PriceInfoReturn)>, PythReceiverError> {
239252
let update_data_array: &[u8] = &update_data;
240253
// Check the first 4 bytes of the update_data_array for the magic header
241254
if update_data_array.len() < 4 {
@@ -325,17 +338,7 @@ impl PythReceiver {
325338
}
326339
};
327340

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)
341+
Ok(price_feeds.into_iter().collect())
339342
}
340343

341344
pub fn parse_twap_price_feed_updates(

0 commit comments

Comments
 (0)