Skip to content

Commit

Permalink
2.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayant Krishnamurthy committed Nov 10, 2021
1 parent c795a1f commit 32b9e69
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/client",
"version": "2.5.0",
"version": "2.5.1",
"description": "Client for consuming Pyth price data",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down
30 changes: 19 additions & 11 deletions src/PythConnection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {Connection, PublicKey, clusterApiUrl, Cluster, Commitment, AccountInfo, Account} from '@solana/web3.js'
import { Connection, PublicKey, clusterApiUrl, Cluster, Commitment, AccountInfo, Account } from '@solana/web3.js'
import {
Base, Magic,
Base,
Magic,
parseMappingData,
parseBaseData,
parsePriceData,
parseProductData, Price, PriceData, Product, ProductData,
Version, AccountType,
parseProductData,
Price,
PriceData,
Product,
ProductData,
Version,
AccountType,
} from './index'

const ONES = '11111111111111111111111111111111'
Expand All @@ -31,7 +37,7 @@ export class PythConnection {
callbacks: PythPriceCallback[] = []

private handleProductAccount(key: PublicKey, account: AccountInfo<Buffer>) {
const {priceAccountKey, type, product} = parseProductData(account.data)
const { priceAccountKey, type, product } = parseProductData(account.data)
this.productAccountKeyToProduct[key.toString()] = product
if (priceAccountKey.toString() !== ONES) {
this.priceAccountKeyToProductAccountKey[priceAccountKey.toString()] = key.toString()
Expand All @@ -43,7 +49,9 @@ export class PythConnection {
if (product === undefined) {
// This shouldn't happen since we're subscribed to all of the program's accounts,
// but let's be good defensive programmers.
throw new Error('Got a price update for an unknown product. This is a bug in the library, please report it to the developers.')
throw new Error(
'Got a price update for an unknown product. This is a bug in the library, please report it to the developers.',
)
}

const priceData = parsePriceData(account.data)
Expand All @@ -59,17 +67,17 @@ export class PythConnection {
switch (AccountType[base.type]) {
case 'Mapping':
// We can skip these because we're going to get every account owned by this program anyway.
break;
break
case 'Product':
this.handleProductAccount(key, account)
break;
break
case 'Price':
if (!productOnly) {
this.handlePriceAccount(key, account)
}
break;
break
case 'Test':
break;
break
default:
throw new Error(`Unknown account type: ${base.type}. Try upgrading pyth-client.`)
}
Expand Down Expand Up @@ -115,4 +123,4 @@ export class PythConnection {
// In the interim, delete callbacks.
this.callbacks = []
}
}
}
14 changes: 9 additions & 5 deletions src/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {Cluster, PublicKey} from '@solana/web3.js'
import { Cluster, PublicKey } from '@solana/web3.js'

/** Mapping from solana clusters to the public key of the pyth program. */
const clusterToPythProgramKey: Record<Cluster, string> = {
'mainnet-beta': 'FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH',
'devnet': 'gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s',
'testnet': '8tfDNiaEyrV6Q1U4DEXrEigs9DoDtkugzFbybENEbCDz',
devnet: 'gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s',
testnet: '8tfDNiaEyrV6Q1U4DEXrEigs9DoDtkugzFbybENEbCDz',
}

/** Gets the public key of the Pyth program running on the given cluster. */
export function getPythProgramKeyForCluster(cluster: Cluster): PublicKey {
if (clusterToPythProgramKey[cluster] !== undefined) {
return new PublicKey(clusterToPythProgramKey[cluster]);
return new PublicKey(clusterToPythProgramKey[cluster])
} else {
throw new Error(`Invalid Solana cluster name: ${cluster}. Valid options are: ${JSON.stringify(Object.keys(clusterToPythProgramKey))}`)
throw new Error(
`Invalid Solana cluster name: ${cluster}. Valid options are: ${JSON.stringify(
Object.keys(clusterToPythProgramKey),
)}`,
)
}
}
4 changes: 2 additions & 2 deletions src/example_usage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Cluster, clusterApiUrl, Connection, PublicKey} from '@solana/web3.js'
import { Cluster, clusterApiUrl, Connection, PublicKey } from '@solana/web3.js'
import { PythConnection } from './PythConnection'
import { getPythProgramKeyForCluster } from './cluster'

Expand All @@ -20,5 +20,5 @@ pythConnection.onPriceChange((product, price) => {
})

// tslint:disable-next-line:no-console
console.log("Reading from Pyth price feed...")
console.log('Reading from Pyth price feed...')
pythConnection.start()
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ export interface PriceData extends Base {
drv5Component: bigint
drv5: number
priceComponents: PriceComponent[]
aggregate: Price,
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,
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 @@ -332,9 +332,9 @@ export const parsePriceData = (data: Buffer): PriceData => {
aggregate,
priceComponents,
price,
confidence
confidence,
}
}

export { PythConnection } from './PythConnection';
export { getPythProgramKeyForCluster } from './cluster';
export { PythConnection } from './PythConnection'
export { getPythProgramKeyForCluster } from './cluster'

0 comments on commit 32b9e69

Please sign in to comment.