@@ -108,23 +108,48 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
108108 ) public returns (bytes [][] memory updateData , uint updateFee ) {
109109 require (messages.length >= 2 , "At least 2 messages required for TWAP " );
110110
111- // Select first two messages for TWAP calculation
112- PriceFeedMessage[] memory startMessages = new PriceFeedMessage [](1 );
113- startMessages[0 ] = messages[0 ];
114-
115- PriceFeedMessage[] memory endMessages = new PriceFeedMessage [](1 );
116- endMessages[0 ] = messages[1 ];
117-
118- // Generate the update data for start and end
111+ // Create TWAP messages from regular price feed messages
112+ // For TWAP calculation, we need cumulative values that increase over time
113+ TwapPriceFeedMessage[]
114+ memory startTwapMessages = new TwapPriceFeedMessage [](1 );
115+ startTwapMessages[0 ].priceId = messages[0 ].priceId;
116+ // For test purposes, we'll set cumulative values for start message
117+ startTwapMessages[0 ].cumulativePrice = int128 (messages[0 ].price) * 1000 ;
118+ startTwapMessages[0 ].cumulativeConf = uint128 (messages[0 ].conf) * 1000 ;
119+ startTwapMessages[0 ].numDownSlots = 0 ; // No down slots for testing
120+ startTwapMessages[0 ].expo = messages[0 ].expo;
121+ startTwapMessages[0 ].publishTime = messages[0 ].publishTime;
122+ startTwapMessages[0 ].prevPublishTime = messages[0 ].prevPublishTime;
123+ startTwapMessages[0 ].publishSlot = 1000 ; // Start slot
124+
125+ TwapPriceFeedMessage[]
126+ memory endTwapMessages = new TwapPriceFeedMessage [](1 );
127+ endTwapMessages[0 ].priceId = messages[1 ].priceId;
128+ // For end message, make sure cumulative values are higher than start
129+ endTwapMessages[0 ].cumulativePrice =
130+ int128 (messages[1 ].price) *
131+ 1000 +
132+ startTwapMessages[0 ].cumulativePrice;
133+ endTwapMessages[0 ].cumulativeConf =
134+ uint128 (messages[1 ].conf) *
135+ 1000 +
136+ startTwapMessages[0 ].cumulativeConf;
137+ endTwapMessages[0 ].numDownSlots = 0 ; // No down slots for testing
138+ endTwapMessages[0 ].expo = messages[1 ].expo;
139+ endTwapMessages[0 ].publishTime = messages[1 ].publishTime;
140+ endTwapMessages[0 ].prevPublishTime = messages[1 ].prevPublishTime;
141+ endTwapMessages[0 ].publishSlot = 1100 ; // End slot (100 slots after start)
142+
143+ // Generate the update data for start and end using the TWAP-specific function
119144 bytes [] memory startUpdateData = new bytes [](1 );
120- startUpdateData[0 ] = generateWhMerkleUpdateWithSource (
121- startMessages ,
145+ startUpdateData[0 ] = generateWhMerkleTwapUpdateWithSource (
146+ startTwapMessages ,
122147 config
123148 );
124149
125150 bytes [] memory endUpdateData = new bytes [](1 );
126- endUpdateData[0 ] = generateWhMerkleUpdateWithSource (
127- endMessages ,
151+ endUpdateData[0 ] = generateWhMerkleTwapUpdateWithSource (
152+ endTwapMessages ,
128153 config
129154 );
130155
@@ -400,10 +425,6 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
400425 uint updateFee
401426 ) = createBatchedTwapUpdateDataFromMessages (messages);
402427
403- // log the updateData
404- console.logBytes (updateData[0 ][0 ]);
405- console.logBytes (updateData[1 ][0 ]);
406-
407428 // Parse the TWAP updates
408429 PythStructs.TwapPriceFeed[] memory twapPriceFeeds = pyth
409430 .parseTwapPriceFeedUpdates {value: updateFee}(updateData, priceIds);
@@ -414,11 +435,18 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
414435 assertEq (twapPriceFeeds[0 ].endTime, uint64 (1100 )); // publishTime end
415436 assertEq (twapPriceFeeds[0 ].twap.expo, int32 (- 8 )); // expo
416437
417- // The TWAP price should be the difference in cumulative price divided by the slot difference
418- assertEq (twapPriceFeeds[0 ].twap.price, int64 (105 ));
419-
420- // The TWAP conf should be the difference in cumulative conf divided by the slot difference
421- assertEq (twapPriceFeeds[0 ].twap.conf, uint64 (9 ));
438+ // Expected TWAP price calculation:
439+ // (endCumulativePrice - startCumulativePrice) / (endSlot - startSlot)
440+ // ((110 * 1000 + 100 * 1000) - (100 * 1000)) / (1100 - 1000)
441+ // = (210000 - 100000) / 100 = 1100
442+ // The smart contract will convert this to int64, and our calculation simplified for clarity
443+ assertEq (twapPriceFeeds[0 ].twap.price, int64 (1100 ));
444+
445+ // Expected TWAP conf calculation:
446+ // (endCumulativeConf - startCumulativeConf) / (endSlot - startSlot)
447+ // ((8 * 1000 + 10 * 1000) - (10 * 1000)) / (1100 - 1000)
448+ // = (18000 - 10000) / 100 = 80
449+ assertEq (twapPriceFeeds[0 ].twap.conf, uint64 (80 ));
422450
423451 // Validate the downSlotsRatio is 0 in our test implementation
424452 assertEq (twapPriceFeeds[0 ].downSlotsRatio, uint32 (0 ));
0 commit comments