Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 61dc1d1

Browse files
author
Jayant Krishnamurthy
committed
readme updates
1 parent 58d07e7 commit 61dc1d1

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

pyth-aptos-js/README.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,11 @@ const connection = new AptosPriceServiceConnection(
3030
); // See Price Service endpoints section below for other endpoints
3131

3232
const priceIds = [
33-
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids/#pyth-cross-chain-testnet
33+
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids#aptos-testnet
3434
"0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b", // BTC/USD price id in testnet
3535
"0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6", // ETH/USD price id in testnet
3636
];
3737

38-
// `getPythLatestPriceFeeds` returns a `PriceFeed` for each price id. It contains all information about a price and has
39-
// utility functions to get the current and exponentially-weighted moving average price, and other functionality.
40-
const priceFeeds = connection.getPythLatestPriceFeeds(priceIds);
41-
// Get the price if it is not older than 60 seconds from the current time.
42-
console.log(priceFeeds[0].getPriceNoOlderThan(60)); // Price { conf: '1234', expo: -8, price: '12345678' }
43-
// Get the exponentially-weighted moving average price if it is not older than 60 seconds from the current time.
44-
console.log(priceFeeds[1].getEmaPriceNoOlderThan(60));
45-
4638
// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target
4739
// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should
4840
// call the Pyth Contract with this data.
@@ -88,14 +80,44 @@ module example::your_module {
8880

8981
We strongly recommend reading our guide which explains [how to work with Pyth price feeds](https://docs.pyth.network/consume-data/best-practices).
9082

83+
### Off-chain prices
84+
85+
Many applications additionally need to display Pyth prices off-chain, for example, in their frontend application.
86+
The `AptosPriceServiceConnection` provides two different ways to fetch the current Pyth price.
87+
The code blocks below assume that the `connection` and `priceIds` objects have been initialized as shown above.
88+
The first method is a single shot query:
89+
90+
```typescript
91+
// `getLatestPriceFeeds` returns a `PriceFeed` for each price id. It contains all information about a price and has
92+
// utility functions to get the current and exponentially-weighted moving average price, and other functionality.
93+
const priceFeeds = await connection.getLatestPriceFeeds(priceIds);
94+
// Get the price if it is not older than 60 seconds from the current time.
95+
console.log(priceFeeds[0].getPriceNoOlderThan(60)); // Price { conf: '1234', expo: -8, price: '12345678' }
96+
// Get the exponentially-weighted moving average price if it is not older than 60 seconds from the current time.
97+
console.log(priceFeeds[1].getEmaPriceNoOlderThan(60));
98+
```
99+
100+
The object also supports a streaming websocket connection that allows you to subscribe to every new price update for a given feed.
101+
This method is useful if you want to show continuously updating real-time prices in your frontend:
102+
103+
```typescript
104+
// Subscribe to the price feeds given by `priceId`. The callback will be invoked every time the requested feed
105+
// gets a price update.
106+
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
107+
console.log("Received a new price feed update!");
108+
console.log(priceFeed.getPriceNoOlderThan(60));
109+
});
110+
```
111+
112+
91113
### Example
92114

93115
[This example](./src/examples/AptosRelay.ts) shows how to update prices on an Aptos network. It does the following:
94116

95117
1. Fetches update data from the Price Service for the given price feeds.
96118
2. Calls the Pyth Aptos contract with the update data.
97119

98-
You can run this example with `npm run example-relay`. A full command that updates BTC and ETH prices on the BNB Chain testnet network looks like so:
120+
You can run this example with `npm run example-relay`. A full command that updates BTC and ETH prices on the BNB Chain testnet network looks like this:
99121

100122
```bash
101123
export APTOS_KEY = "0x...";

pyth-evm-js/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const connection = new EvmPriceServiceConnection(
3030
); // See Price Service endpoints section below for other endpoints
3131

3232
const priceIds = [
33-
// You can find the ids of prices at https://pyth.network/developers/price-feeds#binance-smart-chain-testnet
33+
// You can find the ids of prices at https://pyth.network/developers/price-feed-ids#pyth-evm-testnet
3434
"0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b", // BTC/USD price id in testnet
3535
"0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6", // ETH/USD price id in testnet
3636
];

0 commit comments

Comments
 (0)