Skip to content

refactor: deprecate getPrice function #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/price-feeds/api-reference/evm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ Users of the Pyth contract will typically need to perform two operations:
for verification. This operation makes the price available for on-chain use.
You will typically call [updatePriceFeeds](evm/update-price-feeds) to do this.
- Read the on-chain price -- After updating the price, your on-chain contract can call one of the
many getter functions on the contract to get the price. See [getPrice](evm/get-price) and its variants
many getter functions on the contract to get the price. See [getPriceNoOlderThan](evm/get-price-no-older-than) and its variants
for more information.
4 changes: 2 additions & 2 deletions pages/price-feeds/api-reference/evm/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"type": "separator"
},

"get-price": "getPrice",
"get-price": "getPrice (deprecated)",
"get-price-unsafe": "getPriceUnsafe",
"get-price-no-older-than": "getPriceNoOlderThan",
"get-ema-price": "getEmaPrice",
"get-ema-price-unsafe": "getEmaPriceUnsafe",
"get-ema-price-no-older-than": "getEmaPriceNoOlderThan",
"get-update-fee": "getUpdateFee",
"get-valid-time-period": "getValidTimePeriod",
"get-valid-time-period": "getValidTimePeriod (deprecated)",
"parse-price-feed-updates": "parsePriceFeedUpdates",
"parse-price-feed-updates-unique": "parsePriceFeedUpdatesUnique",
"update-price-feeds": "updatePriceFeeds",
Expand Down
4 changes: 3 additions & 1 deletion pages/price-feeds/api-reference/evm/get-price.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import DynamicCode from "../../../../components/DynamicCode";
import EvmCall from "../../../../components/EvmCall";
import { Tab, Tabs } from "nextra-theme-docs";

# Get Price
# Get Price (Deprecated)

**This function is deprecated, please consider using `getPriceNoOlderThan` or `getPriceUnsafe` instead.**

Get the latest price and confidence interval for the requested price feed id.
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import DynamicCode from "../../../../components/DynamicCode";
import EvmCall from "../../../../components/EvmCall";
import { Tab, Tabs } from "nextra-theme-docs";

# Get Valid Time Period
# Get Valid Time Period (deprecated)

**This function is deprecated. Please consider using `getPriceNoOlderThan` with your own acceptable staleness time period.**

Get the default valid time period in seconds.
This quantity is the maximum age of price updates returned by functions like [getPrice](get-price) and [getEmaPrice](get-ema-price);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Tab, Tabs } from "nextra-theme-docs";

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

If you need to make sure the price update is the earliest update after the `minPublishTime` consider using [parsePriceFeedUpdatesUnique](parse-price-feed-updates-unique).
Expand Down
16 changes: 11 additions & 5 deletions pages/price-feeds/create-your-first-pyth-app/evm/part-1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ contract MyFirstPythContract {
// ... other functions omitted

function mint() public payable {
PythStructs.Price memory price = pyth.getPrice(ethUsdPriceId);
PythStructs.Price memory price = pyth.getPriceNoOlderThan(
ethUsdPriceId,
60
);

uint ethPrice18Decimals = (uint(uint64(price.price)) * (10 ** 18)) /
(10 ** uint8(uint32(-1 * price.expo)));
Expand All @@ -144,7 +147,7 @@ contract MyFirstPythContract {

```

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

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

We can fix this problem, but first, let's fix the test case.
Expand Down Expand Up @@ -413,7 +416,10 @@ contract MyFirstPythContract {
}

function mint() public payable {
PythStructs.Price memory price = pyth.getPrice(ethUsdPriceId);
PythStructs.Price memory price = pyth.getPriceNoOlderThan(
ethUsdPriceId,
60
);
console2.log("price of ETH in USD");
console2.log(price.price);

Expand Down
15 changes: 8 additions & 7 deletions pages/price-feeds/use-real-time-data/evm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ contract SomeContract {
function exampleMethod(bytes[] calldata priceUpdate) public payable {
// Submit a priceUpdate to the Pyth contract to update the on-chain price.
// Updating the price requires paying the fee returned by getUpdateFee.
// WARNING: These lines are required to ensure the getPrice call below succeeds. If you remove them, transactions may fail with "0x19abf40e" error.
// WARNING: These lines are required to ensure the getPriceNoOlderThan call below succeeds. If you remove them, transactions may fail with "0x19abf40e" error.
uint fee = pyth.getUpdateFee(priceUpdate);
pyth.updatePriceFeeds{ value: fee }(priceUpdate);

// Read the current price from a price feed.
// Read the current price from a price feed if it is less than 60 seconds old.
// Each price feed (e.g., ETH/USD) is identified by a price feed ID.
// The complete list of feed IDs is available at https://pyth.network/developers/price-feed-ids
bytes32 priceFeedId = 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace; // ETH/USD
PythStructs.Price memory price = pyth.getPrice(priceFeedId);
PythStructs.Price memory price = pyth.getPriceNoOlderThan(priceFeedId, 60);
}
}

Expand All @@ -85,10 +85,11 @@ contract SomeContract {
The code snippet above does the following things:

1. Instantiate the `IPyth` interface from the Solidity SDK using the price feeds [contract address](../contract-addresses/evm).
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
1. Call `IPyth.getUpdateFee` to calculate the fee charged by Pyth to update the price.
1. Call `IPyth.updatePriceFeeds` to update the price, paying the fee calculated in the previous step.
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.
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
3. Call `IPyth.getUpdateFee` to calculate the fee charged by Pyth to update the price.
4. Call `IPyth.updatePriceFeeds` to update the price, paying the fee calculated in the previous step.
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
the price.

## Additional Resources

Expand Down
Loading