Skip to content

Commit c6e710b

Browse files
authored
(chore):refactor-offchain-guide (#302)
* update * requested changes
1 parent 0cbcbc3 commit c6e710b

File tree

1 file changed

+116
-32
lines changed

1 file changed

+116
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,116 @@
1-
import { Tab, Tabs } from "nextra-theme-docs";
2-
3-
# Off-chain Applications
4-
5-
We provide SDKs in various programming languages that allow you to read the values of Pyth price feeds in off-chain applications, such as web frontends:
6-
7-
<Tabs items={['JavaScript', 'Rust', 'Python', 'Go']}>
8-
<Tab>
9-
There are different javascript SDKs for different purposes.
10-
11-
If you are developing an application that runs on any blockchain except Solana, then the javascript SDK for that blockchain supports querying and streaming live Pyth prices.
12-
You can find the code for this SDK in the [pyth-crosschain](https://github.com/pyth-network/pyth-crosschain) repository, under `target_chains/<chain name>/sdk/js`.
13-
These SDKs also support [pulling price updates](../pythnet-price-feeds/pull-updates) on to your target blockchain.
14-
An example for querying and streaming prices using these SDKs is shown [here](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/sdk/js#off-chain-prices).
15-
16-
If you are developing an application for Solana, or without any blockchain whatsoever, then the [@pythnetwork/client](https://www.npmjs.com/package/@pythnetwork/client) npm package can be used to consume Pyth prices inside off-chain JavaScript programs.
17-
An example can be found [here](https://github.com/pyth-network/pyth-client-js#example-usage).
18-
19-
</Tab>
20-
<Tab>
21-
The [pyth-sdk-solana crate](https://crates.io/crates/pyth-sdk-solana) can be used to consume Pyth prices inside your off-chain Rust programs. An example can be found [here](https://github.com/pyth-network/pyth-sdk-rs/blob/main/pyth-sdk-solana/examples/eth_price.rs).
22-
</Tab>
23-
<Tab>
24-
The [pyth-client-py](https://github.com/pyth-network/pyth-client-py) Python library can be used to consume Pyth prices inside your off-chain Python programs. An example can be found [here](https://github.com/pyth-network/pyth-client-py/blob/main/examples/read_one_price_feed.py).
25-
</Tab>
26-
<Tab>
27-
The [Blockdaemon pyth-go](https://github.com/Blockdaemon/pyth-go) client can be used to consume Pyth prices inside your off-chain Go programs.
28-
</Tab>
29-
</Tabs>
30-
31-
You can find an in-depth explanation from one of our contributors, Daniel:
32-
[How to Build with Pyth Data Off-chain: Pyth Tutorials](https://youtu.be/YgD4IWTKD2c)
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+
];
34+
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.
109+
110+
### Hermes API Reference
111+
112+
The [Hermes API](https://hermes.pyth.network/docs/#/) reference lets you interactively explore the complete API of the Pyth Hermes service.
113+
114+
### Price Feed IDs
115+
116+
The [Price Feed Ids](https://pyth.network/developers/price-feed-ids) lists the price feeds available on the Pyth network.

0 commit comments

Comments
 (0)