Skip to content

Make the default usage check the price status #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 129 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/client",
"version": "2.3.2",
"version": "2.4.0",
"description": "Client for consuming Pyth price data",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down Expand Up @@ -37,7 +37,7 @@
"typescript": "^4.2.4"
},
"dependencies": {
"@solana/web3.js": "^1.10.1",
"@solana/web3.js": "^1.30.2",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by: bump the solana web3 version. I fixed a memory leak in the websocket RPC library that solana web3 is using, so bumping this version incorporates that change.

"assert": "^2.0.0",
"buffer": "^6.0.1"
}
Expand Down
9 changes: 7 additions & 2 deletions src/example_usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ const pythConnection = new PythConnection(connection, pythPublicKey)
pythConnection.onPriceChange((product, price) => {
// sample output:
// SRM/USD: $8.68725 ±$0.0131
// tslint:disable-next-line:no-console
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`)
if (price.price && price.confidence) {
// tslint:disable-next-line:no-console
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`)
} else {
// tslint:disable-next-line:no-console
console.log(`${product.symbol}: price currently unavailable`)
}
})

// tslint:disable-next-line:no-console
Expand Down
30 changes: 25 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface Ema {
denominator: bigint
}

export interface PriceData extends Base, Price {
export interface PriceData extends Base {
priceType: number
exponent: number
numComponentPrices: number
Expand All @@ -90,6 +90,15 @@ export interface PriceData extends Base, Price {
drv3Component: bigint
drv3: number
priceComponents: PriceComponent[]
aggregate: Price,
// The current price and confidence. The typical use of this interface is to consume these two fields.
// If undefined, Pyth does not currently have price information for this product. This condition can
// happen for various reasons (e.g., US equity market is closed, or insufficient publishers), and your
// application should handle it gracefully. Note that other raw price information fields (such as
// aggregate.price) may be defined even if this is undefined; you most likely should not use those fields,
// as their value can be arbitrary when this is undefined.
price: number | undefined
confidence: number | undefined,
}

/** Parse data as a generic Pyth account. Use this method if you don't know the account type. */
Expand Down Expand Up @@ -257,7 +266,15 @@ export const parsePriceData = (data: Buffer): PriceData => {
// space for future derived values
const drv3Component = readBigInt64LE(data, 200)
const drv3 = Number(drv3Component) * 10 ** exponent
const aggregatePriceInfo = parsePriceInfo(data.slice(208, 240), exponent)
const aggregate = parsePriceInfo(data.slice(208, 240), exponent)

let price
let confidence
if (aggregate.status === 1) {
price = aggregate.price
confidence = aggregate.confidence
}

// price components - up to 32
const priceComponents: PriceComponent[] = []
let offset = 240
Expand All @@ -266,15 +283,16 @@ export const parsePriceData = (data: Buffer): PriceData => {
const publisher = PKorNull(data.slice(offset, offset + 32))
offset += 32
if (publisher) {
const aggregate = parsePriceInfo(data.slice(offset, offset + 32), exponent)
const componentAggregate = parsePriceInfo(data.slice(offset, offset + 32), exponent)
offset += 32
const latest = parsePriceInfo(data.slice(offset, offset + 32), exponent)
offset += 32
priceComponents.push({ publisher, aggregate, latest })
priceComponents.push({ publisher, aggregate: componentAggregate, latest })
} else {
shouldContinue = false
}
}

return {
magic,
version,
Expand All @@ -301,8 +319,10 @@ export const parsePriceData = (data: Buffer): PriceData => {
previousConfidence,
drv3Component,
drv3,
...aggregatePriceInfo,
aggregate,
priceComponents,
price,
confidence
}
}

Expand Down