@@ -474,34 +474,20 @@ abstract contract Pyth is
474
474
bytes32 [] memory endPriceIds;
475
475
{
476
476
uint offsetEnd;
477
- (
478
- offsetEnd,
479
- endTwapPriceInfos,
480
- endPriceIds
481
- ) = extractTwapPriceInfos (updateData[1 ]);
477
+ (offsetEnd, endTwapPriceInfos, endPriceIds) = extractTwapPriceInfos (
478
+ updateData[1 ]
479
+ );
482
480
}
483
481
484
482
// Verify that we have the same number of price feeds in start and end updates
485
483
if (startPriceIds.length != endPriceIds.length ) {
486
484
revert PythErrors.InvalidTwapUpdateDataSet ();
487
485
}
488
486
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
492
489
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]) {
505
491
revert PythErrors.InvalidTwapUpdateDataSet ();
506
492
}
507
493
}
@@ -513,36 +499,30 @@ abstract contract Pyth is
513
499
for (uint i = 0 ; i < priceIds.length ; i++ ) {
514
500
bytes32 requestedPriceId = priceIds[i];
515
501
int startIdx = - 1 ;
516
- int endIdx = - 1 ;
517
502
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)
519
505
for (uint j = 0 ; j < startPriceIds.length ; j++ ) {
520
506
if (startPriceIds[j] == requestedPriceId) {
521
507
startIdx = int (j);
522
508
break ;
523
509
}
524
510
}
525
511
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);
535
515
// Validate the pair of price infos
536
516
validateTwapPriceInfo (
537
- startTwapPriceInfos[uint (startIdx) ],
538
- endTwapPriceInfos[uint (endIdx) ]
517
+ startTwapPriceInfos[idx ],
518
+ endTwapPriceInfos[idx ]
539
519
);
540
520
541
521
// Calculate TWAP from these data points
542
522
twapPriceFeeds[i] = calculateTwap (
543
523
requestedPriceId,
544
- startTwapPriceInfos[uint (startIdx) ],
545
- endTwapPriceInfos[uint (endIdx) ]
524
+ startTwapPriceInfos[idx ],
525
+ endTwapPriceInfos[idx ]
546
526
);
547
527
}
548
528
}
0 commit comments