diff --git a/pages/price-feeds/use-real-time-data/off-chain.mdx b/pages/price-feeds/use-real-time-data/off-chain.mdx index ab0bdbf6..e0cdc788 100644 --- a/pages/price-feeds/use-real-time-data/off-chain.mdx +++ b/pages/price-feeds/use-real-time-data/off-chain.mdx @@ -1,116 +1,13 @@ -# How to Use Real-Time Data in Off-Chain Applications - -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. - -## Installation - -The Pyth SDK can be installed using npm or yarn: - -### npm - -```bash copy -npm install --save @pythnetwork/price-service-client -``` - -### Yarn - -```bash copy -yarn add @pythnetwork/price-service-client -``` - -## Usage - -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. -You can then use the `getLatestPriceFeeds` method to fetch the latest prices of the specified price feeds: - -```typescript copy -// Get the Stable Hermes service URL from https://docs.pyth.network/price-feeds/api-instances-and-providers/hermes -const connection = new PriceServiceConnection("https://hermes.pyth.network"); - -const priceIds = [ - "0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id - "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id -]; +import { Callout } from "nextra/components"; -// Get the latest values of the price feeds as JSON objects. -const currentPrices = await connection.getLatestPriceFeeds(priceIds); -console.log(currentPrices); -``` - -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: - -```bash -[ - PriceFeed { - emaPrice: Price { - conf: '3450227100', - expo: -8, - price: '6610104500000', - publishTime: 1715870606 - }, - id: 'e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43', - metadata: undefined, - vaa: undefined, - price: Price { - conf: '3315255511', - expo: -8, - price: '6619626406527', - publishTime: 1715870606 - } - }, - PriceFeed { - emaPrice: Price { - conf: '266713535', - expo: -8, - price: '297876550000', - publishTime: 1715870606 - }, - id: 'ff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace', - metadata: undefined, - vaa: undefined, - price: Price { - conf: '296893358', - expo: -8, - price: '296499502000', - publishTime: 1715870606 - } - } -] -``` - -You can also subscribe to real-time price updates over a WebSocket connection using the `subscribePriceFeedUpdates` method: - -```typescript copy -// priceIds here is the one declared in the above code snippet. -const priceFeeds = await connection.getLatestPriceFeeds(priceIds); - -connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => { - // priceFeed here is the same as returned by getLatestPriceFeeds above. - console.log( - `Received an update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan( - 60 - )}` - ); -}); - -// When using the subscription, make sure to close the WebSocket upon termination to finish the process gracefully. -setTimeout(() => { - connection.closeWebSocket(); -}, 60000); -``` - -## Additional Resources - -You may find these additional resources helpful. - -### Examples - -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. +# How to Use Real-Time Data in Off-Chain Applications -### Hermes API Reference +This guide explains how to fetch the latest prices and subscribe to real-time price updates in off-chain applications. -The [Hermes API](https://hermes.pyth.network/docs/#/) reference lets you interactively explore the complete API of the Pyth Hermes service. + + [`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). + It can be used for fetching prices for off-chain applications as well as fetching price updates. -### Price Feed IDs +Please refer to the [fetch-price-updates](../fetch-price-updates) guide for the details. -The [Price Feed IDs](https://pyth.network/developers/price-feed-ids) lists the price feeds available on the Pyth network. +