@@ -304,70 +304,50 @@ contract TestOracleMiddlewareParseAndValidatePriceRealData is OracleMiddlewareBa
304
304
}
305
305
306
306
/**
307
- * @custom:scenario Use cached Pyth value for `initiate` actions if possible
308
- * @custom:given A pyth signature was provided to the oracle more recently than the latest chainlink on-chain data
309
- * @custom:when A user retrieves a price for a `initiate` action without providing data
310
- * @custom:then The price retrieved by the oracle middleware is the one from pyth
307
+ * @custom:scenario Uses the cached Pyth value for `initiate` actions.
308
+ * @custom:given A pyth signature was provided to the oracle more recently than the last chainlink price.
309
+ * @custom:when A user retrieves a price for a `initiate` action without providing data.
310
+ * @custom:then The price retrieved by the oracle middleware must be equal to the unsafe Pyth price.
311
311
*/
312
312
function test_ForkFFIUseCachedPythPrice () public ethMainnetFork reSetUp {
313
- // chainlink data
313
+ vm. rollFork (PYTH_PRICE_BLOCK_NUMBER);
314
314
(uint256 chainlinkPrice , uint256 chainlinkTimestamp ) = getChainlinkPrice ();
315
315
316
- // get pyth price that must be more recent than chainlink data
317
- (,,,, bytes memory data ) = getHermesApiSignature (PYTH_ETH_USD, chainlinkTimestamp + 1 );
318
- uint256 validationCost = oracleMiddleware.validationCost (data, ProtocolAction.ValidateDeposit);
319
-
320
- // submit to oracle middleware so it gets cached by Pyth
321
- vm.warp (chainlinkTimestamp + 2 );
322
- PriceInfo memory middlewarePrice = oracleMiddleware.parseAndValidatePrice { value: validationCost }(
323
- "" ,
324
- uint128 (chainlinkTimestamp + 1 - oracleMiddleware.getValidationDelay ()),
325
- ProtocolAction.ValidateDeposit,
326
- data
327
- );
316
+ PythStructs.Price memory unsafePythPrice = getPythUnsafePrice ();
317
+
318
+ uint256 adjustedUnsafePythPrice =
319
+ uint64 (unsafePythPrice.price) * 10 ** uint32 (unsafePythPrice.expo + int8 (oracleMiddleware.getDecimals ()));
328
320
329
321
// get oracle middleware price without providing data
330
- PriceInfo memory cachedMiddlewarePrice =
322
+ PriceInfo memory middlewarePrice =
331
323
oracleMiddleware.parseAndValidatePrice ("" , uint128 (block .timestamp ), ProtocolAction.InitiateDeposit, "" );
332
324
333
325
// timestamp check
334
- assertEq (cachedMiddlewarePrice .timestamp, middlewarePrice.timestamp , "timestamp equal to pyth timestamp " );
335
- assertGt (cachedMiddlewarePrice .timestamp, chainlinkTimestamp, "timestamp greater than chainlink timestamp " );
326
+ assertEq (middlewarePrice .timestamp, unsafePythPrice.publishTime , "timestamp equal to pyth unsafe timestamp " );
327
+ assertGt (middlewarePrice .timestamp, chainlinkTimestamp, "timestamp greater than chainlink timestamp " );
336
328
// price check
337
- assertEq (cachedMiddlewarePrice .neutralPrice, middlewarePrice.neutralPrice , "price equal to pyth price " );
338
- assertTrue (cachedMiddlewarePrice .neutralPrice != chainlinkPrice, "price different from chainlink price " );
329
+ assertEq (middlewarePrice .neutralPrice, adjustedUnsafePythPrice , "price equal to pyth price " );
330
+ assertTrue (middlewarePrice .neutralPrice != chainlinkPrice, "price different from chainlink price " );
339
331
}
340
332
341
333
/**
342
- * @custom:scenario The cached Pyth price for initiate action is too old
343
- * @custom:given A pyth signature was provided to the oracle more recently than the last chainlink price
344
- * @custom:and The chainlink price is too old (there is a problem with chainlink)
345
- * @custom:when A user retrieves a price for a `initiate` action without providing data
334
+ * @custom:scenario The cached Pyth price for initiate action is too old.
335
+ * @custom:given A pyth signature was provided to the oracle more recently than the last chainlink price.
336
+ * @custom:and The chainlink price is too old (there is a problem with chainlink).
337
+ * @custom:when A user retrieves a price for a `initiate` action without providing data.
346
338
* @custom:then The price is retrieved from Pyth but checked for freshness and the transaction reverts with
347
- * `OracleMiddlewarePriceTooOld`
339
+ * `OracleMiddlewarePriceTooOld`.
348
340
*/
349
341
function test_RevertWhen_ForkFFIOldCachedPythPrice () public ethMainnetFork reSetUp {
350
- // chainlink data
351
- (, uint256 chainlinkTimestamp ) = getChainlinkPrice ();
352
-
353
- // get pyth price that must be more recent than chainlink data
354
- (,,,, bytes memory data ) = getHermesApiSignature (PYTH_ETH_USD, chainlinkTimestamp + 1 );
355
- uint256 validationCost = oracleMiddleware.validationCost (data, ProtocolAction.ValidateDeposit);
356
-
357
- // submit to oracle middleware so it gets cached by Pyth
358
- vm.warp (chainlinkTimestamp + 2 );
359
- oracleMiddleware.parseAndValidatePrice { value: validationCost }(
360
- "" ,
361
- uint128 (chainlinkTimestamp + 1 - oracleMiddleware.getValidationDelay ()),
362
- ProtocolAction.ValidateDeposit,
363
- data
364
- );
342
+ vm.rollFork (PYTH_PRICE_BLOCK_NUMBER);
343
+ skip (oracleMiddleware.getChainlinkTimeElapsedLimit ());
365
344
366
- // wait for more than _timeElapsedLimit from the middleware
367
- skip (oracleMiddleware.getChainlinkTimeElapsedLimit () + 1 );
345
+ PythStructs.Price memory unsafePythPrice = getPythUnsafePrice ();
368
346
369
347
// get oracle middleware price without providing data
370
- vm.expectRevert (abi.encodeWithSelector (OracleMiddlewarePriceTooOld.selector , chainlinkTimestamp + 1 ));
348
+ vm.expectRevert (
349
+ abi.encodeWithSelector (OracleMiddlewarePriceTooOld.selector , uint64 (unsafePythPrice.publishTime))
350
+ );
371
351
oracleMiddleware.parseAndValidatePrice ("" , uint128 (block .timestamp ), ProtocolAction.InitiateDeposit, "" );
372
352
}
373
353
0 commit comments