@@ -5,10 +5,11 @@ import { Server } from "http";
55import { StatusCodes } from "http-status-codes" ;
66import morgan from "morgan" ;
77import responseTime from "response-time" ;
8- import { DurationInMs , DurationInSec } from "./helpers" ;
8+ import { DurationInMs , DurationInSec , TimestampInSec } from "./helpers" ;
99import { PriceStore } from "./listen" ;
1010import { logger } from "./logging" ;
1111import { PromClient } from "./promClient" ;
12+ import { HexString } from "@pythnetwork/pyth-sdk-js" ;
1213
1314const MORGAN_LOG_FORMAT =
1415 ':remote-addr - :remote-user ":method :url HTTP/:http-version"' +
@@ -104,7 +105,7 @@ export class RestAPI {
104105 }
105106
106107 const freshness : DurationInSec =
107- new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . attestationTime ;
108+ new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . priceFeed . publishTime ;
108109 this . promClient ?. addApiRequestsPriceFreshness (
109110 req . path ,
110111 id ,
@@ -162,7 +163,7 @@ export class RestAPI {
162163 }
163164
164165 const freshness : DurationInSec =
165- new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . attestationTime ;
166+ new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . priceFeed . publishTime ;
166167 this . promClient ?. addApiRequestsPriceFreshness (
167168 req . path ,
168169 id ,
@@ -203,6 +204,35 @@ export class RestAPI {
203204 } ) ;
204205 endpoints . push ( "api/price_feed_ids" ) ;
205206
207+ const staleFeedsInputSchema : schema = {
208+ query : Joi . object ( {
209+ threshold : Joi . number ( ) . required ( ) ,
210+ } ) . required ( ) ,
211+ } ;
212+ app . get ( "/api/stale_feeds" ,
213+ validate ( staleFeedsInputSchema ) ,
214+ ( req : Request , res : Response ) => {
215+ let stalenessThresholdSeconds = Number ( req . query . threshold as string ) ;
216+
217+ let currentTime : TimestampInSec = Math . floor ( Date . now ( ) / 1000 ) ;
218+
219+ let priceIds = [ ...this . priceFeedVaaInfo . getPriceIds ( ) ] ;
220+ let stalePrices : Record < HexString , number > = { }
221+
222+ for ( let priceId of priceIds ) {
223+ const latency = currentTime - this . priceFeedVaaInfo . getLatestPriceInfo ( priceId ) ! . priceFeed . publishTime
224+ if ( latency > stalenessThresholdSeconds ) {
225+ stalePrices [ priceId ] = latency
226+ }
227+ }
228+
229+ res . json ( stalePrices ) ;
230+ }
231+ ) ;
232+ endpoints . push (
233+ "/api/stale_feeds?threshold=<staleness_threshold_seconds>"
234+ ) ;
235+
206236 app . get ( "/ready" , ( _ , res : Response ) => {
207237 if ( this . isReady ! ( ) ) {
208238 res . sendStatus ( StatusCodes . OK ) ;
0 commit comments