Skip to content

feat(target_chains/ethereum): remove getPrice from IPyth #1811

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 2 commits 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 apps/api-reference/src/apis/evm/get-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readApi, solidity, ethersJS } from "./common";
import { ParameterType } from "../../components/EvmApi";

export const getPrice = readApi<"id">({
name: "getPrice",
name: "getPrice (deprecated)",
summary: "Get the **latest** price object for the requested price feed ID.",
description: `
This method returns the latest price object for the requested price feed ID.
Expand Down
2 changes: 1 addition & 1 deletion apps/api-reference/src/apis/evm/get-valid-time-period.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readApi, solidity, ethersJS } from "./common";

export const getValidTimePeriod = readApi<never>({
name: "getValidTimePeriod",
name: "getValidTimePeriod (deprecated)",
summary: "Get the default valid time period of price freshness in seconds.",
description: `
This method returns the default valid time period of price freshness in **seconds**.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const parsePriceFeedUpdatesUnique = writeApi<

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.
followed by [getPriceNoOlderThan](get-price-no-older-than) or one of its variants.

Unlike [updatePriceFeeds](updatePriceFeeds), calling this function will **not** update the on-chain price.

Expand Down
2 changes: 1 addition & 1 deletion apps/api-reference/src/components/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const Home = () => (
<li className="contents">
<ProductLink
icon={PriceFeeds}
href="/price-feeds/evm/getPrice"
href="/price-feeds/evm/getPriceNoOlderThan"
name="Price Feeds"
>
Fetch real-time low-latency market data, on 50+ chains or off
Expand Down
2 changes: 1 addition & 1 deletion express_relay/examples/easy_lend/contracts/EasyLend.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ contract EasyLend is IExpressRelayFeeReceiver {
*/
function _getPrice(bytes32 id) internal view returns (uint256) {
IPyth oracle = IPyth(payable(_oracle));
return convertToUint(oracle.getPrice(id), 18);
return convertToUint(oracle.getPriceNoOlderThan(id, 60), 18);
}

function getAllowUndercollateralized() public view returns (bool) {
Expand Down
21 changes: 8 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -389,18 +389,18 @@ contract GasBenchmark is Test, WormholeTestUtils, PythTestUtils {

function testBenchmarkGetPrice() public {
// Set the block timestamp to 0. As prices have < 10 timestamp and staleness
// is set to 60 seconds, the getPrice should work as expected.
// below is set to 60 seconds, the getPriceNoOlderThan should work as expected.
vm.warp(0);

pyth.getPrice(priceIds[0]);
pyth.getPriceNoOlderThan(priceIds[0], 60);
}

function testBenchmarkGetEmaPrice() public {
// Set the block timestamp to 0. As prices have < 10 timestamp and staleness
// is set to 60 seconds, the getPrice should work as expected.
// below is set to 60 seconds, the getEmaPriceNoOlderThan should work as expected.
vm.warp(0);

pyth.getEmaPrice(priceIds[0]);
pyth.getEmaPriceNoOlderThan(priceIds[0], 60);
}

function testBenchmarkGetUpdateFee1() public view {
Expand Down
5 changes: 4 additions & 1 deletion target_chains/ethereum/contracts/forge-test/Pyth.Aave.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ contract PythAaveTest is PythWormholeMerkleAccumulatorTest {
uint256 aavePrice = assetPrice / BASE_CURRENCY_UNIT;

bytes32 priceId = priceIds[i];
PythStructs.Price memory price = pyth.getPrice(priceId);
PythStructs.Price memory price = pyth.getPriceNoOlderThan(
priceId,
60
);
int64 pythRawPrice = price.price;
uint pythNormalizer;
uint pythPrice;
Expand Down
2 changes: 1 addition & 1 deletion target_chains/ethereum/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@openzeppelin/contracts": "=4.8.1",
"@openzeppelin/contracts-upgradeable": "=4.8.1",
"@openzeppelin/hardhat-upgrades": "^1.22.1",
"@pythnetwork/pyth-sdk-solidity": "^3.0.0",
"@pythnetwork/pyth-sdk-solidity": "workspace:*",
"@pythnetwork/entropy-sdk-solidity": "workspace:*",
"@pythnetwork/contract-manager": "workspace:*",
"dotenv": "^10.0.0",
Expand Down
3 changes: 2 additions & 1 deletion target_chains/ethereum/sdk/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ contract SomeContract {

// Doing other things that uses prices
bytes32 priceId = 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b;
PythStructs.Price price = pyth.getPrice(priceId);
// Get the price if it is not older than 10 seconds from the current time.
PythStructs.Price price = pyth.getPriceNoOlderThan(priceId, 10);
}
}

Expand Down
6 changes: 3 additions & 3 deletions target_chains/ethereum/sdk/js/src/examples/EvmRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ async function run() {
receipt = await web3.eth.getTransactionReceipt(txHash);
}

// For on-chain use, you will typically perform the getPrice call within the same transaction as updatePriceFeeds.
// The call to getPrice below simply demonstrates that the on-chain price was in fact updated.
// For on-chain use, you will typically perform the getPriceNoOlderThan call within the same transaction as updatePriceFeeds.
// The call to getPriceNoOlderThan below simply demonstrates that the on-chain price was in fact updated.
// Note that the code above for waiting for tx confirmation is a little flaky -- if so, you may see an old price printed here.
for (const priceId of priceIds) {
const [price, conf, expo, publishTime] = await pythContract.methods
.getPrice(priceId)
.getPriceNoOlderThan(priceId, 60) // 60 seconds staleness tolerance
.call();
console.log(
`Updated ${priceId} to (${price} +- ${conf}) * 10^${expo} at unix timestamp ${publishTime}`
Expand Down
8 changes: 5 additions & 3 deletions target_chains/ethereum/sdk/solidity/AbstractPyth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ abstract contract AbstractPyth is IPyth {
bytes32 id
) public view virtual returns (bool exists);

/// @notice This function is deprecated and is only kept for backward compatibility.
function getValidTimePeriod()
public
view
virtual
override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the same @notice to getValidTimePeriod() as well?

returns (uint validTimePeriod);

/// @notice This function is deprecated and is only kept for backward compatibility.
function getPrice(
bytes32 id
) external view virtual override returns (PythStructs.Price memory price) {
) external view virtual returns (PythStructs.Price memory price) {
return getPriceNoOlderThan(id, getValidTimePeriod());
}

/// @notice This function is deprecated and is only kept for backward compatibility.
function getEmaPrice(
bytes32 id
) external view virtual override returns (PythStructs.Price memory price) {
) external view virtual returns (PythStructs.Price memory price) {
return getEmaPriceNoOlderThan(id, getValidTimePeriod());
}

Expand Down
21 changes: 1 addition & 20 deletions target_chains/ethereum/sdk/solidity/IPyth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,13 @@ import "./IPythEvents.sol";
/// @dev Please refer to the guidance at https://docs.pyth.network/documentation/pythnet-price-feeds/best-practices for how to consume prices safely.
/// @author Pyth Data Association
interface IPyth is IPythEvents {
/// @notice Returns the period (in seconds) that a price feed is considered valid since its publish time
function getValidTimePeriod() external view returns (uint validTimePeriod);

/// @notice Returns the price and confidence interval.
/// @dev Reverts if the price has not been updated within the last `getValidTimePeriod()` seconds.
/// @param id The Pyth Price Feed ID of which to fetch the price and confidence interval.
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
function getPrice(
bytes32 id
) external view returns (PythStructs.Price memory price);

/// @notice Returns the exponentially-weighted moving average price and confidence interval.
/// @dev Reverts if the EMA price is not available.
/// @param id The Pyth Price Feed ID of which to fetch the EMA price and confidence interval.
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
function getEmaPrice(
bytes32 id
) external view returns (PythStructs.Price memory price);

/// @notice Returns the price of a price feed without any sanity checks.
/// @dev This function returns the most recent price update in this contract without any recency checks.
/// This function is unsafe as the returned price update may be arbitrarily far in the past.
///
/// Users of this function should check the `publishTime` in the price to ensure that the returned price is
/// sufficiently recent for their application. If you are considering using this function, it may be
/// safer / easier to use either `getPrice` or `getPriceNoOlderThan`.
/// safer / easier to use `getPriceNoOlderThan`.
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
function getPriceUnsafe(
bytes32 id
Expand Down
13 changes: 7 additions & 6 deletions target_chains/ethereum/sdk/solidity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ Then add the following line to your `remappings.txt` file:

## Example Usage

To consume prices you should use the [`IPyth`](IPyth.sol) interface. Please make sure to read the documentation of this interface in order to use the prices safely.
To consume prices you should use the [`IPyth`](IPyth.sol) interface. Please make sure to read the documentation of this
interface in order to use the prices safely.

For example, to read the latest price, call [`getPrice`](IPyth.sol) with the Price ID of the price feed you're interested in. The price feeds available on each chain are listed [below](#target-chains).
For example, to read the latest price, call [`getPriceNoOlderThan`](IPyth.sol) with the Price ID of the price feed
you're interested in. The price feeds available on each chain are listed [below](#target-chains).

```solidity
// SPDX-License-Identifier: MIT
Expand All @@ -61,10 +63,9 @@ contract ExampleContract {
pyth.updatePriceFeeds{ value: fee }(priceUpdateData);

bytes32 priceID = 0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b;
// Read the current value of priceID, aborting the transaction if the price has not been updated recently.
// Every chain has a default recency threshold which can be retrieved by calling the getValidTimePeriod() function on the contract.
// Please see IPyth.sol for variants of this function that support configurable recency thresholds and other useful features.
return pyth.getPrice(priceID);
// Read the current value of priceID, aborting the transaction if the price has not been updated in the last 10
// seconds.
return pyth.getPriceNoOlderThan(priceID, 10);
}
}

Expand Down
Loading
Loading