Skip to content

Commit 83ad4d6

Browse files
committed
feat: simplify TWAP price feed processing and update data creation in MockPyth contract
1 parent f19d2c9 commit 83ad4d6

File tree

1 file changed

+45
-69
lines changed

1 file changed

+45
-69
lines changed

target_chains/ethereum/sdk/solidity/MockPyth.sol

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ contract MockPyth is AbstractPyth {
170170
override
171171
returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds)
172172
{
173-
// Validate inputs and fee
174-
if (updateData.length != 2) revert PythErrors.InvalidUpdateData();
175-
176173
uint requiredFee = getUpdateFee(updateData);
177174
if (msg.value < requiredFee) revert PythErrors.InsufficientFee();
178175

@@ -186,103 +183,82 @@ contract MockPyth is AbstractPyth {
186183
return twapPriceFeeds;
187184
}
188185

189-
function findPriceFeed(
190-
bytes[] calldata updateData,
191-
bytes32 priceId,
192-
uint index
193-
)
194-
private
195-
pure
196-
returns (
197-
PythStructs.PriceFeed memory feed,
198-
uint64 prevPublishTime,
199-
bool found
200-
)
201-
{
202-
(feed, prevPublishTime) = abi.decode(
203-
updateData[index],
204-
(PythStructs.PriceFeed, uint64)
205-
);
206-
207-
if (feed.id == priceId) {
208-
found = true;
209-
}
210-
}
211-
186+
// You can create this data either by calling createTwapPriceFeedUpdateData.
187+
// @note: The updateData expected here is different from the one used in the main contract.
188+
// In particular, the expected format is:
189+
// [
190+
// abi.encode(
191+
// bytes32 id,
192+
// PythStructs.TwapPriceInfo startInfo,
193+
// PythStructs.TwapPriceInfo endInfo
194+
// )
195+
// ]
212196
function processTwapPriceFeed(
213197
bytes[] calldata updateData,
214198
bytes32 priceId,
215199
uint index,
216200
PythStructs.TwapPriceFeed[] memory twapPriceFeeds
217201
) private {
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)
202+
// Decode TWAP feed directly
203+
PythStructs.TwapPriceFeed memory twapFeed = abi.decode(
204+
updateData[0],
205+
(PythStructs.TwapPriceFeed)
224206
);
225207

226-
// Validate IDs match
227-
if (startId != priceId || endId != priceId)
228-
revert PythErrors.InvalidTwapUpdateDataSet();
229-
230-
// Validate time ordering
231-
if (startInfo.publishTime >= endInfo.publishTime) {
232-
revert PythErrors.InvalidTwapUpdateDataSet();
233-
}
234-
235-
if (startInfo.publishSlot >= endInfo.publishSlot) {
208+
// Validate ID matches
209+
if (twapFeed.id != priceId)
236210
revert PythErrors.InvalidTwapUpdateDataSet();
237-
}
238211

239-
// Calculate TWAP
240-
twapPriceFeeds[index] = PythUtils.calculateTwap(
241-
priceId,
242-
startInfo,
243-
endInfo
244-
);
212+
// Store the TWAP feed
213+
twapPriceFeeds[index] = twapFeed;
245214

246215
// Emit event
247216
emit TwapPriceFeedUpdate(
248217
priceId,
249-
startInfo.publishTime,
250-
endInfo.publishTime,
251-
twapPriceFeeds[index].twap.price,
252-
twapPriceFeeds[index].twap.conf,
253-
twapPriceFeeds[index].downSlotsRatio
218+
twapFeed.startTime,
219+
twapFeed.endTime,
220+
twapFeed.twap.price,
221+
twapFeed.twap.conf,
222+
twapFeed.downSlotsRatio
254223
);
255224
}
256225

257226
/**
258227
* @notice Creates TWAP price feed update data with simplified parameters for testing
259228
* @param id The price feed ID
229+
* @param startTime Start time of the TWAP
230+
* @param endTime End time of the TWAP
260231
* @param price The price value
261232
* @param conf The confidence interval
262233
* @param expo Price exponent
263-
* @param publishTime Timestamp when price was published
264-
* @param publishSlot Slot number for this update
234+
* @param downSlotsRatio Down slots ratio
265235
* @return twapData Encoded TWAP price feed data ready for parseTwapPriceFeedUpdates
266236
*/
267237
function createTwapPriceFeedUpdateData(
268238
bytes32 id,
239+
uint startTime,
240+
uint endTime,
269241
int64 price,
270242
uint64 conf,
271243
int32 expo,
272-
uint64 publishTime,
273-
uint64 publishSlot
244+
uint32 downSlotsRatio
274245
) public pure returns (bytes memory twapData) {
275-
PythStructs.TwapPriceInfo memory twapInfo;
276-
// Calculate cumulative values based on single price point
277-
twapInfo.cumulativePrice = int128(price);
278-
twapInfo.cumulativeConf = uint128(conf);
279-
twapInfo.numDownSlots = 0; // Assume no down slots for test data
280-
twapInfo.expo = expo;
281-
twapInfo.publishTime = publishTime;
282-
twapInfo.prevPublishTime = publishTime > 60 ? publishTime - 60 : 0; // Set a reasonable previous time
283-
twapInfo.publishSlot = publishSlot;
284-
285-
twapData = abi.encode(id, twapInfo);
246+
PythStructs.Price memory twapPrice = PythStructs.Price({
247+
price: price,
248+
conf: conf,
249+
expo: expo,
250+
publishTime: endTime
251+
});
252+
253+
PythStructs.TwapPriceFeed memory twapFeed = PythStructs.TwapPriceFeed({
254+
id: id,
255+
startTime: startTime,
256+
endTime: endTime,
257+
twap: twapPrice,
258+
downSlotsRatio: downSlotsRatio
259+
});
260+
261+
twapData = abi.encode(twapFeed);
286262
}
287263

288264
function createPriceFeedUpdateData(

0 commit comments

Comments
 (0)