From 8db2af6ee567def722e834e1b445310075976026 Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Fri, 4 Nov 2022 17:16:14 +0100 Subject: [PATCH] Use latestPriceInfoPublishTime when possible (#377) --- ethereum/contracts/pyth/Pyth.sol | 9 ++++----- ethereum/contracts/pyth/PythGetters.sol | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ethereum/contracts/pyth/Pyth.sol b/ethereum/contracts/pyth/Pyth.sol index 836bba64..71b0bc41 100644 --- a/ethereum/contracts/pyth/Pyth.sol +++ b/ethereum/contracts/pyth/Pyth.sol @@ -195,16 +195,16 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth { index += attestationSize; // Store the attestation - PythInternalStructs.PriceInfo memory latestPrice = latestPriceInfo(priceId); + uint64 latestPublishTime = latestPriceInfoPublishTime(priceId); bool fresh = false; - if(info.price.publishTime > latestPrice.price.publishTime) { + if(info.price.publishTime > latestPublishTime) { freshPrices += 1; fresh = true; setLatestPriceInfo(priceId, info); } - emit PriceFeedUpdate(priceId, fresh, vm.emitterChainId, vm.sequence, latestPrice.price.publishTime, + emit PriceFeedUpdate(priceId, fresh, vm.emitterChainId, vm.sequence, latestPublishTime, info.price.publishTime, info.price.price, info.price.conf); } @@ -230,8 +230,7 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth { } function priceFeedExists(bytes32 id) public override view returns (bool) { - PythInternalStructs.PriceInfo memory info = latestPriceInfo(id); - return (info.price.publishTime != 0); + return (latestPriceInfoPublishTime(id) != 0); } function getValidTimePeriod() public override view returns (uint) { diff --git a/ethereum/contracts/pyth/PythGetters.sol b/ethereum/contracts/pyth/PythGetters.sol index 3fa56f56..765a2e76 100644 --- a/ethereum/contracts/pyth/PythGetters.sol +++ b/ethereum/contracts/pyth/PythGetters.sol @@ -27,6 +27,10 @@ contract PythGetters is PythState { return _state.latestPriceInfo[priceId]; } + function latestPriceInfoPublishTime(bytes32 priceId) public view returns (uint64) { + return _state.latestPriceInfo[priceId].price.publishTime; + } + function hashDataSource(PythInternalStructs.DataSource memory ds) public pure returns (bytes32) { return keccak256(abi.encodePacked(ds.chainId, ds.emitterAddress)); }