Skip to content

Commit 8a6f154

Browse files
committed
feat: refactor TWAP update logic to decode price feed data and validate IDs
1 parent 371350a commit 8a6f154

File tree

1 file changed

+30
-80
lines changed

1 file changed

+30
-80
lines changed

target_chains/ethereum/sdk/solidity/MockPyth.sol

Lines changed: 30 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -215,63 +215,23 @@ contract MockPyth is AbstractPyth {
215215
uint index,
216216
PythStructs.TwapPriceFeed[] memory twapPriceFeeds
217217
) private {
218-
// Find start price feed
219-
PythStructs.PriceFeed memory startFeed;
220-
uint64 startPrevPublishTime;
221-
bool foundStart;
222-
(startFeed, startPrevPublishTime, foundStart) = findPriceFeed(
223-
updateData,
224-
priceId,
225-
0
226-
);
227-
if (!foundStart) revert PythErrors.PriceFeedNotFoundWithinRange();
228-
229-
// Find end price feed
230-
PythStructs.PriceFeed memory endFeed;
231-
uint64 endPrevPublishTime;
232-
bool foundEnd;
233-
(endFeed, endPrevPublishTime, foundEnd) = findPriceFeed(
234-
updateData,
235-
priceId,
236-
1
218+
// Decode start and end TWAP info
219+
(bytes32 startId, PythStructs.TwapPriceInfo memory startInfo) = abi
220+
.decode(updateData[0], (bytes32, PythStructs.TwapPriceInfo));
221+
(bytes32 endId, PythStructs.TwapPriceInfo memory endInfo) = abi.decode(
222+
updateData[1],
223+
(bytes32, PythStructs.TwapPriceInfo)
237224
);
238-
if (!foundEnd) revert PythErrors.PriceFeedNotFoundWithinRange();
239225

240-
validateAndCalculateTwap(
241-
priceId,
242-
startFeed,
243-
endFeed,
244-
startPrevPublishTime,
245-
endPrevPublishTime,
246-
index,
247-
twapPriceFeeds
248-
);
249-
}
226+
// Validate IDs match
227+
if (startId != priceId || endId != priceId)
228+
revert PythErrors.InvalidTwapUpdateDataSet();
250229

251-
function validateAndCalculateTwap(
252-
bytes32 priceId,
253-
PythStructs.PriceFeed memory startFeed,
254-
PythStructs.PriceFeed memory endFeed,
255-
uint64 startPrevPublishTime,
256-
uint64 endPrevPublishTime,
257-
uint index,
258-
PythStructs.TwapPriceFeed[] memory twapPriceFeeds
259-
) private {
260230
// Validate time ordering
261-
if (startFeed.price.publishTime >= endFeed.price.publishTime) {
231+
if (startInfo.publishTime >= endInfo.publishTime) {
262232
revert PythErrors.InvalidTwapUpdateDataSet();
263233
}
264234

265-
// Convert to TwapPriceInfo
266-
PythStructs.TwapPriceInfo memory startInfo = createMockTwapInfo(
267-
startFeed,
268-
startPrevPublishTime
269-
);
270-
PythStructs.TwapPriceInfo memory endInfo = createMockTwapInfo(
271-
endFeed,
272-
endPrevPublishTime
273-
);
274-
275235
if (startInfo.publishSlot >= endInfo.publishSlot) {
276236
revert PythErrors.InvalidTwapUpdateDataSet();
277237
}
@@ -294,36 +254,26 @@ contract MockPyth is AbstractPyth {
294254
);
295255
}
296256

297-
function createMockTwapInfo(
298-
PythStructs.PriceFeed memory feed,
299-
uint64 prevPublishTime
300-
) internal pure returns (PythStructs.TwapPriceInfo memory mockInfo) {
301-
// Set basic fields
302-
mockInfo.expo = feed.price.expo;
303-
mockInfo.publishTime = uint64(feed.price.publishTime);
304-
mockInfo.prevPublishTime = prevPublishTime;
305-
306-
// Use publishTime as publishSlot in mock implementation
307-
// In real implementation, this would be actual slot number
308-
mockInfo.publishSlot = uint64(feed.price.publishTime);
309-
310-
// Calculate cumulative values
311-
// In mock implementation, we simulate cumulative values by multiplying current values by slot
312-
// This creates a linear accumulation which is sufficient for testing
313-
// In real implementation, these would be actual accumulated values over time
314-
mockInfo.cumulativePrice =
315-
int128(feed.price.price) *
316-
int128(uint128(mockInfo.publishSlot));
317-
mockInfo.cumulativeConf =
318-
uint128(feed.price.conf) *
319-
uint128(mockInfo.publishSlot);
320-
321-
// Set number of down slots
322-
// In mock implementation we default to 0 down slots
323-
// In real implementation this would track actual network downtime
324-
mockInfo.numDownSlots = 0;
325-
326-
return mockInfo;
257+
function createTwapPriceFeedUpdateData(
258+
bytes32 id,
259+
int128 cumulativePrice,
260+
uint128 cumulativeConf,
261+
uint64 numDownSlots,
262+
int32 expo,
263+
uint64 publishTime,
264+
uint64 prevPublishTime,
265+
uint64 publishSlot
266+
) public pure returns (bytes memory twapData) {
267+
PythStructs.TwapPriceInfo memory twapInfo;
268+
twapInfo.cumulativePrice = cumulativePrice;
269+
twapInfo.cumulativeConf = cumulativeConf;
270+
twapInfo.numDownSlots = numDownSlots;
271+
twapInfo.expo = expo;
272+
twapInfo.publishTime = publishTime;
273+
twapInfo.prevPublishTime = prevPublishTime;
274+
twapInfo.publishSlot = publishSlot;
275+
276+
twapData = abi.encode(id, twapInfo);
327277
}
328278

329279
function createPriceFeedUpdateData(

0 commit comments

Comments
 (0)