Skip to content

Commit 78aff34

Browse files
committed
refactor(pyth): streamline TWAP price validation logic for efficiency
1 parent 9f937e3 commit 78aff34

File tree

1 file changed

+15
-35
lines changed
  • target_chains/ethereum/contracts/contracts/pyth

1 file changed

+15
-35
lines changed

target_chains/ethereum/contracts/contracts/pyth/Pyth.sol

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -474,34 +474,20 @@ abstract contract Pyth is
474474
bytes32[] memory endPriceIds;
475475
{
476476
uint offsetEnd;
477-
(
478-
offsetEnd,
479-
endTwapPriceInfos,
480-
endPriceIds
481-
) = extractTwapPriceInfos(updateData[1]);
477+
(offsetEnd, endTwapPriceInfos, endPriceIds) = extractTwapPriceInfos(
478+
updateData[1]
479+
);
482480
}
483481

484482
// Verify that we have the same number of price feeds in start and end updates
485483
if (startPriceIds.length != endPriceIds.length) {
486484
revert PythErrors.InvalidTwapUpdateDataSet();
487485
}
488486

489-
// Create a mapping to check that every startPriceId has a matching endPriceId
490-
// This ensures price feed continuity between start and end points
491-
bool[] memory endPriceIdMatched = new bool[](endPriceIds.length);
487+
// Hermes always returns price feeds in the same order for start and end updates
488+
// This allows us to assume startPriceIds[i] == endPriceIds[i] for efficiency
492489
for (uint i = 0; i < startPriceIds.length; i++) {
493-
bool foundMatch = false;
494-
for (uint j = 0; j < endPriceIds.length; j++) {
495-
if (
496-
startPriceIds[i] == endPriceIds[j] && !endPriceIdMatched[j]
497-
) {
498-
endPriceIdMatched[j] = true;
499-
foundMatch = true;
500-
break;
501-
}
502-
}
503-
// If a price ID in start doesn't have a match in end, it's invalid
504-
if (!foundMatch) {
490+
if (startPriceIds[i] != endPriceIds[i]) {
505491
revert PythErrors.InvalidTwapUpdateDataSet();
506492
}
507493
}
@@ -513,36 +499,30 @@ abstract contract Pyth is
513499
for (uint i = 0; i < priceIds.length; i++) {
514500
bytes32 requestedPriceId = priceIds[i];
515501
int startIdx = -1;
516-
int endIdx = -1;
517502

518-
// Find the index of this price ID in start and end arrays
503+
// Find the index of this price ID in the startPriceIds array
504+
// (which is the same as the endPriceIds array based on our validation above)
519505
for (uint j = 0; j < startPriceIds.length; j++) {
520506
if (startPriceIds[j] == requestedPriceId) {
521507
startIdx = int(j);
522508
break;
523509
}
524510
}
525511

526-
for (uint j = 0; j < endPriceIds.length; j++) {
527-
if (endPriceIds[j] == requestedPriceId) {
528-
endIdx = int(j);
529-
break;
530-
}
531-
}
532-
533-
// If we found both start and end data for this price ID
534-
if (startIdx >= 0 && endIdx >= 0) {
512+
// If we found the price ID
513+
if (startIdx >= 0) {
514+
uint idx = uint(startIdx);
535515
// Validate the pair of price infos
536516
validateTwapPriceInfo(
537-
startTwapPriceInfos[uint(startIdx)],
538-
endTwapPriceInfos[uint(endIdx)]
517+
startTwapPriceInfos[idx],
518+
endTwapPriceInfos[idx]
539519
);
540520

541521
// Calculate TWAP from these data points
542522
twapPriceFeeds[i] = calculateTwap(
543523
requestedPriceId,
544-
startTwapPriceInfos[uint(startIdx)],
545-
endTwapPriceInfos[uint(endIdx)]
524+
startTwapPriceInfos[idx],
525+
endTwapPriceInfos[idx]
546526
);
547527
}
548528
}

0 commit comments

Comments
 (0)