@@ -35,25 +35,6 @@ const priceIds = [
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
- // 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
-
57
38
// In order to use Pyth prices in your protocol you need to submit the price update data to Pyth contract in your target
58
39
// chain. `getPriceUpdateData` creates the update data which can be submitted to your contract. Then your contract should
59
40
// call the Pyth Contract with this data.
@@ -104,6 +85,39 @@ contract SomeContract {
104
85
105
86
We strongly recommend reading our guide which explains [ how to work with Pyth price feeds] ( https://docs.pyth.network/consume-data/best-practices ) .
106
87
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
+
107
121
### Examples
108
122
109
123
There are two examples in [ examples] ( ./src/examples/ ) .
0 commit comments