Skip to content

Commit 5b027c2

Browse files
authored
test: fix unsafe pyth (#899)
* test: fix unsafe pyth * test: fix abs
1 parent fc87703 commit 5b027c2

File tree

1 file changed

+25
-45
lines changed

1 file changed

+25
-45
lines changed

test/integration/Middlewares/Oracle/ParseAndValidatePrice.t.sol

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -304,70 +304,50 @@ contract TestOracleMiddlewareParseAndValidatePriceRealData is OracleMiddlewareBa
304304
}
305305

306306
/**
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.
311311
*/
312312
function test_ForkFFIUseCachedPythPrice() public ethMainnetFork reSetUp {
313-
// chainlink data
313+
vm.rollFork(PYTH_PRICE_BLOCK_NUMBER);
314314
(uint256 chainlinkPrice, uint256 chainlinkTimestamp) = getChainlinkPrice();
315315

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()));
328320

329321
// get oracle middleware price without providing data
330-
PriceInfo memory cachedMiddlewarePrice =
322+
PriceInfo memory middlewarePrice =
331323
oracleMiddleware.parseAndValidatePrice("", uint128(block.timestamp), ProtocolAction.InitiateDeposit, "");
332324

333325
// 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");
336328
// 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");
339331
}
340332

341333
/**
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.
346338
* @custom:then The price is retrieved from Pyth but checked for freshness and the transaction reverts with
347-
* `OracleMiddlewarePriceTooOld`
339+
* `OracleMiddlewarePriceTooOld`.
348340
*/
349341
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());
365344

366-
// wait for more than _timeElapsedLimit from the middleware
367-
skip(oracleMiddleware.getChainlinkTimeElapsedLimit() + 1);
345+
PythStructs.Price memory unsafePythPrice = getPythUnsafePrice();
368346

369347
// 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+
);
371351
oracleMiddleware.parseAndValidatePrice("", uint128(block.timestamp), ProtocolAction.InitiateDeposit, "");
372352
}
373353

0 commit comments

Comments
 (0)