@@ -5,10 +5,11 @@ import { Server } from "http";
5
5
import { StatusCodes } from "http-status-codes" ;
6
6
import morgan from "morgan" ;
7
7
import responseTime from "response-time" ;
8
- import { DurationInMs , DurationInSec } from "./helpers" ;
8
+ import { DurationInMs , DurationInSec , TimestampInSec } from "./helpers" ;
9
9
import { PriceStore } from "./listen" ;
10
10
import { logger } from "./logging" ;
11
11
import { PromClient } from "./promClient" ;
12
+ import { HexString } from "@pythnetwork/pyth-sdk-js" ;
12
13
13
14
const MORGAN_LOG_FORMAT =
14
15
':remote-addr - :remote-user ":method :url HTTP/:http-version"' +
@@ -104,7 +105,7 @@ export class RestAPI {
104
105
}
105
106
106
107
const freshness : DurationInSec =
107
- new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . attestationTime ;
108
+ new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . priceFeed . publishTime ;
108
109
this . promClient ?. addApiRequestsPriceFreshness (
109
110
req . path ,
110
111
id ,
@@ -162,7 +163,7 @@ export class RestAPI {
162
163
}
163
164
164
165
const freshness : DurationInSec =
165
- new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . attestationTime ;
166
+ new Date ( ) . getTime ( ) / 1000 - latestPriceInfo . priceFeed . publishTime ;
166
167
this . promClient ?. addApiRequestsPriceFreshness (
167
168
req . path ,
168
169
id ,
@@ -203,6 +204,35 @@ export class RestAPI {
203
204
} ) ;
204
205
endpoints . push ( "api/price_feed_ids" ) ;
205
206
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
+
206
236
app . get ( "/ready" , ( _ , res : Response ) => {
207
237
if ( this . isReady ! ( ) ) {
208
238
res . sendStatus ( StatusCodes . OK ) ;
0 commit comments