Skip to content

Commit af67760

Browse files
author
Jayant Krishnamurthy
committed
Document PythHttpClient
1 parent b8f330a commit af67760

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ $ yarn add @pythnetwork/client
2222

2323
## Example Usage
2424

25-
This library provides a subscription model for consuming price updates:
25+
This library lets you consume prices in two different ways: you can either get continuously-streaming price updates via a websocket connection, or send one-off requests every time you want the current price.
2626

27-
```javascript
27+
### Streaming updates
28+
29+
The websocket connection provides a subscription model for consuming price updates:
30+
31+
```typescript
2832
const pythConnection = new PythConnection(solanaWeb3Connection, getPythProgramKeyForCluster(solanaClusterName))
2933
pythConnection.onPriceChange((product, price) => {
3034
// sample output:
31-
// SRM/USD: $8.68725 ±$0.0131
35+
// Crypto.SRM/USD: $8.68725 ±$0.0131 Status: Trading
3236
console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence} Status: ${PriceStatus[price.status]}`)
3337
})
3438

@@ -41,12 +45,33 @@ This callback gets two arguments:
4145
* `price` contains the official Pyth price and confidence, along with the component prices that were combined to produce this result.
4246
* `product` contains metadata about the price feed, such as the symbol (e.g., "BTC/USD") and the number of decimal points.
4347

44-
See `src/example_usage.ts` for a runnable example of the above usage.
45-
You can run this example with `npm run example`.
48+
See `src/example_ws_usage.ts` for a runnable example of the above usage.
49+
You can run this example with `npm run ws_example`.
4650

4751
You may also register to specific account updates using `connection.onAccountChange` in the solana web3 API, then
4852
use the methods in `index.ts` to parse the on-chain data structures into Javascript-friendly objects.
4953

54+
### Request an update
55+
56+
The request model allows you to send one-off HTTP requests to get the current price without subscribing to ongoing updates:
57+
58+
```typescript
59+
const pythClient = new PythHttpClient(connection, pythPublicKey);
60+
const data = await pythClient.getData();
61+
62+
for (let symbol of data.symbols) {
63+
const price = data.productPrice.get(symbol)!;
64+
// Sample output:
65+
// Crypto.SRM/USD: $8.68725 ±$0.0131 Status: Trading
66+
console.log(`${symbol}: $${price.price} \xB1$${price.confidence} Status: ${PriceStatus[price.status]}`)
67+
}
68+
```
69+
70+
The `getData` function will fetch all information about every product listed on Pyth.
71+
This includes the current price as well as metadata, such as the base and quote currencies.
72+
See `src/example_http_usage.ts` for a runnable example of the above usage.
73+
You can run this example with `npm run http_example`.
74+
5075
## Releases
5176

5277
In order to release a new version of this library and publish it to npm, follow these steps:

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"preversion": "npm run lint",
2020
"version": "npm run format && git add -A src",
2121
"postversion": "git push && git push --tags",
22-
"example": "npm run build && node lib/example_usage.js"
22+
"ws_example": "npm run build && node lib/example_ws_usage.js",
23+
"http_example": "npm run build && node lib/example_http_usage.js"
2324
},
2425
"keywords": [
2526
"pyth",

src/PythHttpClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { Product, PriceData, parseProductData, parsePriceData, parseBaseData, Ac
33

44
export interface PythHttpClientResult {
55
assetTypes: string[]
6+
/** The name of each product, e.g., "Crypto.BTC/USD" */
67
symbols: string[]
78
products: Product[]
9+
/** Metadata for each product. */
810
productFromSymbol: Map<string, Product>
11+
/** The current price of each product. */
912
productPrice: Map<string, PriceData>
1013
prices: PriceData[]
1114
}
@@ -40,7 +43,7 @@ export class PythHttpClient {
4043
// Retrieve data from blockchain
4144
const accountList = await this.connection.getProgramAccounts(this.pythProgramKey, this.commitment)
4245

43-
// Popolate producs and prices
46+
// Populate products and prices
4447
const priceDataQueue = new Array<PriceData>()
4548
const productAccountKeyToProduct = new Map<string, Product>()
4649
const currentSlot = await this.connection.getSlot(this.commitment)

src/example_http_usage.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Cluster, clusterApiUrl, Connection} from '@solana/web3.js'
2+
import {getPythProgramKeyForCluster} from './cluster'
3+
import {PriceStatus, PythHttpClient} from '.'
4+
5+
const SOLANA_CLUSTER_NAME: Cluster = 'mainnet-beta'
6+
const connection = new Connection(clusterApiUrl(SOLANA_CLUSTER_NAME))
7+
const pythPublicKey = getPythProgramKeyForCluster(SOLANA_CLUSTER_NAME)
8+
9+
async function runQuery(): Promise<void> {
10+
const pythClient = new PythHttpClient(connection, pythPublicKey);
11+
const data = await pythClient.getData();
12+
13+
for (let symbol of data.symbols) {
14+
const price = data.productPrice.get(symbol)!;
15+
16+
if (price.price && price.confidence) {
17+
// tslint:disable-next-line:no-console
18+
console.log(`${symbol}: $${price.price} \xB1$${price.confidence}`)
19+
} else {
20+
// tslint:disable-next-line:no-console
21+
console.log(`${symbol}: price currently unavailable. status is ${PriceStatus[price.status]}`)
22+
}
23+
}
24+
}
25+
26+
runQuery()

src/example_usage.ts renamed to src/example_ws_usage.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Cluster, clusterApiUrl, Connection, PublicKey } from '@solana/web3.js'
2-
import { PythConnection } from './PythConnection'
3-
import { getPythProgramKeyForCluster } from './cluster'
4-
import { PriceStatus } from '.'
1+
import {Cluster, clusterApiUrl, Connection} from '@solana/web3.js'
2+
import {PythConnection} from './PythConnection'
3+
import {getPythProgramKeyForCluster} from './cluster'
4+
import {PriceStatus} from '.'
55

6-
const SOLANA_CLUSTER_NAME: Cluster = 'devnet'
6+
const SOLANA_CLUSTER_NAME: Cluster = 'mainnet-beta'
77
const connection = new Connection(clusterApiUrl(SOLANA_CLUSTER_NAME))
88
const pythPublicKey = getPythProgramKeyForCluster(SOLANA_CLUSTER_NAME)
99

0 commit comments

Comments
 (0)