Skip to content

Commit ef91b1e

Browse files
authored
Merge pull request #53 from pyth-network/guibescos/better-logic-for-components
Use numComponents
2 parents 5bbbe2b + fb54224 commit ef91b1e

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/__tests__/Price.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,7 @@ test('Handle price getting stale', (done) => {
111111
expect(stalePrice.price).toBeUndefined()
112112
expect(stalePrice.confidence).toBeUndefined()
113113

114+
expect(price.numComponentPrices).toBe(price.priceComponents.length)
115+
114116
done()
115117
})

src/index.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export interface Price {
8080
}
8181

8282
export interface PriceComponent {
83-
publisher: PublicKey | null
83+
publisher: PublicKey
8484
aggregate: Price
8585
latest: Price
8686
}
@@ -329,19 +329,14 @@ export const parsePriceData = (data: Buffer, currentSlot?: number): PriceData =>
329329
// price components - up to 32
330330
const priceComponents: PriceComponent[] = []
331331
let offset = 240
332-
let shouldContinue = true
333-
while (offset < data.length && shouldContinue) {
334-
const publisher = PKorNull(data.slice(offset, offset + 32))
332+
while (priceComponents.length < numComponentPrices) {
333+
const publisher = new PublicKey(data.slice(offset, offset + 32))
335334
offset += 32
336-
if (publisher) {
337-
const componentAggregate = parsePriceInfo(data.slice(offset, offset + 32), exponent)
338-
offset += 32
339-
const latest = parsePriceInfo(data.slice(offset, offset + 32), exponent)
340-
offset += 32
341-
priceComponents.push({ publisher, aggregate: componentAggregate, latest })
342-
} else {
343-
shouldContinue = false
344-
}
335+
const componentAggregate = parsePriceInfo(data.slice(offset, offset + 32), exponent)
336+
offset += 32
337+
const latest = parsePriceInfo(data.slice(offset, offset + 32), exponent)
338+
offset += 32
339+
priceComponents.push({ publisher, aggregate: componentAggregate, latest })
345340
}
346341

347342
return {

0 commit comments

Comments
 (0)