| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055 |
- // SPDX-License-Identifier: Apache 2
- pragma solidity ^0.8.0;
- import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
- import "forge-std/Test.sol";
- import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
- import "@pythnetwork/pyth-sdk-solidity/PythErrors.sol";
- import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
- import "./utils/WormholeTestUtils.t.sol";
- import "./utils/PythTestUtils.t.sol";
- import "./utils/RandTestUtils.t.sol";
- import "forge-std/console.sol";
- contract PythTest is Test, WormholeTestUtils, PythTestUtils {
- IPyth public pyth;
- // -1 is equal to 0xffffff which is the biggest uint if converted back
- uint64 constant MAX_UINT64 = uint64(int64(-1));
- // 2/3 of the guardians should sign a message for a VAA which is 13 out of 19 guardians.
- // It is possible to have more signers but the median seems to be 13.
- uint8 constant NUM_GUARDIAN_SIGNERS = 13;
- // We will have less than 512 price for a foreseeable future.
- uint8 constant MERKLE_TREE_DEPTH = 9;
- // Base TWAP messages that will be used as templates for tests
- TwapPriceFeedMessage[2] baseTwapStartMessages;
- TwapPriceFeedMessage[2] baseTwapEndMessages;
- bytes32[2] basePriceIds;
- function setUp() public {
- pyth = IPyth(setUpPyth(setUpWormholeReceiver(NUM_GUARDIAN_SIGNERS)));
- // Initialize base TWAP messages for two price feeds
- basePriceIds[0] = bytes32(uint256(1));
- basePriceIds[1] = bytes32(uint256(2));
- // First price feed TWAP messages
- baseTwapStartMessages[0] = TwapPriceFeedMessage({
- priceId: basePriceIds[0],
- cumulativePrice: 100_000, // Base cumulative value
- cumulativeConf: 10_000, // Base cumulative conf
- numDownSlots: 0,
- publishSlot: 1000,
- publishTime: 1000,
- prevPublishTime: 900,
- expo: -8
- });
- baseTwapEndMessages[0] = TwapPriceFeedMessage({
- priceId: basePriceIds[0],
- cumulativePrice: 210_000, // Increased by 110_000
- cumulativeConf: 18_000, // Increased by 8_000
- numDownSlots: 0,
- publishSlot: 1100,
- publishTime: 1100,
- prevPublishTime: 1000,
- expo: -8
- });
- // Second price feed TWAP messages
- baseTwapStartMessages[1] = TwapPriceFeedMessage({
- priceId: basePriceIds[1],
- cumulativePrice: 500_000, // Different base cumulative value
- cumulativeConf: 20_000, // Different base cumulative conf
- numDownSlots: 0,
- publishSlot: 1000,
- publishTime: 1000,
- prevPublishTime: 900,
- expo: -8
- });
- baseTwapEndMessages[1] = TwapPriceFeedMessage({
- priceId: basePriceIds[1],
- cumulativePrice: 800_000, // Increased by 300_000
- cumulativeConf: 40_000, // Increased by 20_000
- numDownSlots: 0,
- publishSlot: 1100,
- publishTime: 1100,
- prevPublishTime: 1000,
- expo: -8
- });
- }
- function generateRandomPriceMessages(
- uint length
- )
- internal
- returns (bytes32[] memory priceIds, PriceFeedMessage[] memory messages)
- {
- messages = new PriceFeedMessage[](length);
- priceIds = new bytes32[](length);
- for (uint i = 0; i < length; i++) {
- messages[i].priceId = bytes32(i + 1); // price ids should be non-zero and unique
- messages[i].price = getRandInt64();
- messages[i].conf = getRandUint64();
- messages[i].expo = getRandInt32();
- messages[i].emaPrice = getRandInt64();
- messages[i].emaConf = getRandUint64();
- messages[i].publishTime = getRandUint64();
- messages[i].prevPublishTime = getRandUint64();
- priceIds[i] = messages[i].priceId;
- }
- }
- // This method divides messages into a couple of batches and creates
- // updateData for them. It returns the updateData and the updateFee
- function createBatchedUpdateDataFromMessagesWithConfig(
- PriceFeedMessage[] memory messages,
- MerkleUpdateConfig memory config
- ) internal returns (bytes[] memory updateData, uint updateFee) {
- uint batchSize = 1 + (getRandUint() % messages.length);
- uint numBatches = (messages.length + batchSize - 1) / batchSize;
- updateData = new bytes[](numBatches);
- for (uint i = 0; i < messages.length; i += batchSize) {
- uint len = batchSize;
- if (messages.length - i < len) {
- len = messages.length - i;
- }
- PriceFeedMessage[] memory batchMessages = new PriceFeedMessage[](
- len
- );
- for (uint j = i; j < i + len; j++) {
- batchMessages[j - i] = messages[j];
- }
- updateData[i / batchSize] = generateWhMerkleUpdateWithSource(
- batchMessages,
- config
- );
- }
- updateFee = pyth.getUpdateFee(updateData);
- }
- function createBatchedUpdateDataFromMessages(
- PriceFeedMessage[] memory messages
- ) internal returns (bytes[] memory updateData, uint updateFee) {
- (updateData, updateFee) = createBatchedUpdateDataFromMessagesWithConfig(
- messages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- }
- // This method divides messages into a couple of batches and creates
- // twap updateData for them. It returns the updateData and the updateFee
- function createBatchedTwapUpdateDataFromMessagesWithConfig(
- PriceFeedMessage[] memory messages,
- MerkleUpdateConfig memory config
- ) public returns (bytes[] memory updateData, uint updateFee) {
- require(messages.length >= 2, "At least 2 messages required for TWAP");
- // Create arrays to hold the start and end TWAP messages for all price feeds
- TwapPriceFeedMessage[]
- memory startTwapMessages = new TwapPriceFeedMessage[](
- messages.length / 2
- );
- TwapPriceFeedMessage[]
- memory endTwapMessages = new TwapPriceFeedMessage[](
- messages.length / 2
- );
- // Fill the arrays with all price feeds' start and end points
- for (uint i = 0; i < messages.length / 2; i++) {
- // Create start message for this price feed
- startTwapMessages[i].priceId = messages[i * 2].priceId;
- startTwapMessages[i].cumulativePrice =
- int128(messages[i * 2].price) *
- 1000;
- startTwapMessages[i].cumulativeConf =
- uint128(messages[i * 2].conf) *
- 1000;
- startTwapMessages[i].numDownSlots = 0; // No down slots for testing
- startTwapMessages[i].expo = messages[i * 2].expo;
- startTwapMessages[i].publishTime = messages[i * 2].publishTime;
- startTwapMessages[i].prevPublishTime = messages[i * 2]
- .prevPublishTime;
- startTwapMessages[i].publishSlot = 1000; // Start slot
- // Create end message for this price feed
- endTwapMessages[i].priceId = messages[i * 2 + 1].priceId;
- endTwapMessages[i].cumulativePrice =
- int128(messages[i * 2 + 1].price) *
- 1000 +
- startTwapMessages[i].cumulativePrice;
- endTwapMessages[i].cumulativeConf =
- uint128(messages[i * 2 + 1].conf) *
- 1000 +
- startTwapMessages[i].cumulativeConf;
- endTwapMessages[i].numDownSlots = 0; // No down slots for testing
- endTwapMessages[i].expo = messages[i * 2 + 1].expo;
- endTwapMessages[i].publishTime = messages[i * 2 + 1].publishTime;
- endTwapMessages[i].prevPublishTime = messages[i * 2 + 1]
- .prevPublishTime;
- endTwapMessages[i].publishSlot = 1100; // End slot (100 slots after start)
- }
- // Create exactly 2 updateData entries as required by parseTwapPriceFeedUpdates
- updateData = new bytes[](2);
- // First update contains all start points
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startTwapMessages,
- config
- );
- // Second update contains all end points
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endTwapMessages,
- config
- );
- // Calculate the update fee
- updateFee = pyth.getUpdateFee(updateData);
- }
- function createBatchedTwapUpdateDataFromMessages(
- PriceFeedMessage[] memory messages
- ) internal returns (bytes[] memory updateData, uint updateFee) {
- (
- updateData,
- updateFee
- ) = createBatchedTwapUpdateDataFromMessagesWithConfig(
- messages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- }
- function testParsePriceFeedUpdatesWorks(uint seed) public {
- setRandSeed(seed);
- uint numMessages = 1 + (getRandUint() % 10);
- (
- bytes32[] memory priceIds,
- PriceFeedMessage[] memory messages
- ) = generateRandomPriceMessages(numMessages);
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessages(messages);
- PythStructs.PriceFeed[] memory priceFeeds = pyth.parsePriceFeedUpdates{
- value: updateFee
- }(updateData, priceIds, 0, MAX_UINT64);
- for (uint i = 0; i < numMessages; i++) {
- assertEq(priceFeeds[i].id, priceIds[i]);
- assertEq(priceFeeds[i].price.price, messages[i].price);
- assertEq(priceFeeds[i].price.conf, messages[i].conf);
- assertEq(priceFeeds[i].price.expo, messages[i].expo);
- assertEq(priceFeeds[i].price.publishTime, messages[i].publishTime);
- assertEq(priceFeeds[i].emaPrice.price, messages[i].emaPrice);
- assertEq(priceFeeds[i].emaPrice.conf, messages[i].emaConf);
- assertEq(priceFeeds[i].emaPrice.expo, messages[i].expo);
- assertEq(
- priceFeeds[i].emaPrice.publishTime,
- messages[i].publishTime
- );
- }
- }
- function testParsePriceFeedUpdatesWithSlotsStrictWorks(uint seed) public {
- setRandSeed(seed);
- uint numMessages = 1 + (getRandUint() % 10);
- (
- bytes32[] memory priceIds,
- PriceFeedMessage[] memory messages
- ) = generateRandomPriceMessages(numMessages);
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessages(messages);
- (
- PythStructs.PriceFeed[] memory priceFeeds,
- uint64[] memory slots
- ) = pyth.parsePriceFeedUpdatesWithSlotsStrict{value: updateFee}(
- updateData,
- priceIds,
- 0,
- MAX_UINT64
- );
- assertEq(priceFeeds.length, numMessages);
- assertEq(slots.length, numMessages);
- for (uint i = 0; i < numMessages; i++) {
- assertEq(priceFeeds[i].id, priceIds[i]);
- assertEq(priceFeeds[i].price.price, messages[i].price);
- assertEq(priceFeeds[i].price.conf, messages[i].conf);
- assertEq(priceFeeds[i].price.expo, messages[i].expo);
- assertEq(priceFeeds[i].price.publishTime, messages[i].publishTime);
- assertEq(priceFeeds[i].emaPrice.price, messages[i].emaPrice);
- assertEq(priceFeeds[i].emaPrice.conf, messages[i].emaConf);
- assertEq(priceFeeds[i].emaPrice.expo, messages[i].expo);
- assertEq(
- priceFeeds[i].emaPrice.publishTime,
- messages[i].publishTime
- );
- // Check that the slot returned is 1, as set in generateWhMerkleUpdateWithSource
- assertEq(slots[i], 1);
- }
- }
- function testParsePriceFeedUpdatesWorksWithOverlappingWithinTimeRangeUpdates()
- public
- {
- PriceFeedMessage[] memory messages = new PriceFeedMessage[](2);
- messages[0].priceId = bytes32(uint(1));
- messages[0].price = 1000;
- messages[0].publishTime = 10;
- messages[1].priceId = bytes32(uint(1));
- messages[1].price = 2000;
- messages[1].publishTime = 20;
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessages(messages);
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint(1));
- PythStructs.PriceFeed[] memory priceFeeds = pyth.parsePriceFeedUpdates{
- value: updateFee
- }(updateData, priceIds, 0, 20);
- assertEq(priceFeeds.length, 1);
- assertEq(priceFeeds[0].id, bytes32(uint(1)));
- assertTrue(
- (priceFeeds[0].price.price == 1000 &&
- priceFeeds[0].price.publishTime == 10) ||
- (priceFeeds[0].price.price == 2000 &&
- priceFeeds[0].price.publishTime == 20)
- );
- }
- function testParsePriceFeedUpdatesWorksWithOverlappingMixedTimeRangeUpdates()
- public
- {
- PriceFeedMessage[] memory messages = new PriceFeedMessage[](2);
- messages[0].priceId = bytes32(uint(1));
- messages[0].price = 1000;
- messages[0].publishTime = 10;
- messages[1].priceId = bytes32(uint(1));
- messages[1].price = 2000;
- messages[1].publishTime = 20;
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessages(messages);
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint(1));
- PythStructs.PriceFeed[] memory priceFeeds = pyth.parsePriceFeedUpdates{
- value: updateFee
- }(updateData, priceIds, 5, 15);
- assertEq(priceFeeds.length, 1);
- assertEq(priceFeeds[0].id, bytes32(uint(1)));
- assertEq(priceFeeds[0].price.price, 1000);
- assertEq(priceFeeds[0].price.publishTime, 10);
- priceFeeds = pyth.parsePriceFeedUpdates{value: updateFee}(
- updateData,
- priceIds,
- 15,
- 25
- );
- assertEq(priceFeeds.length, 1);
- assertEq(priceFeeds[0].id, bytes32(uint(1)));
- assertEq(priceFeeds[0].price.price, 2000);
- assertEq(priceFeeds[0].price.publishTime, 20);
- }
- function testParsePriceFeedUpdatesRevertsIfUpdateVAAIsInvalid(
- uint seed
- ) public {
- setRandSeed(seed);
- uint numMessages = 1 + (getRandUint() % 10);
- (
- bytes32[] memory priceIds,
- PriceFeedMessage[] memory messages
- ) = generateRandomPriceMessages(numMessages);
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessagesWithConfig(
- messages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- true
- )
- );
- // It might revert due to different wormhole errors
- vm.expectRevert();
- pyth.parsePriceFeedUpdates{value: updateFee}(
- updateData,
- priceIds,
- 0,
- MAX_UINT64
- );
- }
- function testParsePriceFeedUpdatesWithSlotsStrictRevertsWithExcessUpdateData()
- public
- {
- // Create a price update with more price updates than requested price IDs
- uint numPriceIds = 2;
- uint numMessages = numPriceIds + 1; // One more than the number of price IDs
- (
- bytes32[] memory priceIds,
- PriceFeedMessage[] memory messages
- ) = generateRandomPriceMessages(numMessages);
- // Only use a subset of the price IDs to trigger the strict check
- bytes32[] memory requestedPriceIds = new bytes32[](numPriceIds);
- for (uint i = 0; i < numPriceIds; i++) {
- requestedPriceIds[i] = priceIds[i];
- }
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessages(messages);
- // Should revert in strict mode
- vm.expectRevert(PythErrors.InvalidArgument.selector);
- pyth.parsePriceFeedUpdatesWithSlotsStrict{value: updateFee}(
- updateData,
- requestedPriceIds,
- 0,
- MAX_UINT64
- );
- }
- function testParsePriceFeedUpdatesWithSlotsStrictRevertsWithFewerUpdateData()
- public
- {
- // Create a price update with fewer price updates than requested price IDs
- uint numPriceIds = 3;
- uint numMessages = numPriceIds - 1; // One less than the number of price IDs
- (
- bytes32[] memory priceIds,
- PriceFeedMessage[] memory messages
- ) = generateRandomPriceMessages(numMessages);
- // Create a larger array of requested price IDs to trigger the strict check
- bytes32[] memory requestedPriceIds = new bytes32[](numPriceIds);
- for (uint i = 0; i < numMessages; i++) {
- requestedPriceIds[i] = priceIds[i];
- }
- // Add an extra price ID that won't be in the updates
- requestedPriceIds[numMessages] = bytes32(
- uint256(keccak256(abi.encodePacked("extra_id")))
- );
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessages(messages);
- // Should revert in strict mode because we have fewer updates than price IDs
- vm.expectRevert(PythErrors.InvalidArgument.selector);
- pyth.parsePriceFeedUpdatesWithSlotsStrict{value: updateFee}(
- updateData,
- requestedPriceIds,
- 0,
- MAX_UINT64
- );
- }
- function testParsePriceFeedUpdatesRevertsIfUpdateSourceChainIsInvalid()
- public
- {
- uint numMessages = 10;
- (
- bytes32[] memory priceIds,
- PriceFeedMessage[] memory messages
- ) = generateRandomPriceMessages(numMessages);
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessagesWithConfig(
- messages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID + 1,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- vm.expectRevert(PythErrors.InvalidUpdateDataSource.selector);
- pyth.parsePriceFeedUpdates{value: updateFee}(
- updateData,
- priceIds,
- 0,
- MAX_UINT64
- );
- }
- function testParsePriceFeedUpdatesRevertsIfUpdateSourceAddressIsInvalid()
- public
- {
- uint numMessages = 10;
- (
- bytes32[] memory priceIds,
- PriceFeedMessage[] memory messages
- ) = generateRandomPriceMessages(numMessages);
- (
- bytes[] memory updateData,
- uint updateFee
- ) = createBatchedUpdateDataFromMessagesWithConfig(
- messages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- 0x00000000000000000000000000000000000000000000000000000000000000aa, // Random wrong source address
- false
- )
- );
- vm.expectRevert(PythErrors.InvalidUpdateDataSource.selector);
- pyth.parsePriceFeedUpdates{value: updateFee}(
- updateData,
- priceIds,
- 0,
- MAX_UINT64
- );
- }
- function testParseTwapPriceFeedUpdates() public {
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = basePriceIds[0];
- // Create update data directly from base TWAP messages
- bytes[] memory updateData = new bytes[](2);
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0];
- endMessages[0] = baseTwapEndMessages[0];
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- // Parse the TWAP updates
- PythStructs.TwapPriceFeed[] memory twapPriceFeeds = pyth
- .parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- // Validate results
- assertEq(twapPriceFeeds[0].id, basePriceIds[0]);
- assertEq(
- twapPriceFeeds[0].startTime,
- baseTwapStartMessages[0].publishTime
- );
- assertEq(twapPriceFeeds[0].endTime, baseTwapEndMessages[0].publishTime);
- assertEq(twapPriceFeeds[0].twap.expo, baseTwapStartMessages[0].expo);
- // Expected TWAP price: (210_000 - 100_000) / (1100 - 1000) = 1100
- assertEq(twapPriceFeeds[0].twap.price, int64(1100));
- // Expected TWAP conf: (18_000 - 10_000) / (1100 - 1000) = 80
- assertEq(twapPriceFeeds[0].twap.conf, uint64(80));
- // Validate the downSlotsRatio is 0 in our test implementation
- assertEq(twapPriceFeeds[0].downSlotsRatio, uint32(0));
- }
- function testParseTwapPriceFeedUpdatesRevertsWithInvalidUpdateDataLength()
- public
- {
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint256(1));
- // Create invalid update data with wrong length
- bytes[] memory updateData = new bytes[](1); // Should be 2
- updateData[0] = new bytes(1);
- vm.expectRevert(PythErrors.InvalidUpdateData.selector);
- pyth.parseTwapPriceFeedUpdates{value: 0}(updateData, priceIds);
- }
- function testParseTwapPriceFeedUpdatesRevertsWithMismatchedPriceIds()
- public
- {
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint256(1));
- // Copy base messages
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0];
- endMessages[0] = baseTwapEndMessages[0];
- // Change end message priceId to create mismatch
- endMessages[0].priceId = bytes32(uint256(2));
- // Create update data
- bytes[] memory updateData = new bytes[](2);
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- }
- function testParseTwapPriceFeedUpdatesRevertsWithInvalidTimeOrdering()
- public
- {
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint256(1));
- // Copy base messages
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0];
- endMessages[0] = baseTwapEndMessages[0];
- // Modify times to create invalid ordering
- startMessages[0].publishTime = 1100;
- startMessages[0].publishSlot = 1100;
- endMessages[0].publishTime = 1000;
- endMessages[0].publishSlot = 1000;
- endMessages[0].prevPublishTime = 900;
- // Create update data
- bytes[] memory updateData = new bytes[](2);
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- }
- function testParseTwapPriceFeedUpdatesRevertsWithMismatchedExponents()
- public
- {
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint256(1));
- // Copy base messages
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0];
- endMessages[0] = baseTwapEndMessages[0];
- // Change end message expo to create mismatch
- endMessages[0].expo = -6;
- // Create update data
- bytes[] memory updateData = new bytes[](2);
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- vm.expectRevert(PythErrors.InvalidTwapUpdateDataSet.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- }
- function testParseTwapPriceFeedUpdatesRevertsWithInvalidPrevPublishTime()
- public
- {
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint256(1));
- // Copy base messages
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0];
- endMessages[0] = baseTwapEndMessages[0];
- // Set invalid prevPublishTime (greater than publishTime)
- startMessages[0].prevPublishTime = 1100;
- // Create update data
- bytes[] memory updateData = new bytes[](2);
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- vm.expectRevert(PythErrors.InvalidTwapUpdateData.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- }
- function testParseTwapPriceFeedUpdatesRevertsWithInsufficientFee() public {
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = bytes32(uint256(1));
- // Copy base messages
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0];
- endMessages[0] = baseTwapEndMessages[0];
- // Create update data
- bytes[] memory updateData = new bytes[](2);
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- )
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- vm.expectRevert(PythErrors.InsufficientFee.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee - 1}(
- updateData,
- priceIds
- );
- }
- function testParseTwapPriceFeedUpdatesMultipleFeeds() public {
- bytes32[] memory priceIds = new bytes32[](2);
- priceIds[0] = basePriceIds[0];
- priceIds[1] = basePriceIds[1];
- // Create update data with both price feeds in the same updates
- bytes[] memory updateData = new bytes[](2); // Just 2 updates (start/end) for both price feeds
- // Combine both price feeds in the same messages
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](2);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 2
- );
- // Add both price feeds to the start and end messages
- startMessages[0] = baseTwapStartMessages[0];
- startMessages[1] = baseTwapStartMessages[1];
- endMessages[0] = baseTwapEndMessages[0];
- endMessages[1] = baseTwapEndMessages[1];
- // Generate Merkle updates with both price feeds included
- MerkleUpdateConfig memory config = MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- );
- // Create just 2 updates that contain both price feeds
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- config
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- config
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- // Parse the TWAP updates
- PythStructs.TwapPriceFeed[] memory twapPriceFeeds = pyth
- .parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- // Validate results for first price feed
- assertEq(twapPriceFeeds[0].id, basePriceIds[0]);
- assertEq(
- twapPriceFeeds[0].startTime,
- baseTwapStartMessages[0].publishTime
- );
- assertEq(twapPriceFeeds[0].endTime, baseTwapEndMessages[0].publishTime);
- assertEq(twapPriceFeeds[0].twap.expo, baseTwapStartMessages[0].expo);
- // Expected TWAP price: (210_000 - 100_000) / (1100 - 1000) = 1100
- assertEq(twapPriceFeeds[0].twap.price, int64(1100));
- // Expected TWAP conf: (18_000 - 10_000) / (1100 - 1000) = 80
- assertEq(twapPriceFeeds[0].twap.conf, uint64(80));
- assertEq(twapPriceFeeds[0].downSlotsRatio, uint32(0));
- // Validate results for second price feed
- assertEq(twapPriceFeeds[1].id, basePriceIds[1]);
- assertEq(
- twapPriceFeeds[1].startTime,
- baseTwapStartMessages[1].publishTime
- );
- assertEq(twapPriceFeeds[1].endTime, baseTwapEndMessages[1].publishTime);
- assertEq(twapPriceFeeds[1].twap.expo, baseTwapStartMessages[1].expo);
- // Expected TWAP price: (800_000 - 500_000) / (1100 - 1000) = 3000
- assertEq(twapPriceFeeds[1].twap.price, int64(3000));
- // Expected TWAP conf: (40_000 - 20_000) / (1100 - 1000) = 200
- assertEq(twapPriceFeeds[1].twap.conf, uint64(200));
- assertEq(twapPriceFeeds[1].downSlotsRatio, uint32(0));
- }
- function testParseTwapPriceFeedUpdatesRevertsWithMismatchedArrayLengths()
- public
- {
- // Case 1: Too many updates (more than 2)
- bytes32[] memory priceIds = new bytes32[](1);
- priceIds[0] = basePriceIds[0];
- // Create 3 updates (should only be 2)
- bytes[] memory updateData = new bytes[](3);
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0];
- endMessages[0] = baseTwapEndMessages[0];
- MerkleUpdateConfig memory config = MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- );
- // Fill with valid updates, but too many of them
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- config
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- config
- );
- updateData[2] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- config
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- vm.expectRevert(PythErrors.InvalidUpdateData.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- // Case 2: Too few updates (less than 2)
- updateData = new bytes[](1); // Only 1 update (should be 2)
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- config
- );
- updateFee = pyth.getUpdateFee(updateData);
- vm.expectRevert(PythErrors.InvalidUpdateData.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- }
- function testParseTwapPriceFeedUpdatesWithRequestedButNotFoundPriceId()
- public
- {
- // Create price IDs, including one that's not in the updates
- bytes32[] memory priceIds = new bytes32[](2);
- priceIds[0] = basePriceIds[0]; // This one exists in our updates
- priceIds[1] = bytes32(uint256(999)); // This one doesn't exist in our updates
- TwapPriceFeedMessage[]
- memory startMessages = new TwapPriceFeedMessage[](1);
- TwapPriceFeedMessage[] memory endMessages = new TwapPriceFeedMessage[](
- 1
- );
- startMessages[0] = baseTwapStartMessages[0]; // Only includes priceIds[0]
- endMessages[0] = baseTwapEndMessages[0]; // Only includes priceIds[0]
- MerkleUpdateConfig memory config = MerkleUpdateConfig(
- MERKLE_TREE_DEPTH,
- NUM_GUARDIAN_SIGNERS,
- SOURCE_EMITTER_CHAIN_ID,
- SOURCE_EMITTER_ADDRESS,
- false
- );
- bytes[] memory updateData = new bytes[](2);
- updateData[0] = generateWhMerkleTwapUpdateWithSource(
- startMessages,
- config
- );
- updateData[1] = generateWhMerkleTwapUpdateWithSource(
- endMessages,
- config
- );
- uint updateFee = pyth.getTwapUpdateFee(updateData);
- // Should revert because one of the requested price IDs is not found in the updates
- vm.expectRevert(PythErrors.PriceFeedNotFoundWithinRange.selector);
- pyth.parseTwapPriceFeedUpdates{value: updateFee}(updateData, priceIds);
- }
- }
|