Skip to content

pricefeed: Update Off chain guide #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 8 additions & 111 deletions pages/price-feeds/use-real-time-data/off-chain.mdx
Original file line number Diff line number Diff line change
@@ -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.
<Callout type="info" emoji="💡">
[`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.
</Callout>
Loading