Skip to content

Commit e54da23

Browse files
authored
chore(pricefeed) User hermes Client (#436)
1 parent 8a68dca commit e54da23

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

pages/price-feeds/fetch-price-updates.mdx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -110,46 +110,46 @@ data:{"binary":{"encoding":"hex","data":["504e41550100000003b801000000040d00c225
110110

111111
## SDK
112112

113-
Pyth provides a typescript SDK to fetch price updates.
114-
The `PriceServiceConnection` class in this SDK connects to Hermes to fetch and stream price updates.
113+
Pyth provides a typescript SDK for Hermes to fetch price updates.
114+
The [`HermesClient`](https://github.com/pyth-network/pyth-crosschain/blob/main/apps/hermes/client/js/src/HermesClient.ts#L41) class in this [SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/apps/hermes/client/js) connects to Hermes to fetch and stream price updates.
115115

116116
```typescript copy
117-
const connection = new PriceServiceConnection("https://hermes.pyth.network", {
118-
priceFeedRequestConfig: {
119-
// Provide this option to retrieve binary price updates for on-chain contracts.
120-
// Ignore this option for off-chain use.
121-
binary: true,
122-
},
123-
});
117+
const connection = new HermesClient("https://hermes.pyth.network", {});
124118

125119
const priceIds = [
126-
// You can find the IDs of prices at https://pyth.network/developers/price-feed-ids
120+
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids
127121
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id
128122
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id
129123
];
130124

131-
// Get the latest price updates for the given price IDs.
132-
// If you set `binary: true` above, then this method also returns signed price updates for the on-chain Pyth contract.
133-
const priceUpdates = await connection.getLatestVaas(priceIds);
125+
// Get price feeds
126+
// You can also fetch price feeds for other assets by specifying the asset name and asset class.
127+
const priceFeeds = await connection.getPriceFeeds("btc", "crypto");
128+
console.log(priceFeeds);
129+
130+
// Latest price updates
131+
const priceUpdates = await connection.getLatestPriceUpdates(priceIds);
132+
console.log(priceUpdates);
134133
```
135134

136-
`PriceServiceConnection` also allows subscribing to price updates via a WebSocket connection:
135+
`HermesClient` also allows subscribing to real-time price updates over a Server-Sent Events (SSE) connection:
137136

138137
```typescript copy
139-
// priceIds here is the one declared in the above code snippet.
140-
const priceFeeds = await connection.getLatestPriceFeeds(priceIds);
141-
142-
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
143-
// It will include signed price updates if the binary option was provided to the connection constructor.
144-
console.log(
145-
`Received an update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan(
146-
60
147-
)}`
148-
);
149-
});
150-
151-
// When using the subscription, make sure to close the websocket upon termination to finish the process gracefully.
152-
setTimeout(() => {
153-
connection.closeWebSocket();
154-
}, 60000);
138+
// Streaming price updates
139+
const eventSource = await connection.getStreamingPriceUpdates(priceIds);
140+
141+
eventSource.onmessage = (event) => {
142+
console.log("Received price update:", event.data);
143+
};
144+
145+
eventSource.onerror = (error) => {
146+
console.error("Error receiving updates:", error);
147+
eventSource.close();
148+
};
149+
150+
await sleep(5000);
151+
152+
// To stop listening to the updates, you can call eventSource.close();
153+
console.log("Closing event source.");
154+
eventSource.close();
155155
```

0 commit comments

Comments
 (0)