|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import "@api3/contracts/interfaces/IApi3ReaderProxy.sol"; |
| 5 | +import "./interfaces/IPriceCappedApi3ReaderProxyV1.sol"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @title An immutable proxy contract that provides a price bounding mechanism. |
| 9 | + * It reads the price from the underlying Api3 proxy and if this price falls |
| 10 | + * outside a predefined `lowerBound` and `upperBound`, this contract will report |
| 11 | + * the respective bound instead. |
| 12 | + * This is primarily intended for assets (e.g., stablecoins) where a protocol |
| 13 | + * wants to limit the price range it ingests for risk management purposes. |
| 14 | + * @dev `lowerBound` and `upperBound` are immutable and set during deployment. |
| 15 | + * To set only an upper bound, `lowerBound_` can be set to 0. |
| 16 | + * To set only a lower bound, `upperBound_` can be set to `type(int224).max`. |
| 17 | + * To configure a fixed price, set `lowerBound_` and `upperBound_` to the |
| 18 | + * same desired price. |
| 19 | + * If `lowerBound_` is 0 and `upperBound_` is `type(int224).max`, no effective |
| 20 | + * capping occurs, though negative prices from the underlying proxy would be |
| 21 | + * floored at 0 if `lowerBound_` is 0. |
| 22 | + */ |
| 23 | +contract PriceCappedApi3ReaderProxyV1 is IPriceCappedApi3ReaderProxyV1 { |
| 24 | + /// @notice IApi3ReaderProxy contract address |
| 25 | + address public immutable override proxy; |
| 26 | + |
| 27 | + /// @notice The minimum price (inclusive) that this proxy will report. |
| 28 | + int224 public immutable override lowerBound; |
| 29 | + |
| 30 | + /// @notice The maximum price (inclusive) that this proxy will report. |
| 31 | + int224 public immutable override upperBound; |
| 32 | + |
| 33 | + /// @param proxy_ IApi3ReaderProxy contract address |
| 34 | + /// @param lowerBound_ The minimum price (inclusive) this proxy will report |
| 35 | + /// @param upperBound_ The maximum price (inclusive) this proxy will report |
| 36 | + constructor(address proxy_, int224 lowerBound_, int224 upperBound_) { |
| 37 | + if (proxy_ == address(0)) { |
| 38 | + revert ZeroProxyAddress(); |
| 39 | + } |
| 40 | + if (lowerBound_ < 0) { |
| 41 | + revert LowerBoundMustBeNonNegative(); |
| 42 | + } |
| 43 | + if (upperBound_ < lowerBound_) { |
| 44 | + revert UpperBoundMustBeGreaterOrEqualToLowerBound(); |
| 45 | + } |
| 46 | + proxy = proxy_; |
| 47 | + lowerBound = lowerBound_; |
| 48 | + upperBound = upperBound_; |
| 49 | + } |
| 50 | + |
| 51 | + /// @notice Reads the current value and timestamp from the underlying |
| 52 | + /// `IApi3ReaderProxy` and applies the price bounds. |
| 53 | + /// @dev If the `baseValue` from the underlying proxy is less than |
| 54 | + /// `lowerBound`, then `lowerBound` is returned as the `value`. If |
| 55 | + /// `baseValue` is greater than `upperBound`, then `upperBound` is returned. |
| 56 | + /// Otherwise, the `baseValue` is returned. The timestamp is passed through |
| 57 | + /// unmodified. |
| 58 | + /// @return value Value of the underlying proxy, potentially bounded |
| 59 | + /// @return timestamp Timestamp of the underlying proxy |
| 60 | + function read() |
| 61 | + public |
| 62 | + view |
| 63 | + override |
| 64 | + returns (int224 value, uint32 timestamp) |
| 65 | + { |
| 66 | + (int224 baseValue, uint32 baseTimestamp) = IApi3ReaderProxy(proxy) |
| 67 | + .read(); |
| 68 | + |
| 69 | + timestamp = baseTimestamp; |
| 70 | + |
| 71 | + if (baseValue < lowerBound) { |
| 72 | + value = lowerBound; |
| 73 | + } else if (baseValue > upperBound) { |
| 74 | + value = upperBound; |
| 75 | + } else { |
| 76 | + value = baseValue; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// @notice Checks if the current price from the underlying proxy would be |
| 81 | + /// capped or floored by the bounds. |
| 82 | + /// @return True if the base value is less than `lowerBound` or greater |
| 83 | + /// than `upperBound`, false otherwise. |
| 84 | + function isCapped() external view returns (bool) { |
| 85 | + (int224 baseValue, ) = IApi3ReaderProxy(proxy).read(); |
| 86 | + return baseValue < lowerBound || baseValue > upperBound; |
| 87 | + } |
| 88 | + |
| 89 | + /// @dev AggregatorV2V3Interface users are already responsible with |
| 90 | + /// validating the values that they receive (e.g., revert if the spot price |
| 91 | + /// of an asset is negative). Therefore, this contract omits validation. |
| 92 | + function latestAnswer() external view override returns (int256 value) { |
| 93 | + (value, ) = read(); |
| 94 | + } |
| 95 | + |
| 96 | + /// @dev A Chainlink feed contract returns the block timestamp at which the |
| 97 | + /// feed was last updated. On the other hand, an Api3 feed timestamp |
| 98 | + /// denotes the point in time at which the first-party oracles signed the |
| 99 | + /// data used to do the last update. We find this to be a reasonable |
| 100 | + /// approximation, considering that usually the timestamp is only used to |
| 101 | + /// check if the last update is stale. |
| 102 | + function latestTimestamp() |
| 103 | + external |
| 104 | + view |
| 105 | + override |
| 106 | + returns (uint256 timestamp) |
| 107 | + { |
| 108 | + (, timestamp) = read(); |
| 109 | + } |
| 110 | + |
| 111 | + /// @dev Api3 feeds are updated asynchronously and not in rounds |
| 112 | + function latestRound() external pure override returns (uint256) { |
| 113 | + revert FunctionIsNotSupported(); |
| 114 | + } |
| 115 | + |
| 116 | + /// @dev Functions that use the round ID as an argument are not supported |
| 117 | + function getAnswer(uint256) external pure override returns (int256) { |
| 118 | + revert FunctionIsNotSupported(); |
| 119 | + } |
| 120 | + |
| 121 | + /// @dev Functions that use the round ID as an argument are not supported |
| 122 | + function getTimestamp(uint256) external pure override returns (uint256) { |
| 123 | + revert FunctionIsNotSupported(); |
| 124 | + } |
| 125 | + |
| 126 | + /// @dev Api3 feeds always use 18 decimals |
| 127 | + function decimals() external pure override returns (uint8) { |
| 128 | + return 18; |
| 129 | + } |
| 130 | + |
| 131 | + /// @dev Underlying proxy dApp ID and dAPI name act as the description, and |
| 132 | + /// this is left empty to save gas on contract deployment |
| 133 | + function description() external pure override returns (string memory) { |
| 134 | + return ""; |
| 135 | + } |
| 136 | + |
| 137 | + /// @dev A unique version is chosen to easily check if an unverified |
| 138 | + /// contract that acts as a Chainlink feed is a PriceCappedApi3ReaderProxyV1 |
| 139 | + function version() external pure override returns (uint256) { |
| 140 | + return 4918; |
| 141 | + } |
| 142 | + |
| 143 | + /// @dev Functions that use the round ID as an argument are not supported |
| 144 | + function getRoundData( |
| 145 | + uint80 |
| 146 | + ) |
| 147 | + external |
| 148 | + pure |
| 149 | + override |
| 150 | + returns (uint80, int256, uint256, uint256, uint80) |
| 151 | + { |
| 152 | + revert FunctionIsNotSupported(); |
| 153 | + } |
| 154 | + |
| 155 | + /// @dev Rounds IDs are returned as `0` as invalid values. |
| 156 | + /// Similar to `latestAnswer()`, we leave the validation of the returned |
| 157 | + /// value to the caller. |
| 158 | + function latestRoundData() |
| 159 | + external |
| 160 | + view |
| 161 | + override |
| 162 | + returns ( |
| 163 | + uint80 roundId, |
| 164 | + int256 answer, |
| 165 | + uint256 startedAt, |
| 166 | + uint256 updatedAt, |
| 167 | + uint80 answeredInRound |
| 168 | + ) |
| 169 | + { |
| 170 | + roundId = answeredInRound = 0; |
| 171 | + (answer, startedAt) = read(); |
| 172 | + updatedAt = startedAt; |
| 173 | + } |
| 174 | +} |
0 commit comments