@@ -67,7 +67,7 @@ export interface Ema {
67
67
denominator : bigint
68
68
}
69
69
70
- export interface PriceData extends Base , Price {
70
+ export interface PriceData extends Base {
71
71
priceType : number
72
72
exponent : number
73
73
numComponentPrices : number
@@ -90,6 +90,15 @@ export interface PriceData extends Base, Price {
90
90
drv3Component : bigint
91
91
drv3 : number
92
92
priceComponents : PriceComponent [ ]
93
+ aggregate : Price ,
94
+ // The current price and confidence. The typical use of this interface is to consume these two fields.
95
+ // If undefined, Pyth does not currently have price information for this product. This condition can
96
+ // happen for various reasons (e.g., US equity market is closed, or insufficient publishers), and your
97
+ // application should handle it gracefully. Note that other raw price information fields (such as
98
+ // aggregate.price) may be defined even if this is undefined; you most likely should not use those fields,
99
+ // as their value can be arbitrary when this is undefined.
100
+ price : number | undefined
101
+ confidence : number | undefined ,
93
102
}
94
103
95
104
/** Parse data as a generic Pyth account. Use this method if you don't know the account type. */
@@ -257,7 +266,15 @@ export const parsePriceData = (data: Buffer): PriceData => {
257
266
// space for future derived values
258
267
const drv3Component = readBigInt64LE ( data , 200 )
259
268
const drv3 = Number ( drv3Component ) * 10 ** exponent
260
- const aggregatePriceInfo = parsePriceInfo ( data . slice ( 208 , 240 ) , exponent )
269
+ const aggregate = parsePriceInfo ( data . slice ( 208 , 240 ) , exponent )
270
+
271
+ let price
272
+ let confidence
273
+ if ( aggregate . status === 1 ) {
274
+ price = aggregate . price
275
+ confidence = aggregate . confidence
276
+ }
277
+
261
278
// price components - up to 32
262
279
const priceComponents : PriceComponent [ ] = [ ]
263
280
let offset = 240
@@ -266,15 +283,16 @@ export const parsePriceData = (data: Buffer): PriceData => {
266
283
const publisher = PKorNull ( data . slice ( offset , offset + 32 ) )
267
284
offset += 32
268
285
if ( publisher ) {
269
- const aggregate = parsePriceInfo ( data . slice ( offset , offset + 32 ) , exponent )
286
+ const componentAggregate = parsePriceInfo ( data . slice ( offset , offset + 32 ) , exponent )
270
287
offset += 32
271
288
const latest = parsePriceInfo ( data . slice ( offset , offset + 32 ) , exponent )
272
289
offset += 32
273
- priceComponents . push ( { publisher, aggregate, latest } )
290
+ priceComponents . push ( { publisher, aggregate : componentAggregate , latest } )
274
291
} else {
275
292
shouldContinue = false
276
293
}
277
294
}
295
+
278
296
return {
279
297
magic,
280
298
version,
@@ -301,8 +319,10 @@ export const parsePriceData = (data: Buffer): PriceData => {
301
319
previousConfidence,
302
320
drv3Component,
303
321
drv3,
304
- ... aggregatePriceInfo ,
322
+ aggregate ,
305
323
priceComponents,
324
+ price,
325
+ confidence
306
326
}
307
327
}
308
328
0 commit comments