Skip to content

Commit 417672b

Browse files
jayantkJayant Krishnamurthy
and
Jayant Krishnamurthy
authored
[easy] Improve error message (#14)
* Improve error message * update changelog * 2.5.1 Co-authored-by: Jayant Krishnamurthy <[email protected]>
1 parent d14e733 commit 417672b

7 files changed

+47
-25
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
### 2.5.1
4+
5+
### Changed
6+
7+
Improved error message when passing an invalid cluster name to `pythProgramKeyForCluster`
8+
39
### 2.5.0
410

511
### Changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/client",
3-
"version": "2.5.0",
3+
"version": "2.5.1",
44
"description": "Client for consuming Pyth price data",
55
"homepage": "https://pyth.network",
66
"main": "lib/index.js",

src/PythConnection.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import {Connection, PublicKey, clusterApiUrl, Cluster, Commitment, AccountInfo, Account} from '@solana/web3.js'
1+
import { Connection, PublicKey, clusterApiUrl, Cluster, Commitment, AccountInfo, Account } from '@solana/web3.js'
22
import {
3-
Base, Magic,
3+
Base,
4+
Magic,
45
parseMappingData,
56
parseBaseData,
67
parsePriceData,
7-
parseProductData, Price, PriceData, Product, ProductData,
8-
Version, AccountType,
8+
parseProductData,
9+
Price,
10+
PriceData,
11+
Product,
12+
ProductData,
13+
Version,
14+
AccountType,
915
} from './index'
1016

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

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

4957
const priceData = parsePriceData(account.data)
@@ -59,17 +67,17 @@ export class PythConnection {
5967
switch (AccountType[base.type]) {
6068
case 'Mapping':
6169
// We can skip these because we're going to get every account owned by this program anyway.
62-
break;
70+
break
6371
case 'Product':
6472
this.handleProductAccount(key, account)
65-
break;
73+
break
6674
case 'Price':
6775
if (!productOnly) {
6876
this.handlePriceAccount(key, account)
6977
}
70-
break;
78+
break
7179
case 'Test':
72-
break;
80+
break
7381
default:
7482
throw new Error(`Unknown account type: ${base.type}. Try upgrading pyth-client.`)
7583
}
@@ -115,4 +123,4 @@ export class PythConnection {
115123
// In the interim, delete callbacks.
116124
this.callbacks = []
117125
}
118-
}
126+
}

src/cluster.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import {Cluster, PublicKey} from '@solana/web3.js'
1+
import { Cluster, PublicKey } from '@solana/web3.js'
22

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

1010
/** Gets the public key of the Pyth program running on the given cluster. */
1111
export function getPythProgramKeyForCluster(cluster: Cluster): PublicKey {
12-
return new PublicKey(clusterToPythProgramKey[cluster]);
12+
if (clusterToPythProgramKey[cluster] !== undefined) {
13+
return new PublicKey(clusterToPythProgramKey[cluster])
14+
} else {
15+
throw new Error(
16+
`Invalid Solana cluster name: ${cluster}. Valid options are: ${JSON.stringify(
17+
Object.keys(clusterToPythProgramKey),
18+
)}`,
19+
)
20+
}
1321
}

src/example_usage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Cluster, clusterApiUrl, Connection, PublicKey} from '@solana/web3.js'
1+
import { Cluster, clusterApiUrl, Connection, PublicKey } from '@solana/web3.js'
22
import { PythConnection } from './PythConnection'
33
import { getPythProgramKeyForCluster } from './cluster'
44

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

2222
// tslint:disable-next-line:no-console
23-
console.log("Reading from Pyth price feed...")
23+
console.log('Reading from Pyth price feed...')
2424
pythConnection.start()

src/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ export interface PriceData extends Base {
9292
drv5Component: bigint
9393
drv5: number
9494
priceComponents: PriceComponent[]
95-
aggregate: Price,
95+
aggregate: Price
9696
// The current price and confidence. The typical use of this interface is to consume these two fields.
9797
// If undefined, Pyth does not currently have price information for this product. This condition can
9898
// happen for various reasons (e.g., US equity market is closed, or insufficient publishers), and your
9999
// application should handle it gracefully. Note that other raw price information fields (such as
100100
// aggregate.price) may be defined even if this is undefined; you most likely should not use those fields,
101101
// as their value can be arbitrary when this is undefined.
102102
price: number | undefined
103-
confidence: number | undefined,
103+
confidence: number | undefined
104104
}
105105

106106
/** Parse data as a generic Pyth account. Use this method if you don't know the account type. */
@@ -332,9 +332,9 @@ export const parsePriceData = (data: Buffer): PriceData => {
332332
aggregate,
333333
priceComponents,
334334
price,
335-
confidence
335+
confidence,
336336
}
337337
}
338338

339-
export { PythConnection } from './PythConnection';
340-
export { getPythProgramKeyForCluster } from './cluster';
339+
export { PythConnection } from './PythConnection'
340+
export { getPythProgramKeyForCluster } from './cluster'

0 commit comments

Comments
 (0)