Skip to content

Commit 9e45927

Browse files
committed
getting publish time filtering working
1 parent 7f2407d commit 9e45927

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
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
@@ -17,6 +17,7 @@ pub enum PythReceiverError {
1717
InsufficientFee,
1818
InvalidEmitterAddress,
1919
TooManyUpdates,
20+
PriceFeedNotFoundWithinRange,
2021
}
2122

2223
impl core::fmt::Debug for PythReceiverError {
@@ -43,6 +44,7 @@ impl From<PythReceiverError> for Vec<u8> {
4344
PythReceiverError::InsufficientFee => 13,
4445
PythReceiverError::InvalidEmitterAddress => 14,
4546
PythReceiverError::TooManyUpdates => 15,
47+
PythReceiverError::PriceFeedNotFoundWithinRange => 16,
4648
}]
4749
}
4850
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,14 @@ mod test {
369369
)
370370
);
371371
}
372+
373+
#[motsu::test]
374+
fn test_multiple_updates_same_id_updates_latest(
375+
pyth_contract: Contract<PythReceiver>,
376+
wormhole_contract: Contract<WormholeContract>,
377+
alice: Address,
378+
) {
379+
pyth_wormhole_init(&pyth_contract, &wormhole_contract, &alice);
380+
alice.fund(U256::from(200));
381+
}
372382
}

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl PythReceiver {
191191
min_publish_time: u64,
192192
max_publish_time: u64,
193193
_unique: bool,
194-
) -> Result<(), PythReceiverError> {
194+
) -> Result<Vec<([u8; 32], PriceInfoReturn)>, PythReceiverError> {
195195
let price_pairs = self.parse_price_feed_updates_internal(
196196
update_data,
197197
min_publish_time,
@@ -201,7 +201,7 @@ impl PythReceiver {
201201
true, // store_updates_if_fresh
202202
)?;
203203

204-
for (price_id, price_return) in price_pairs {
204+
for (price_id, price_return) in price_pairs.clone() {
205205
let price_id_fb: FixedBytes<32> = FixedBytes::from(price_id);
206206
let mut recent_price_info = self.latest_price_info.setter(price_id_fb);
207207

@@ -217,7 +217,7 @@ impl PythReceiver {
217217
}
218218
}
219219

220-
Ok(())
220+
Ok(price_pairs)
221221
}
222222

223223
fn get_update_fee(&self, update_data: Vec<u8>) -> Result<U256, PythReceiverError> {
@@ -266,14 +266,29 @@ impl PythReceiver {
266266
check_update_data_is_minimal: bool,
267267
store_updates_if_fresh: bool,
268268
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
269-
let price_pairs = self.parse_price_feed_updates_internal(
270-
update_data,
271-
min_allowed_publish_time,
272-
max_allowed_publish_time,
273-
check_uniqueness,
274-
check_update_data_is_minimal,
275-
store_updates_if_fresh,
276-
)?;
269+
let price_pairs;
270+
if store_updates_if_fresh {
271+
price_pairs = self.update_price_feeds_internal(
272+
update_data,
273+
price_ids.clone(),
274+
min_allowed_publish_time,
275+
max_allowed_publish_time,
276+
check_uniqueness,
277+
)?;
278+
} else {
279+
price_pairs = self.parse_price_feed_updates_internal(
280+
update_data,
281+
min_allowed_publish_time,
282+
max_allowed_publish_time,
283+
check_uniqueness,
284+
check_update_data_is_minimal,
285+
store_updates_if_fresh,
286+
)?;
287+
}
288+
289+
if check_update_data_is_minimal && price_ids.len() != price_pairs.len() {
290+
return Err(PythReceiverError::InvalidUpdateData);
291+
}
277292

278293
let price_map: BTreeMap<[u8; 32], PriceInfoReturn> = price_pairs.into_iter().collect();
279294

@@ -283,14 +298,7 @@ impl PythReceiver {
283298
if let Some(price_info) = price_map.get(&price_id) {
284299
result.push(*price_info);
285300
} else {
286-
result.push((
287-
U64::from(0),
288-
I32::from_be_bytes([0, 0, 0, 0]),
289-
I64::from_be_bytes([0, 0, 0, 0, 0, 0, 0, 0]),
290-
U64::from(0),
291-
I64::from_be_bytes([0, 0, 0, 0, 0, 0, 0, 0]),
292-
U64::from(0),
293-
));
301+
return Err(PythReceiverError::PriceFeedNotFoundWithinRange);
294302
}
295303
}
296304

0 commit comments

Comments
 (0)