@@ -22,13 +22,17 @@ $ yarn add @pythnetwork/client
22
22
23
23
## Example Usage
24
24
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.
26
26
27
- ``` javascript
27
+ ### Streaming updates
28
+
29
+ The websocket connection provides a subscription model for consuming price updates:
30
+
31
+ ``` typescript
28
32
const pythConnection = new PythConnection (solanaWeb3Connection , getPythProgramKeyForCluster (solanaClusterName ))
29
33
pythConnection .onPriceChange ((product , price ) => {
30
34
// sample output:
31
- // SRM/USD: $8.68725 ±$0.0131
35
+ // Crypto. SRM/USD: $8.68725 ±$0.0131 Status: Trading
32
36
console .log (` ${product .symbol }: $${price .price } \xB1 $${price .confidence } Status: ${PriceStatus [price .status ]} ` )
33
37
})
34
38
@@ -41,12 +45,33 @@ This callback gets two arguments:
41
45
* ` price ` contains the official Pyth price and confidence, along with the component prices that were combined to produce this result.
42
46
* ` product ` contains metadata about the price feed, such as the symbol (e.g., "BTC/USD") and the number of decimal points.
43
47
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 ` .
46
50
47
51
You may also register to specific account updates using ` connection.onAccountChange ` in the solana web3 API, then
48
52
use the methods in ` index.ts ` to parse the on-chain data structures into Javascript-friendly objects.
49
53
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
+
50
75
## Releases
51
76
52
77
In order to release a new version of this library and publish it to npm, follow these steps:
0 commit comments