Skip to content

Commit 9f2bff4

Browse files
author
Jayant Krishnamurthy
committed
example usage and doc cleanup
1 parent a99dccb commit 9f2bff4

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,27 @@ $ yarn add @pythnetwork/client
2424

2525
## Example Usage
2626

27-
This library provides a subscription model for consuming all price updates:
27+
This library provides a subscription model for consuming price updates:
2828

2929
```javascript
30-
import { Connection, PublicKey } from '@solana/web3.js'
31-
import { parseMappingData, parsePriceData, parseProductData } from '@pythnetwork/client'
32-
import { getPythProgramKeyForCluster } from '@pythnetwork/client'
33-
34-
const connection = new Connection(SOLANA_CLUSTER_URL)
35-
const pythPublicKey = getPythProgramKeyForCluster(CLUSTER)
36-
37-
const pythConnection = new PythConnection(connection, pythPublicKey)
30+
const pythConnection = new PythConnection(solanaWeb3Connection, getPythProgramKeyForCluster(solanaClusterName))
3831
pythConnection.onPriceChange((product, price) => {
3932
// sample output:
4033
// SRM/USD: $8.68725 ±$0.0131
4134
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`)
4235
})
4336

37+
// Start listening for price change events.
4438
pythConnection.start()
4539
```
4640

41+
The `onPriceChange` callback will be invoked every time a Pyth price gets updated.
42+
This callback gets two arguments:
43+
* `price` contains the official Pyth price and confidence, along with the component prices that were combined to produce this result.
44+
* `product` contains metadata about the price feed, such as the symbol (e.g., "BTC/USD") and the number of decimal points.
45+
46+
See `src/example_usage.ts` for a runnable example of the above usage.
47+
You can run this example with `npm run example`.
48+
4749
You may also register to specific account updates using `connection.onAccountChange` in the solana web3 API, then
4850
use the methods in `index.ts` to parse the on-chain data structures into Javascript-friendly objects.

package-lock.json

Lines changed: 2 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"prepublishOnly": "npm test && npm run lint",
1919
"preversion": "npm run lint",
2020
"version": "npm run format && git add -A src",
21-
"postversion": "git push && git push --tags"
21+
"postversion": "git push && git push --tags",
22+
"example": "npm run build && node lib/example_usage.js"
2223
},
2324
"keywords": [
2425
"pyth",

src/example_usage.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {Cluster, clusterApiUrl, Connection, PublicKey} from '@solana/web3.js'
2+
import { PythConnection } from './PythConnection'
3+
import { getPythProgramKeyForCluster } from './cluster'
4+
5+
const SOLANA_CLUSTER_NAME: Cluster = 'devnet'
6+
const connection = new Connection(clusterApiUrl(SOLANA_CLUSTER_NAME))
7+
const pythPublicKey = getPythProgramKeyForCluster(SOLANA_CLUSTER_NAME)
8+
9+
const pythConnection = new PythConnection(connection, pythPublicKey)
10+
pythConnection.onPriceChange((product, price) => {
11+
// sample output:
12+
// SRM/USD: $8.68725 ±$0.0131
13+
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence}`)
14+
})
15+
16+
console.log("Reading from Pyth price feed...")
17+
pythConnection.start()

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const PriceStatus = ['Unknown', 'Trading', 'Halted', 'Auction']
1111
export const CorpAction = ['NoCorpAct']
1212
export const PriceType = ['Unknown', 'Price']
1313
export const DeriveType = ['Unknown', 'TWAP', 'Volatility']
14-
export const AccountType = ['Mapping', 'Product', 'Price', 'Test']
14+
export const AccountType = ['Unknown', 'Mapping', 'Product', 'Price', 'Test']
1515

1616
/** Number of slots that can pass before a publisher's price is no longer included in the aggregate. */
1717
export const MAX_SLOT_DIFFERENCE = 25

0 commit comments

Comments
 (0)