@@ -67,7 +67,7 @@ export interface Ema {
6767 denominator : bigint
6868}
6969
70- export interface PriceData extends Base , Price {
70+ export interface PriceData extends Base {
7171 priceType : number
7272 exponent : number
7373 numComponentPrices : number
@@ -90,6 +90,15 @@ export interface PriceData extends Base, Price {
9090 drv3Component : bigint
9191 drv3 : number
9292 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 ,
93102}
94103
95104/** 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 => {
257266 // space for future derived values
258267 const drv3Component = readBigInt64LE ( data , 200 )
259268 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+
261278 // price components - up to 32
262279 const priceComponents : PriceComponent [ ] = [ ]
263280 let offset = 240
@@ -266,15 +283,16 @@ export const parsePriceData = (data: Buffer): PriceData => {
266283 const publisher = PKorNull ( data . slice ( offset , offset + 32 ) )
267284 offset += 32
268285 if ( publisher ) {
269- const aggregate = parsePriceInfo ( data . slice ( offset , offset + 32 ) , exponent )
286+ const componentAggregate = parsePriceInfo ( data . slice ( offset , offset + 32 ) , exponent )
270287 offset += 32
271288 const latest = parsePriceInfo ( data . slice ( offset , offset + 32 ) , exponent )
272289 offset += 32
273- priceComponents . push ( { publisher, aggregate, latest } )
290+ priceComponents . push ( { publisher, aggregate : componentAggregate , latest } )
274291 } else {
275292 shouldContinue = false
276293 }
277294 }
295+
278296 return {
279297 magic,
280298 version,
@@ -301,8 +319,10 @@ export const parsePriceData = (data: Buffer): PriceData => {
301319 previousConfidence,
302320 drv3Component,
303321 drv3,
304- ... aggregatePriceInfo ,
322+ aggregate ,
305323 priceComponents,
324+ price,
325+ confidence
306326 }
307327}
308328
0 commit comments