|
1 | | -# How to Use Real-Time Data in Off-Chain Applications |
2 | | - |
3 | | -This guide explains how to use the [typescript SDK client](https://github.com/pyth-network/pyth-crosschain/tree/main/price_service/client/js) to fetch the latest prices and subscribe to real-time price updates in off-chain applications. |
4 | | - |
5 | | -## Installation |
6 | | - |
7 | | -The Pyth SDK can be installed using npm or yarn: |
8 | | - |
9 | | -### npm |
10 | | - |
11 | | -```bash copy |
12 | | -npm install --save @pythnetwork/price-service-client |
13 | | -``` |
14 | | - |
15 | | -### Yarn |
16 | | - |
17 | | -```bash copy |
18 | | -yarn add @pythnetwork/price-service-client |
19 | | -``` |
20 | | - |
21 | | -## Usage |
22 | | - |
23 | | -Typical usage of the Pyth SDK involves creating a `PriceServiceConnection` instance, by passing the URL of the [Hermes](https://hermes.pyth.network/docs/#/) service to the constructor. |
24 | | -You can then use the `getLatestPriceFeeds` method to fetch the latest prices of the specified price feeds: |
25 | | - |
26 | | -```typescript copy |
27 | | -// Get the Stable Hermes service URL from https://docs.pyth.network/price-feeds/api-instances-and-providers/hermes |
28 | | -const connection = new PriceServiceConnection("https://hermes.pyth.network"); |
29 | | - |
30 | | -const priceIds = [ |
31 | | - "0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id |
32 | | - "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id |
33 | | -]; |
| 1 | +import { Callout } from "nextra/components"; |
34 | 2 |
|
35 | | -// Get the latest values of the price feeds as JSON objects. |
36 | | -const currentPrices = await connection.getLatestPriceFeeds(priceIds); |
37 | | -console.log(currentPrices); |
38 | | -``` |
39 | | - |
40 | | -The `getLatestPriceFeeds` method returns an array of `PriceFeed` objects. Each `PriceFeed` object contains the latest price, the Exponential Moving Average (EMA) price, and other metadata: |
41 | | - |
42 | | -```bash |
43 | | -[ |
44 | | - PriceFeed { |
45 | | - emaPrice: Price { |
46 | | - conf: '3450227100', |
47 | | - expo: -8, |
48 | | - price: '6610104500000', |
49 | | - publishTime: 1715870606 |
50 | | - }, |
51 | | - id: 'e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43', |
52 | | - metadata: undefined, |
53 | | - vaa: undefined, |
54 | | - price: Price { |
55 | | - conf: '3315255511', |
56 | | - expo: -8, |
57 | | - price: '6619626406527', |
58 | | - publishTime: 1715870606 |
59 | | - } |
60 | | - }, |
61 | | - PriceFeed { |
62 | | - emaPrice: Price { |
63 | | - conf: '266713535', |
64 | | - expo: -8, |
65 | | - price: '297876550000', |
66 | | - publishTime: 1715870606 |
67 | | - }, |
68 | | - id: 'ff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace', |
69 | | - metadata: undefined, |
70 | | - vaa: undefined, |
71 | | - price: Price { |
72 | | - conf: '296893358', |
73 | | - expo: -8, |
74 | | - price: '296499502000', |
75 | | - publishTime: 1715870606 |
76 | | - } |
77 | | - } |
78 | | -] |
79 | | -``` |
80 | | - |
81 | | -You can also subscribe to real-time price updates over a WebSocket connection using the `subscribePriceFeedUpdates` method: |
82 | | - |
83 | | -```typescript copy |
84 | | -// priceIds here is the one declared in the above code snippet. |
85 | | -const priceFeeds = await connection.getLatestPriceFeeds(priceIds); |
86 | | - |
87 | | -connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => { |
88 | | - // priceFeed here is the same as returned by getLatestPriceFeeds above. |
89 | | - console.log( |
90 | | - `Received an update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan( |
91 | | - 60 |
92 | | - )}` |
93 | | - ); |
94 | | -}); |
95 | | - |
96 | | -// When using the subscription, make sure to close the WebSocket upon termination to finish the process gracefully. |
97 | | -setTimeout(() => { |
98 | | - connection.closeWebSocket(); |
99 | | -}, 60000); |
100 | | -``` |
101 | | - |
102 | | -## Additional Resources |
103 | | - |
104 | | -You may find these additional resources helpful. |
105 | | - |
106 | | -### Examples |
107 | | - |
108 | | -This [example](https://github.com/pyth-network/pyth-crosschain/blob/main/price_service/client/js/src/examples/PriceServiceClient.ts) demonstrates how to use the Pyth SDK to fetch prices. |
| 3 | +# How to Use Real-Time Data in Off-Chain Applications |
109 | 4 |
|
110 | | -### Hermes API Reference |
| 5 | +This guide explains how to fetch the latest prices and subscribe to real-time price updates in off-chain applications. |
111 | 6 |
|
112 | | -The [Hermes API](https://hermes.pyth.network/docs/#/) reference lets you interactively explore the complete API of the Pyth Hermes service. |
| 7 | +<Callout type="info" emoji="💡"> |
| 8 | + [`price-service-sdk`](https://github.com/pyth-network/pyth-crosschain/tree/main/price_service/client/js) was is deprecated and replaced by the [`hermes-client`](https://github.com/pyth-network/pyth-crosschain/tree/main/apps/hermes/client/js). |
| 9 | + It can be used for fetching prices for off-chain applications as well as fetching price updates. |
113 | 10 |
|
114 | | -### Price Feed IDs |
| 11 | +Please refer to the [fetch-price-updates](../fetch-price-updates) guide for the details. |
115 | 12 |
|
116 | | -The [Price Feed IDs](https://pyth.network/developers/price-feed-ids) lists the price feeds available on the Pyth network. |
| 13 | +</Callout> |
0 commit comments