11import { Convert , PriceFeed as JsonPriceFeed } from "./schemas/PriceFeed" ;
22
33export type UnixTimestamp = number ;
4+ export type DurationInSeconds = number ;
45export type HexString = string ;
56
67/**
@@ -190,9 +191,14 @@ export class PriceFeed {
190191
191192 /**
192193 * Get the current price and confidence interval as fixed-point numbers of the form a * 10^e.
194+ * This function returns the current best estimate of the price at the time that this `PriceFeed` was
195+ * published (`publish_time`). This function returns `undefined` if the oracle was unable to determine
196+ * the price at that time; this condition can happen for various reasons, such as certain markets only
197+ * trading during certain times.
193198 *
194- * @returns a struct containing the current price, confidence interval, and the exponent for
195- * both numbers. Returns `undefined` if price information is currently unavailable for any reason.
199+ * @returns a struct containing the price and confidence interval as of `publish_time`, along with
200+ * the exponent for both numbers. Returns `undefined` if price information is currently unavailable
201+ * for any reason.
196202 */
197203 getCurrentPrice ( ) : Price | undefined {
198204 if ( this . status !== PriceStatus . Trading ) {
@@ -216,20 +222,55 @@ export class PriceFeed {
216222 }
217223
218224 /**
219- * Get the "unchecked" previous price with Trading status, along with the timestamp at which it was generated.
225+ * Get the latest available price, along with the timestamp when it was generated.
226+ * This function returns the same price as `getCurrentPrice` in the case where a price was available
227+ * at the time this `PriceFeed` was published (`publish_time`). However, if a price was not available
228+ * at that time, this function returns the price from the latest time at which the price was available.
229+ * The returned price can be from arbitrarily far in the past; this function makes no guarantees that
230+ * the returned price is recent or useful for any particular application.
220231 *
221- * @returns a struct containing the previous price, confidence interval, and the exponent for
222- * both numbers along with the timestamp that the price was generated.
232+ * Users of this function should check the returned timestamp to ensure that the returned price is
233+ * sufficiently recent for their application. If you are considering using this function, it may be
234+ * safer / easier to use either `getCurrentPrice` or `getLatestAvailablePriceWithinDuration`.
223235 *
224- * WARNING:
225- * We make no guarantees about the unchecked price and confidence returned by
226- * this function: it could differ significantly from the current price.
227- * We strongly encourage you to use `get_current_price` instead.
236+ * @returns a struct containing the latest available price, confidence interval, and the exponent for
237+ * both numbers along with the timestamp when that price was generated.
228238 */
229- getPrevPriceUnchecked ( ) : [ Price , UnixTimestamp ] {
239+ getLatestAvailablePriceUnchecked ( ) : [ Price , UnixTimestamp ] {
240+ // If the price status is Trading then it's the latest price
241+ // with the Trading status.
242+ if ( this . status === PriceStatus . Trading ) {
243+ return [ new Price ( this . conf , this . expo , this . price ) , this . publishTime ] ;
244+ }
245+
230246 return [
231247 new Price ( this . prevConf , this . expo , this . prevPrice ) ,
232248 this . prevPublishTime ,
233249 ] ;
234250 }
251+
252+ /**
253+ * Get the latest price as long as it was updated within `duration` seconds of the current time.
254+ * This function is a sanity-checked version of `getLatestAvailablePriceUnchecked` which is useful in
255+ * applications that require a sufficiently-recent price. Returns `undefined` if the price wasn't
256+ * updated sufficiently recently.
257+ *
258+ * @param duration return a price as long as it has been updated within this number of seconds
259+ * @returns a struct containing the latest available price, confidence interval and the exponent for
260+ * both numbers, or `undefined` if no price update occurred within `duration` seconds of the current time.
261+ */
262+ getLatestAvailablePriceWithinDuration ( duration : DurationInSeconds ) : Price | undefined {
263+ const [ price , timestamp ] = this . getLatestAvailablePriceUnchecked ( ) ;
264+
265+ const currentTime : UnixTimestamp = Math . floor ( Date . now ( ) / 1000 ) ;
266+
267+ // This checks the absolute difference as a sanity check
268+ // for the cases that the system time is behind or price
269+ // feed timestamp happen to be in the future (a bug).
270+ if ( Math . abs ( currentTime - timestamp ) > duration ) {
271+ return undefined ;
272+ }
273+
274+ return price ;
275+ }
235276}
0 commit comments