@@ -30,19 +30,11 @@ const connection = new AptosPriceServiceConnection(
30
30
); // See Price Service endpoints section below for other endpoints
31
31
32
32
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
34
34
" 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b" , // BTC/USD price id in testnet
35
35
" 0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6" , // ETH/USD price id in testnet
36
36
];
37
37
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
38
// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target
47
39
// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should
48
40
// call the Pyth Contract with this data.
@@ -88,14 +80,44 @@ module example::your_module {
88
80
89
81
We strongly recommend reading our guide which explains [ how to work with Pyth price feeds] ( https://docs.pyth.network/consume-data/best-practices ) .
90
82
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
+
91
113
### Example
92
114
93
115
[ This example] ( ./src/examples/AptosRelay.ts ) shows how to update prices on an Aptos network. It does the following:
94
116
95
117
1 . Fetches update data from the Price Service for the given price feeds.
96
118
2 . Calls the Pyth Aptos contract with the update data.
97
119
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 :
99
121
100
122
``` bash
101
123
export APTOS_KEY = " 0x..." ;
0 commit comments