Skip to content

Commit bac1263

Browse files
committed
Replace get_prev_price with better methods
1 parent fb9312e commit bac1263

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

pyth-sdk/src/lib.rs

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub type ProductIdentifier = Identifier;
8484
/// Jan 1970). It is a signed integer because it's the standard in Unix systems and allows easier
8585
/// time difference.
8686
pub type UnixTimestamp = i64;
87+
pub type DurationInSeconds = u64;
8788

8889
/// Represents availability status of a price feed.
8990
#[derive(
@@ -277,22 +278,67 @@ impl PriceFeed {
277278
}
278279
}
279280

280-
/// Get the "unchecked" previous price with Trading status,
281-
/// along with the timestamp at which it was generated.
281+
/// Get the latest available price, along with the timestamp when it was generated.
282282
///
283-
/// WARNING:
284-
/// We make no guarantees about the unchecked price and confidence returned by
285-
/// this function: it could differ significantly from the current price.
286-
/// We strongly encourage you to use `get_current_price` instead.
287-
pub fn get_prev_price_unchecked(&self) -> (Price, UnixTimestamp) {
288-
(
289-
Price {
290-
price: self.prev_price,
291-
conf: self.prev_conf,
292-
expo: self.expo,
293-
},
294-
self.prev_publish_time,
295-
)
283+
/// This function returns the same price as `get_current_price` in the case where a price was
284+
/// available at the time this `PriceFeed` was published (`publish_time`). However, if a
285+
/// price was not available at that time, this function returns the price from the latest
286+
/// time at which the price was available. The returned price can be from arbitrarily far in
287+
/// the past; this function makes no guarantees that the returned price is recent or useful
288+
/// for any particular application.
289+
///
290+
/// Users of this function should check the returned timestamp to ensure that the returned price
291+
/// is sufficiently recent for their application. If you are considering using this
292+
/// function, it may be safer / easier to use either `get_current_price` or
293+
/// `get_latest_available_price_within_duration`.
294+
///
295+
/// Returns a struct containing the latest available price, confidence interval, and the
296+
/// exponent for both numbers along with the timestamp when that price was generated.
297+
pub fn get_latest_available_price_unchecked(&self) -> (Price, UnixTimestamp) {
298+
match self.status {
299+
PriceStatus::Trading => (
300+
Price {
301+
price: self.price,
302+
conf: self.conf,
303+
expo: self.expo,
304+
},
305+
self.publish_time,
306+
),
307+
_ => (
308+
Price {
309+
price: self.prev_price,
310+
conf: self.prev_conf,
311+
expo: self.expo,
312+
},
313+
self.prev_publish_time,
314+
),
315+
}
316+
}
317+
318+
/// Get the latest price as long as it was updated within `duration` seconds of the
319+
/// `current_time`.
320+
///
321+
/// This function is a sanity-checked version of `get_latest_available_price_unchecked` which is
322+
/// useful in applications that require a sufficiently-recent price. Returns `None` if the
323+
/// price wasn't updated sufficiently recently.
324+
///
325+
/// Returns a struct containing the latest available price, confidence interval and the exponent
326+
/// for both numbers, or `None` if no price update occurred within `duration` seconds of the
327+
/// `current_time`.
328+
pub fn get_latest_available_price_within_duration(
329+
&self,
330+
current_time: UnixTimestamp,
331+
duration: DurationInSeconds,
332+
) -> Option<Price> {
333+
let (price, publish_time) = self.get_latest_available_price_unchecked();
334+
335+
let time_diff_abs = (publish_time - current_time).abs() as u64;
336+
337+
if time_diff_abs > duration {
338+
return None;
339+
}
340+
341+
Some(price)
296342
}
297343
}
298344
#[cfg(test)]

0 commit comments

Comments
 (0)