From 9c5425d829b4aaa49ddacb44c062e78c1dcfd2f7 Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 17 Nov 2022 20:41:02 +0100 Subject: [PATCH] Optimize updatePriceFeedsIfNecessary (#388) --- ethereum/contracts/pyth/Pyth.sol | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ethereum/contracts/pyth/Pyth.sol b/ethereum/contracts/pyth/Pyth.sol index 1cd0098c..088b3ca9 100644 --- a/ethereum/contracts/pyth/Pyth.sol +++ b/ethereum/contracts/pyth/Pyth.sol @@ -207,6 +207,34 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth { } } + // This is an overwrite of the same method in AbstractPyth.sol + // to be more gas efficient. + function updatePriceFeedsIfNecessary( + bytes[] calldata updateData, + bytes32[] calldata priceIds, + uint64[] calldata publishTimes + ) external payable override { + require( + priceIds.length == publishTimes.length, + "priceIds and publishTimes arrays should have same length" + ); + + for (uint i = 0; i < priceIds.length;) { + // If the price does not exist, then the publish time is zero and + // this condition will work fine. + if (latestPriceInfoPublishTime(priceIds[i]) < publishTimes[i]) { + updatePriceFeeds(updateData); + return; + } + + unchecked { i++; } + } + + revert( + "no prices in the submitted batch have fresh prices, so this update will have no effect" + ); + } + function parsePriceFeedUpdates( bytes[] calldata updateData, bytes32[] calldata priceIds,