@@ -170,9 +170,6 @@ contract MockPyth is AbstractPyth {
170
170
override
171
171
returns (PythStructs.TwapPriceFeed[] memory twapPriceFeeds )
172
172
{
173
- // Validate inputs and fee
174
- if (updateData.length != 2 ) revert PythErrors.InvalidUpdateData ();
175
-
176
173
uint requiredFee = getUpdateFee (updateData);
177
174
if (msg .value < requiredFee) revert PythErrors.InsufficientFee ();
178
175
@@ -186,103 +183,82 @@ contract MockPyth is AbstractPyth {
186
183
return twapPriceFeeds;
187
184
}
188
185
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
+ // ]
212
196
function processTwapPriceFeed (
213
197
bytes [] calldata updateData ,
214
198
bytes32 priceId ,
215
199
uint index ,
216
200
PythStructs.TwapPriceFeed[] memory twapPriceFeeds
217
201
) 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)
224
206
);
225
207
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)
236
210
revert PythErrors.InvalidTwapUpdateDataSet ();
237
- }
238
211
239
- // Calculate TWAP
240
- twapPriceFeeds[index] = PythUtils.calculateTwap (
241
- priceId,
242
- startInfo,
243
- endInfo
244
- );
212
+ // Store the TWAP feed
213
+ twapPriceFeeds[index] = twapFeed;
245
214
246
215
// Emit event
247
216
emit TwapPriceFeedUpdate (
248
217
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
254
223
);
255
224
}
256
225
257
226
/**
258
227
* @notice Creates TWAP price feed update data with simplified parameters for testing
259
228
* @param id The price feed ID
229
+ * @param startTime Start time of the TWAP
230
+ * @param endTime End time of the TWAP
260
231
* @param price The price value
261
232
* @param conf The confidence interval
262
233
* @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
265
235
* @return twapData Encoded TWAP price feed data ready for parseTwapPriceFeedUpdates
266
236
*/
267
237
function createTwapPriceFeedUpdateData (
268
238
bytes32 id ,
239
+ uint startTime ,
240
+ uint endTime ,
269
241
int64 price ,
270
242
uint64 conf ,
271
243
int32 expo ,
272
- uint64 publishTime ,
273
- uint64 publishSlot
244
+ uint32 downSlotsRatio
274
245
) 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);
286
262
}
287
263
288
264
function createPriceFeedUpdateData (
0 commit comments