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

Commit 9a0ba57

Browse files
author
Jayant Krishnamurthy
committed
offchain prices
1 parent 61dc1d1 commit 9a0ba57

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

pyth-aptos-js/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module example::your_module {
6565
6666
public fun do_something(user: &signer, pyth_update_data: vector<vector<u8>>) {
6767
68-
// First update the Pyth price feeds
68+
// First update the Pyth price feeds. The user pays the fee for the update.
6969
let coins = coin::withdraw(user, pyth::get_update_fee());
7070
pyth::update_price_feeds(pyth_update_data, coins);
7171
@@ -85,7 +85,7 @@ We strongly recommend reading our guide which explains [how to work with Pyth pr
8585
Many applications additionally need to display Pyth prices off-chain, for example, in their frontend application.
8686
The `AptosPriceServiceConnection` provides two different ways to fetch the current Pyth price.
8787
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:
88+
The first method is a single-shot query:
8989

9090
```typescript
9191
// `getLatestPriceFeeds` returns a `PriceFeed` for each price id. It contains all information about a price and has
@@ -104,11 +104,14 @@ This method is useful if you want to show continuously updating real-time prices
104104
// Subscribe to the price feeds given by `priceId`. The callback will be invoked every time the requested feed
105105
// gets a price update.
106106
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
107-
console.log("Received a new price feed update!");
108-
console.log(priceFeed.getPriceNoOlderThan(60));
107+
console.log(`Received update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan(60)}`);
109108
});
110-
```
111109

110+
// When using the subscription, make sure to close the websocket upon termination to finish the process gracefully.
111+
setTimeout(() => {
112+
connection.closeWebSocket();
113+
}, 60000);
114+
```
112115

113116
### Example
114117

pyth-evm-js/README.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,6 @@ const priceIds = [
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-
46-
// By subscribing to the price feeds you can get their updates in real-time.
47-
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
48-
console.log("Received a new price feed update!");
49-
console.log(priceFeed.getPriceNoOlderThan(60));
50-
});
51-
52-
// When using subscription, make sure to close the websocket upon termination to finish the process gracefully.
53-
setTimeout(() => {
54-
connection.closeWebSocket();
55-
}, 60000);
56-
5738
// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target
5839
// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should
5940
// call the Pyth Contract with this data.
@@ -104,6 +85,39 @@ contract SomeContract {
10485

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

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

109123
There are two examples in [examples](./src/examples/).

0 commit comments

Comments
 (0)