Skip to content

Commit b67e688

Browse files
committed
update readme
1 parent d6efa4f commit b67e688

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

apps/hermes/client/js/README.md

+30-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Price Service Client
1+
# Hermes Client
22

33
[Pyth Network](https://pyth.network/) provides real-time pricing data in a variety of asset classes, including cryptocurrency, equities, FX and commodities.
44
These prices are available either via HTTP or WebSocket from [Hermes](/apps/hermes).
@@ -9,70 +9,67 @@ This library is a client for interacting with Hermes, allowing your application
99
### npm
1010

1111
```
12-
$ npm install --save @pythnetwork/price-service-client
12+
$ npm install --save @pythnetwork/hermes-client
1313
```
1414

1515
### Yarn
1616

1717
```
18-
$ yarn add @pythnetwork/price-service-client
18+
$ yarn add @pythnetwork/hermes-client
1919
```
2020

2121
## Quickstart
2222

2323
Typical usage of the connection is along the following lines:
2424

2525
```typescript
26-
const connection = new HermesConnection("https://hermes.pyth.network", {
27-
priceFeedRequestConfig: {
28-
// Provide this option to retrieve signed price updates for on-chain contracts.
29-
// Ignore this option for off-chain use.
30-
binary: true,
31-
},
32-
}); // See Hermes endpoints section below for other endpoints
26+
const connection = new HermesConnection("https://hermes.pyth.network", {}); // See Hermes endpoints section below for other endpoints
3327

3428
const priceIds = [
3529
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids
3630
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id
3731
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id
3832
];
3933

40-
// Get the latest values of the price feeds as json objects.
41-
// If you set `binary: true` above, then this method also returns signed price updates for the on-chain Pyth contract.
42-
const currentPrices = await connection.getLatestPriceFeeds(priceIds);
34+
// Get price feeds
35+
const priceFeeds = await connection.getPriceFeeds("btc", "crypto");
36+
console.log(priceFeeds);
4337

44-
// You can also call this function to get price updates for the on-chain contract directly.
45-
const priceUpdateData = await connection.getLatestVaas(priceIds);
38+
// Latest price updates
39+
const priceUpdates = await connection.getLatestPriceUpdates(priceIds);
40+
console.log(priceUpdates);
4641
```
4742

48-
`HermesConnection` also allows subscribing to real-time price updates over a websocket connection:
43+
`HermesConnection` also allows subscribing to real-time price updates over a Server-Sent Events (SSE) connection:
4944

5045
```typescript
51-
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
52-
// priceFeed here is the same as returned by getLatestPriceFeeds above.
53-
// It will include signed price updates if the binary option was provided to the connection constructor.
54-
console.log(
55-
`Received update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan(60)}`
56-
);
57-
});
58-
59-
// When using the subscription, make sure to close the websocket upon termination to finish the process gracefully.
60-
setTimeout(() => {
61-
connection.closeWebSocket();
62-
}, 60000);
46+
// Streaming price updates
47+
const eventSource = await connection.getStreamingPriceUpdates(priceIds);
48+
49+
eventSource.onmessage = (event) => {
50+
console.log("Received price update:", event.data);
51+
};
52+
53+
eventSource.onerror = (error) => {
54+
console.error("Error receiving updates:", error);
55+
eventSource.close();
56+
};
57+
58+
await sleep(5000);
59+
60+
// To stop listening to the updates, you can call eventSource.close();
61+
console.log("Closing event source.");
62+
eventSource.close();
6363
```
6464

6565
### On-chain Applications
6666

6767
On-chain applications will need to submit the price updates returned by Hermes to the Pyth contract on their blockchain.
68-
These applications should pass the `binary: true` option to the constructor as shown above, to ensure that all methods on `HermesConnection` return the required information.
69-
This option will add a `vaa` field to `PriceFeed` that represents a signed price update.
70-
The `vaa` is a binary blob serialized as a base64 string.
71-
Depending on the blockchain, you may need to reformat this into hex or another format before submitting it to the Pyth contract.
68+
By default, these updates are returned as binary data and is serialized as either a base64 string or a hex string depending on the chosen encoding. This binary data will need to be submitted to the Pyth contract.
7269

7370
### Examples
7471

75-
The [HermesClient](./src/examples/HermesClient.ts) example demonstrates both the HTTP and websocket APIs described above.
72+
The [HermesClient](./src/examples/HermesClient.ts) example demonstrates both the examples above.
7673
You can run it with `npm run example`.
7774
A full command that prints BTC and ETH price feeds, in the testnet network, looks like so:
7875

0 commit comments

Comments
 (0)