From 08edd23710f6473b4238a04315a53dff5736381c Mon Sep 17 00:00:00 2001 From: piobab Date: Tue, 13 Jun 2023 18:27:05 +0200 Subject: [PATCH] Fix 'attempt to subtract with overflow' if publish_time from Pyth is > current block time. (#206) --- contracts/oracle/base/src/pyth.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/contracts/oracle/base/src/pyth.rs b/contracts/oracle/base/src/pyth.rs index 34077d22a..ab38d4ddd 100644 --- a/contracts/oracle/base/src/pyth.rs +++ b/contracts/oracle/base/src/pyth.rs @@ -28,18 +28,17 @@ pub fn query_pyth_price>( let price_feed_response = query_price_feed(&deps.querier, contract_addr, price_feed_id)?; let price_feed = price_feed_response.price_feed; - // Get the current price and confidence interval from the price feed - let current_price = price_feed.get_price_unchecked(); - // Check if the current price is not too old - if (current_time - current_price.publish_time as u64) > max_staleness { + let current_price_opt = price_feed.get_price_no_older_than(current_time as i64, max_staleness); + let Some(current_price) = current_price_opt else { return Err(InvalidPrice { reason: format!( "current price publish time is too old/stale. published: {}, now: {}", - current_price.publish_time, current_time + price_feed.get_price_unchecked().publish_time, + current_time ), }); - } + }; // Check if the current price is > 0 if current_price.price <= 0 {