Skip to content

Commit c39a0e2

Browse files
authored
refactor: deprecate getPrice function (#393)
1 parent 17e44ef commit c39a0e2

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed

pages/price-feeds/api-reference/evm.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ Users of the Pyth contract will typically need to perform two operations:
1111
for verification. This operation makes the price available for on-chain use.
1212
You will typically call [updatePriceFeeds](evm/update-price-feeds) to do this.
1313
- Read the on-chain price -- After updating the price, your on-chain contract can call one of the
14-
many getter functions on the contract to get the price. See [getPrice](evm/get-price) and its variants
14+
many getter functions on the contract to get the price. See [getPriceNoOlderThan](evm/get-price-no-older-than) and its variants
1515
for more information.

pages/price-feeds/api-reference/evm/_meta.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
"type": "separator"
1111
},
1212

13-
"get-price": "getPrice",
13+
"get-price": "getPrice (deprecated)",
1414
"get-price-unsafe": "getPriceUnsafe",
1515
"get-price-no-older-than": "getPriceNoOlderThan",
1616
"get-ema-price": "getEmaPrice",
1717
"get-ema-price-unsafe": "getEmaPriceUnsafe",
1818
"get-ema-price-no-older-than": "getEmaPriceNoOlderThan",
1919
"get-update-fee": "getUpdateFee",
20-
"get-valid-time-period": "getValidTimePeriod",
20+
"get-valid-time-period": "getValidTimePeriod (deprecated)",
2121
"parse-price-feed-updates": "parsePriceFeedUpdates",
2222
"parse-price-feed-updates-unique": "parsePriceFeedUpdatesUnique",
2323
"update-price-feeds": "updatePriceFeeds",

pages/price-feeds/api-reference/evm/get-price.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import DynamicCode from "../../../../components/DynamicCode";
77
import EvmCall from "../../../../components/EvmCall";
88
import { Tab, Tabs } from "nextra-theme-docs";
99

10-
# Get Price
10+
# Get Price (Deprecated)
11+
12+
**This function is deprecated, please consider using `getPriceNoOlderThan` or `getPriceUnsafe` instead.**
1113

1214
Get the latest price and confidence interval for the requested price feed id.
1315
The price feed id is a 32-byte id written as a hexadecimal string; see the [price feed ids](https://pyth.network/developers/price-feed-ids) page to look up the id for a given symbol.

pages/price-feeds/api-reference/evm/get-valid-time-period.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import DynamicCode from "../../../../components/DynamicCode";
22
import EvmCall from "../../../../components/EvmCall";
33
import { Tab, Tabs } from "nextra-theme-docs";
44

5-
# Get Valid Time Period
5+
# Get Valid Time Period (deprecated)
6+
7+
**This function is deprecated. Please consider using `getPriceNoOlderThan` with your own acceptable staleness time period.**
68

79
Get the default valid time period in seconds.
810
This quantity is the maximum age of price updates returned by functions like [getPrice](get-price) and [getEmaPrice](get-ema-price);

pages/price-feeds/api-reference/evm/parse-price-feed-updates.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Tab, Tabs } from "nextra-theme-docs";
1111

1212
Parse `updateData` and return the price feeds for the given `priceIds` within, if they are all published between `minPublishTime` and `maxPublishTime` (`minPublishTime <= publishTime <= maxPublishTime`).
1313
Use this function if you want to use a Pyth price for a fixed time and not the most recent price;
14-
otherwise, consider using [updatePriceFeeds](update-price-feeds) followed by [getPrice](get-price) or one of its variants.
14+
otherwise, consider using [updatePriceFeeds](update-price-feeds) followed by [getPriceNoOlderThan](get-price-no-older-than) or one of its variants.
1515
Unlike `updatePriceFeeds`, calling this function will not update the on-chain price.
1616

1717
If you need to make sure the price update is the earliest update after the `minPublishTime` consider using [parsePriceFeedUpdatesUnique](parse-price-feed-updates-unique).

pages/price-feeds/create-your-first-pyth-app/evm/part-1.mdx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ contract MyFirstPythContract {
121121
// ... other functions omitted
122122
123123
function mint() public payable {
124-
PythStructs.Price memory price = pyth.getPrice(ethUsdPriceId);
124+
PythStructs.Price memory price = pyth.getPriceNoOlderThan(
125+
ethUsdPriceId,
126+
60
127+
);
125128
126129
uint ethPrice18Decimals = (uint(uint64(price.price)) * (10 ** 18)) /
127130
(10 ** uint8(uint32(-1 * price.expo)));
@@ -144,7 +147,7 @@ contract MyFirstPythContract {
144147
145148
```
146149

147-
This function first reads a `Price` from the pyth contract.
150+
This function first reads a `Price` from the pyth contract if it is updated within the last 60 seconds.
148151
It then performs some arithmetic on the price in order to calculate how much the caller needs to pay. This conversion
149152
assumes that 10^18 wei is equal to the native token (ETH in this example); in some networks (like Hedera) the decimal
150153
places are different and you need to change the math.
@@ -292,8 +295,8 @@ Now run `forge test -vvv`
292295
```
293296

294297
Oh no, the test fails with a `StalePrice` error!
295-
When our contract calls `getPrice`, it checks the timestamp on the blockchain and compares it to the timestamp for the Pyth price.
296-
If the Pyth price's timestamp is too far in the past, then a `StalePrice` error occurs.
298+
When our contract calls `getPriceNoOlderThan(.., 60)`, it checks the timestamp on the blockchain and compares it to the timestamp for the Pyth price.
299+
If the Pyth price's timestamp is more than 60 seconds in the past, then a `StalePrice` error occurs.
297300
`skip` moves the timestamp on the blockchain forward, which triggers the error.
298301

299302
We can fix this problem, but first, let's fix the test case.
@@ -413,7 +416,10 @@ contract MyFirstPythContract {
413416
}
414417
415418
function mint() public payable {
416-
PythStructs.Price memory price = pyth.getPrice(ethUsdPriceId);
419+
PythStructs.Price memory price = pyth.getPriceNoOlderThan(
420+
ethUsdPriceId,
421+
60
422+
);
417423
console2.log("price of ETH in USD");
418424
console2.log(price.price);
419425

pages/price-feeds/use-real-time-data/evm.mdx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ contract SomeContract {
6868
function exampleMethod(bytes[] calldata priceUpdate) public payable {
6969
// Submit a priceUpdate to the Pyth contract to update the on-chain price.
7070
// Updating the price requires paying the fee returned by getUpdateFee.
71-
// WARNING: These lines are required to ensure the getPrice call below succeeds. If you remove them, transactions may fail with "0x19abf40e" error.
71+
// WARNING: These lines are required to ensure the getPriceNoOlderThan call below succeeds. If you remove them, transactions may fail with "0x19abf40e" error.
7272
uint fee = pyth.getUpdateFee(priceUpdate);
7373
pyth.updatePriceFeeds{ value: fee }(priceUpdate);
7474
75-
// Read the current price from a price feed.
75+
// Read the current price from a price feed if it is less than 60 seconds old.
7676
// Each price feed (e.g., ETH/USD) is identified by a price feed ID.
7777
// The complete list of feed IDs is available at https://pyth.network/developers/price-feed-ids
7878
bytes32 priceFeedId = 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace; // ETH/USD
79-
PythStructs.Price memory price = pyth.getPrice(priceFeedId);
79+
PythStructs.Price memory price = pyth.getPriceNoOlderThan(priceFeedId, 60);
8080
}
8181
}
8282
@@ -85,10 +85,11 @@ contract SomeContract {
8585
The code snippet above does the following things:
8686

8787
1. Instantiate the `IPyth` interface from the Solidity SDK using the price feeds [contract address](../contract-addresses/evm).
88-
1. Select the [Price Feed IDs](https://pyth.network/developers/price-feed-ids) for the assets you want to fetch prices for. Price feeds come in two varieties, Stable and Beta. You should select Stable feed ids
89-
1. Call `IPyth.getUpdateFee` to calculate the fee charged by Pyth to update the price.
90-
1. Call `IPyth.updatePriceFeeds` to update the price, paying the fee calculated in the previous step.
91-
1. Call `IPyth.getPrice` to read the current price, providing the [price feed ID](https://pyth.network/developers/price-feed-ids) that you wish to read.
88+
2. Select the [Price Feed IDs](https://pyth.network/developers/price-feed-ids) for the assets you want to fetch prices for. Price feeds come in two varieties, Stable and Beta. You should select Stable feed ids
89+
3. Call `IPyth.getUpdateFee` to calculate the fee charged by Pyth to update the price.
90+
4. Call `IPyth.updatePriceFeeds` to update the price, paying the fee calculated in the previous step.
91+
5. Call `IPyth.getPriceNoOlderThan` to read the current price, providing the [price feed ID](https://pyth.network/developers/price-feed-ids) that you wish to read and your acceptable staleness threshold for
92+
the price.
9293

9394
## Additional Resources
9495

0 commit comments

Comments
 (0)