|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity 0.8.26; |
| 3 | + |
| 4 | +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | + |
| 6 | +import { EMPTY_STREAM_V3, EMPTY_STREAM_V4 } from "../../utils/Constants.sol"; |
| 7 | +import { OracleMiddlewareWithDataStreamsFixture } from "../../utils/Fixtures.sol"; |
| 8 | + |
| 9 | +import { FormattedDataStreamsPrice } from "../../../../../src/interfaces/OracleMiddleware/IOracleMiddlewareTypes.sol"; |
| 10 | + |
| 11 | +/// @custom:feature The `_getChainlinkDataStreamPrice` function of the `ChainlinkDataStreamsOracle`. |
| 12 | +contract TestChainlinkDataStreamsOracleGetPrice is OracleMiddlewareWithDataStreamsFixture { |
| 13 | + function setUp() public override { |
| 14 | + super.setUp(); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with a large fee. |
| 19 | + * @custom:when The function is called with a large fee. |
| 20 | + * @custom:then The call should revert with `OracleMiddlewareDataStreamFeeSafeguard`. |
| 21 | + */ |
| 22 | + function test_RevertWhen_getChainlinkDataStreamPriceFeeSafeguard() public { |
| 23 | + report.nativeFee = type(uint192).max; |
| 24 | + (, payload) = _encodeReport(report); |
| 25 | + vm.expectRevert(abi.encodeWithSelector(OracleMiddlewareDataStreamFeeSafeguard.selector, report.nativeFee)); |
| 26 | + oracleMiddleware.i_getChainlinkDataStreamPrice(payload, 0, 0); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with an incorrect fee. |
| 31 | + * @custom:when The function is called with an incorrect fee. |
| 32 | + * @custom:then The call should revert with `OracleMiddlewareIncorrectFee`. |
| 33 | + */ |
| 34 | + function test_RevertWhen_getChainlinkDataStreamPriceIncorrectFee() public { |
| 35 | + vm.expectRevert(OracleMiddlewareIncorrectFee.selector); |
| 36 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee + 1 }(payload, 0, 0); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with an invalid report version. |
| 41 | + * @custom:when The function is called with an invalid report version. |
| 42 | + * @custom:then The call should revert with `OracleMiddlewareInvalidReportVersion`. |
| 43 | + */ |
| 44 | + function test_RevertWhen_getChainlinkDataStreamPriceIncorrectReportVersion() public { |
| 45 | + report.feedId = EMPTY_STREAM_V4; |
| 46 | + (, payload) = _encodeReport(report); |
| 47 | + vm.expectRevert(OracleMiddlewareInvalidReportVersion.selector); |
| 48 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }(payload, 0, 0); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with an invalid stream id. |
| 53 | + * @custom:when The function is called with an invalid stream id. |
| 54 | + * @custom:then The call should revert with `OracleMiddlewareInvalidStreamId`. |
| 55 | + */ |
| 56 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidStreamId() public { |
| 57 | + report.feedId = bytes32(uint256(EMPTY_STREAM_V3) | 1); |
| 58 | + (, payload) = _encodeReport(report); |
| 59 | + vm.expectRevert(OracleMiddlewareInvalidStreamId.selector); |
| 60 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }(payload, 0, 0); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with an invalid timestamp |
| 65 | + * payload that lacks a target timestamp. |
| 66 | + * @custom:when The function is called. |
| 67 | + * @custom:then The call should revert with `OracleMiddlewareDataStreamInvalidTimestamp`. |
| 68 | + */ |
| 69 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidTimestampWithoutTargetTimestamp() public { |
| 70 | + report.validFromTimestamp = 0; |
| 71 | + (, payload) = _encodeReport(report); |
| 72 | + vm.expectRevert(OracleMiddlewareDataStreamInvalidTimestamp.selector); |
| 73 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }(payload, 0, 0); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with an invalid timestamp |
| 78 | + * payload and a target timestamp that is lower than the report's `validFromTimestamp`. |
| 79 | + * @custom:when The function is called. |
| 80 | + * @custom:then The call should revert with `OracleMiddlewareDataStreamInvalidTimestamp`. |
| 81 | + */ |
| 82 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidTimestampWithTargetTimestampLtValidFromTimestamp() |
| 83 | + public |
| 84 | + { |
| 85 | + vm.expectRevert(OracleMiddlewareDataStreamInvalidTimestamp.selector); |
| 86 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }( |
| 87 | + payload, report.validFromTimestamp - 1, 0 |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with an invalid timestamp |
| 93 | + * payload and a target timestamp that is greater than the report's `observationsTimestamp`. |
| 94 | + * @custom:when The function is called. |
| 95 | + * @custom:then The call should revert with `OracleMiddlewareDataStreamInvalidTimestamp`. |
| 96 | + */ |
| 97 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidTimestampWithTargetTimestampGtObservationsTimestamp() |
| 98 | + public |
| 99 | + { |
| 100 | + vm.expectRevert(OracleMiddlewareDataStreamInvalidTimestamp.selector); |
| 101 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }( |
| 102 | + payload, report.observationsTimestamp + 1, 0 |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with an invalid timestamp |
| 108 | + * payload and a target limit that is lower than the report's `observationsTimestamp`. |
| 109 | + * @custom:when The function is called. |
| 110 | + * @custom:then The call should revert with `OracleMiddlewareDataStreamInvalidTimestamp`. |
| 111 | + */ |
| 112 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidTimestampWithTargetLimitLtObservationsTimestamp() |
| 113 | + public |
| 114 | + { |
| 115 | + vm.expectRevert(OracleMiddlewareDataStreamInvalidTimestamp.selector); |
| 116 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }( |
| 117 | + payload, report.validFromTimestamp, report.observationsTimestamp - 1 |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with a report price equal to zero. |
| 123 | + * @custom:when The function is called. |
| 124 | + * @custom:then The call should revert with `OracleMiddlewareWrongPrice`. |
| 125 | + */ |
| 126 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidTimestampWithInvalidPrice() public { |
| 127 | + report.price = 0; |
| 128 | + (, payload) = _encodeReport(report); |
| 129 | + vm.expectRevert(abi.encodeWithSelector(OracleMiddlewareWrongPrice.selector, report.price)); |
| 130 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }(payload, 0, 0); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with a report ask price equal to zero. |
| 135 | + * @custom:when The function is called. |
| 136 | + * @custom:then The call should revert with `OracleMiddlewareWrongAskPrice`. |
| 137 | + */ |
| 138 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidTimestampWithInvalidAskPrice() public { |
| 139 | + report.ask = 0; |
| 140 | + (, payload) = _encodeReport(report); |
| 141 | + vm.expectRevert(abi.encodeWithSelector(OracleMiddlewareWrongAskPrice.selector, report.ask)); |
| 142 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }(payload, 0, 0); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function with a report bid price equal to zero. |
| 147 | + * @custom:when The function is called. |
| 148 | + * @custom:then The call should revert with `OracleMiddlewareWrongBidPrice`. |
| 149 | + */ |
| 150 | + function test_RevertWhen_getChainlinkDataStreamPriceInvalidTimestampWithInvalidBidPrice() public { |
| 151 | + report.bid = 0; |
| 152 | + (, payload) = _encodeReport(report); |
| 153 | + vm.expectRevert(abi.encodeWithSelector(OracleMiddlewareWrongBidPrice.selector, report.bid)); |
| 154 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }(payload, 0, 0); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @custom:scenario Tests the `_getChainlinkDataStreamPrice` function. |
| 159 | + * @custom:when The function is called. |
| 160 | + * @custom:then The verified report must match the Chainlink data streams report. |
| 161 | + */ |
| 162 | + function test_getChainlinkDataStreamPrice() public { |
| 163 | + FormattedDataStreamsPrice memory formattedReport = |
| 164 | + oracleMiddleware.i_getChainlinkDataStreamPrice{ value: report.nativeFee }(payload, 0, 0); |
| 165 | + |
| 166 | + assertEq(formattedReport.timestamp, report.observationsTimestamp, "Invalid observationsTimestamp"); |
| 167 | + assertEq(int192(int256(formattedReport.price)), report.price, "Invalid price"); |
| 168 | + assertEq(int192(int256(formattedReport.bid)), report.bid, "Invalid bid"); |
| 169 | + assertEq(int192(int256(formattedReport.ask)), report.ask, "Invalid ask"); |
| 170 | + } |
| 171 | +} |
0 commit comments