Skip to content

Commit 93bdedc

Browse files
jayantkJayant Krishnamurthy
andauthored
Make the default usage check the price status (#11)
* Check the status of the price * fix lint errors * drive-by: bump solana web3 version * bump minor version Co-authored-by: Jayant Krishnamurthy <[email protected]>
1 parent 2b8dafe commit 93bdedc

File tree

4 files changed

+163
-53
lines changed

4 files changed

+163
-53
lines changed

package-lock.json

Lines changed: 129 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/client",
3-
"version": "2.3.2",
3+
"version": "2.4.0",
44
"description": "Client for consuming Pyth price data",
55
"homepage": "https://pyth.network",
66
"main": "lib/index.js",
@@ -37,7 +37,7 @@
3737
"typescript": "^4.2.4"
3838
},
3939
"dependencies": {
40-
"@solana/web3.js": "^1.10.1",
40+
"@solana/web3.js": "^1.30.2",
4141
"assert": "^2.0.0",
4242
"buffer": "^6.0.1"
4343
}

src/example_usage.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ const pythConnection = new PythConnection(connection, pythPublicKey)
1010
pythConnection.onPriceChange((product, price) => {
1111
// sample output:
1212
// SRM/USD: $8.68725 ±$0.0131
13-
// tslint:disable-next-line:no-console
14-
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`)
13+
if (price.price && price.confidence) {
14+
// tslint:disable-next-line:no-console
15+
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`)
16+
} else {
17+
// tslint:disable-next-line:no-console
18+
console.log(`${product.symbol}: price currently unavailable`)
19+
}
1520
})
1621

1722
// tslint:disable-next-line:no-console

src/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)