Skip to content

Commit 9c5425d

Browse files
authored
Optimize updatePriceFeedsIfNecessary (#388)
1 parent 2597596 commit 9c5425d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ethereum/contracts/pyth/Pyth.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,34 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
207207
}
208208
}
209209

210+
// This is an overwrite of the same method in AbstractPyth.sol
211+
// to be more gas efficient.
212+
function updatePriceFeedsIfNecessary(
213+
bytes[] calldata updateData,
214+
bytes32[] calldata priceIds,
215+
uint64[] calldata publishTimes
216+
) external payable override {
217+
require(
218+
priceIds.length == publishTimes.length,
219+
"priceIds and publishTimes arrays should have same length"
220+
);
221+
222+
for (uint i = 0; i < priceIds.length;) {
223+
// If the price does not exist, then the publish time is zero and
224+
// this condition will work fine.
225+
if (latestPriceInfoPublishTime(priceIds[i]) < publishTimes[i]) {
226+
updatePriceFeeds(updateData);
227+
return;
228+
}
229+
230+
unchecked { i++; }
231+
}
232+
233+
revert(
234+
"no prices in the submitted batch have fresh prices, so this update will have no effect"
235+
);
236+
}
237+
210238
function parsePriceFeedUpdates(
211239
bytes[] calldata updateData,
212240
bytes32[] calldata priceIds,

0 commit comments

Comments
 (0)