Optimize updatePriceFeedsIfNecessary (#388)

This commit is contained in:
Ali Behjati 2022-11-17 20:41:02 +01:00 committed by GitHub
parent 2597596022
commit 9c5425d829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

View File

@ -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( function parsePriceFeedUpdates(
bytes[] calldata updateData, bytes[] calldata updateData,
bytes32[] calldata priceIds, bytes32[] calldata priceIds,