Skip to content

Commit c6ffd34

Browse files
authored
refactor: replace getPrice with getPriceNoOlderThan (#24)
1 parent 0c418bc commit c6ffd34

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

express-relay/easy_lend/contracts/EasyLend.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ contract EasyLend is IExpressRelayFeeReceiver {
8585
*/
8686
function _getPrice(bytes32 id) internal view returns (uint256) {
8787
IPyth oracle = IPyth(payable(_oracle));
88-
return convertToUint(oracle.getPrice(id), 18);
88+
// Get the price if it is not older than 60 seconds.
89+
return convertToUint(oracle.getPriceNoOlderThan(id, 60), 18);
8990
}
9091

9192
function getAllowUndercollateralized() public view returns (bool) {

price_feeds/evm/my_first_pyth_app/contracts/src/MyFirstPythContract.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ contract MyFirstPythContract {
1414
}
1515

1616
function mint() public payable {
17-
PythStructs.Price memory price = pyth.getPrice(ethUsdPriceId);
17+
// Get the price if it is not older than 60 seconds.
18+
PythStructs.Price memory price = pyth.getPriceNoOlderThan(ethUsdPriceId, 60);
1819

1920
uint ethPrice18Decimals = (uint(uint64(price.price)) * (10 ** 18)) /
2021
(10 ** uint8(uint32(-1 * price.expo)));
@@ -41,4 +42,4 @@ contract MyFirstPythContract {
4142
// Error raised if the payment is not sufficient
4243
error InsufficientFee();
4344
}
44-
45+

price_feeds/evm/oracle_swap/contract/src/OracleSwap.sol

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ contract OracleSwap {
6565
uint256 updateFee = pyth.getUpdateFee(pythUpdateData);
6666
pyth.updatePriceFeeds{value: updateFee}(pythUpdateData);
6767

68-
PythStructs.Price memory currentBasePrice = pyth.getPrice(
69-
baseTokenPriceId
68+
// Get the prices if they are not older than 60 seconds.
69+
PythStructs.Price memory currentBasePrice = pyth.getPriceNoOlderThan(
70+
baseTokenPriceId,
71+
60
7072
);
71-
PythStructs.Price memory currentQuotePrice = pyth.getPrice(
72-
quoteTokenPriceId
73+
PythStructs.Price memory currentQuotePrice = pyth.getPriceNoOlderThan(
74+
quoteTokenPriceId,
75+
60
7376
);
7477

7578
// Note: this code does all arithmetic with 18 decimal points. This approach should be fine for most

0 commit comments

Comments
 (0)