-
Notifications
You must be signed in to change notification settings - Fork 306
feat(twap): update TWAP price feed logic to handle multiple price feeds and improve validation #2624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(twap): update TWAP price feed logic to handle multiple price feeds and improve validation #2624
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
21b35d4
feat(twap): update TWAP price feed logic to handle multiple price fee…
cctdaniel 3e5de9b
feat(pyth): enhance TWAP processing to support multiple price feeds a…
cctdaniel 9f937e3
refactor(pyth): rename processSingleTwapUpdate to extractTwapPriceInf…
cctdaniel 78aff34
refactor(pyth): streamline TWAP price validation logic for efficiency
cctdaniel 4891ad2
feat(pyth): add getTwapUpdateFee function to calculate TWAP update fees
cctdaniel a0a205b
feat(pyth): update getTwapUpdateFee to accept updateData and calculat…
cctdaniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -379,11 +379,11 @@ abstract contract Pyth is | |
| view | ||
| returns ( | ||
| /// @return newOffset The next position in the update data after processing this TWAP update | ||
| /// @return twapPriceInfo The extracted time-weighted average price information | ||
| /// @return priceId The unique identifier for this price feed | ||
| /// @return priceInfos Array of extracted TWAP price information | ||
| /// @return priceIds Array of corresponding price feed IDs | ||
| uint newOffset, | ||
| PythStructs.TwapPriceInfo memory twapPriceInfo, | ||
| bytes32 priceId | ||
| PythStructs.TwapPriceInfo[] memory twapPriceInfos, | ||
| bytes32[] memory priceIds | ||
| ) | ||
| { | ||
| UpdateType updateType; | ||
|
|
@@ -417,12 +417,22 @@ abstract contract Pyth is | |
| revert PythErrors.InvalidUpdateData(); | ||
| } | ||
|
|
||
| // Extract start TWAP data with robust error checking | ||
| (offset, twapPriceInfo, priceId) = extractTwapPriceInfoFromMerkleProof( | ||
| digest, | ||
| encoded, | ||
| offset | ||
| ); | ||
| // Initialize arrays to store all price infos and ids from this update | ||
| twapPriceInfos = new PythStructs.TwapPriceInfo[](numUpdates); | ||
| priceIds = new bytes32[](numUpdates); | ||
|
|
||
| // Extract each TWAP price info from the merkle proof | ||
| for (uint i = 0; i < numUpdates; i++) { | ||
| PythStructs.TwapPriceInfo memory twapPriceInfo; | ||
| bytes32 priceId; | ||
| ( | ||
| offset, | ||
| twapPriceInfo, | ||
| priceId | ||
| ) = extractTwapPriceInfoFromMerkleProof(digest, encoded, offset); | ||
| twapPriceInfos[i] = twapPriceInfo; | ||
| priceIds[i] = priceId; | ||
| } | ||
|
|
||
| if (offset != encoded.length) { | ||
| revert PythErrors.InvalidTwapUpdateData(); | ||
|
|
@@ -439,71 +449,109 @@ abstract contract Pyth is | |
| override | ||
| returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds) | ||
| { | ||
| // TWAP requires exactly 2 updates - one for the start point and one for the end point | ||
| // to calculate the time-weighted average price between those two points | ||
| // TWAP requires exactly 2 updates: one for the start point and one for the end point | ||
| if (updateData.length != 2) { | ||
| revert PythErrors.InvalidUpdateData(); | ||
| } | ||
|
|
||
| uint requiredFee = getUpdateFee(updateData); | ||
| if (msg.value < requiredFee) revert PythErrors.InsufficientFee(); | ||
|
|
||
| unchecked { | ||
| twapPriceFeeds = new PythStructs.TwapPriceFeed[](priceIds.length); | ||
| for (uint i = 0; i < updateData.length - 1; i++) { | ||
| // Process start update data | ||
| PythStructs.TwapPriceInfo[] memory startTwapPriceInfos; | ||
| bytes32[] memory startPriceIds; | ||
| { | ||
| uint offsetStart; | ||
| ( | ||
| offsetStart, | ||
| startTwapPriceInfos, | ||
| startPriceIds | ||
| ) = processSingleTwapUpdate(updateData[0]); | ||
| } | ||
|
|
||
| // Process end update data | ||
| PythStructs.TwapPriceInfo[] memory endTwapPriceInfos; | ||
| bytes32[] memory endPriceIds; | ||
| { | ||
| uint offsetEnd; | ||
| ( | ||
| offsetEnd, | ||
| endTwapPriceInfos, | ||
| endPriceIds | ||
| ) = processSingleTwapUpdate(updateData[1]); | ||
| } | ||
|
|
||
| // Verify that we have the same number of price feeds in start and end updates | ||
| if (startPriceIds.length != endPriceIds.length) { | ||
| revert PythErrors.InvalidTwapUpdateDataSet(); | ||
| } | ||
|
|
||
| // Create a mapping to check that every startPriceId has a matching endPriceId | ||
| // This ensures price feed continuity between start and end points | ||
| bool[] memory endPriceIdMatched = new bool[](endPriceIds.length); | ||
| for (uint i = 0; i < startPriceIds.length; i++) { | ||
| bool foundMatch = false; | ||
| for (uint j = 0; j < endPriceIds.length; j++) { | ||
cctdaniel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( | ||
| (updateData[i].length > 4 && | ||
| UnsafeCalldataBytesLib.toUint32(updateData[i], 0) == | ||
| ACCUMULATOR_MAGIC) && | ||
| (updateData[i + 1].length > 4 && | ||
| UnsafeCalldataBytesLib.toUint32(updateData[i + 1], 0) == | ||
| ACCUMULATOR_MAGIC) | ||
| startPriceIds[i] == endPriceIds[j] && !endPriceIdMatched[j] | ||
| ) { | ||
|
||
| uint offsetStart; | ||
| uint offsetEnd; | ||
| bytes32 priceIdStart; | ||
| bytes32 priceIdEnd; | ||
| PythStructs.TwapPriceInfo memory twapPriceInfoStart; | ||
| PythStructs.TwapPriceInfo memory twapPriceInfoEnd; | ||
| ( | ||
| offsetStart, | ||
| twapPriceInfoStart, | ||
| priceIdStart | ||
| ) = processSingleTwapUpdate(updateData[i]); | ||
| ( | ||
| offsetEnd, | ||
| twapPriceInfoEnd, | ||
| priceIdEnd | ||
| ) = processSingleTwapUpdate(updateData[i + 1]); | ||
|
|
||
| if (priceIdStart != priceIdEnd) | ||
| revert PythErrors.InvalidTwapUpdateDataSet(); | ||
|
|
||
| validateTwapPriceInfo(twapPriceInfoStart, twapPriceInfoEnd); | ||
|
|
||
| uint k = findIndexOfPriceId(priceIds, priceIdStart); | ||
|
|
||
| // If priceFeed[k].id != 0 then it means that there was a valid | ||
| // update for priceIds[k] and we don't need to process this one. | ||
| if (k == priceIds.length || twapPriceFeeds[k].id != 0) { | ||
| continue; | ||
| } | ||
|
|
||
| twapPriceFeeds[k] = calculateTwap( | ||
| priceIdStart, | ||
| twapPriceInfoStart, | ||
| twapPriceInfoEnd | ||
| ); | ||
| } else { | ||
| revert PythErrors.InvalidUpdateData(); | ||
| endPriceIdMatched[j] = true; | ||
| foundMatch = true; | ||
| break; | ||
| } | ||
| } | ||
| // If a price ID in start doesn't have a match in end, it's invalid | ||
| if (!foundMatch) { | ||
| revert PythErrors.InvalidTwapUpdateDataSet(); | ||
| } | ||
| } | ||
|
|
||
| // Initialize the output array | ||
| twapPriceFeeds = new PythStructs.TwapPriceFeed[](priceIds.length); | ||
|
|
||
| for (uint k = 0; k < priceIds.length; k++) { | ||
| if (twapPriceFeeds[k].id == 0) { | ||
| revert PythErrors.PriceFeedNotFoundWithinRange(); | ||
| // For each requested price ID, find matching start and end data points | ||
| for (uint i = 0; i < priceIds.length; i++) { | ||
cctdaniel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bytes32 requestedPriceId = priceIds[i]; | ||
| int startIdx = -1; | ||
| int endIdx = -1; | ||
|
|
||
| // Find the index of this price ID in start and end arrays | ||
| for (uint j = 0; j < startPriceIds.length; j++) { | ||
| if (startPriceIds[j] == requestedPriceId) { | ||
| startIdx = int(j); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| for (uint j = 0; j < endPriceIds.length; j++) { | ||
| if (endPriceIds[j] == requestedPriceId) { | ||
| endIdx = int(j); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // If we found both start and end data for this price ID | ||
| if (startIdx >= 0 && endIdx >= 0) { | ||
| // Validate the pair of price infos | ||
| validateTwapPriceInfo( | ||
| startTwapPriceInfos[uint(startIdx)], | ||
| endTwapPriceInfos[uint(endIdx)] | ||
| ); | ||
|
|
||
| // Calculate TWAP from these data points | ||
| twapPriceFeeds[i] = calculateTwap( | ||
| requestedPriceId, | ||
| startTwapPriceInfos[uint(startIdx)], | ||
| endTwapPriceInfos[uint(endIdx)] | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Ensure all requested price IDs were found | ||
| for (uint k = 0; k < priceIds.length; k++) { | ||
| if (twapPriceFeeds[k].id == 0) { | ||
| revert PythErrors.PriceFeedNotFoundWithinRange(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.