|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import { Math } from "@openzeppelin/contracts/utils/math/Math.sol"; |
| 5 | + |
| 6 | +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | +import { IPerpetualTranche } from "@ampleforthorg/spot-contracts/contracts/_interfaces/IPerpetualTranche.sol"; |
| 8 | +import { IChainlinkOracle } from "../_interfaces/external/IChainlinkOracle.sol"; |
| 9 | +import { IAmpleforthOracle } from "../_interfaces/external/IAmpleforthOracle.sol"; |
| 10 | +import { ISpotPricingStrategy } from "../_interfaces/ISpotPricingStrategy.sol"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @title SpotCDRPricer |
| 14 | + * |
| 15 | + * @notice Pricing strategy adapter for SPOT. |
| 16 | + * |
| 17 | + * SPOT is a perpetual claim on AMPL senior tranches. |
| 18 | + * We price spot based on the redeemable value of it's collateral at maturity. |
| 19 | + * NOTE: SPOT's internal `getTVL` prices the collateral this way. |
| 20 | + * |
| 21 | + * SPOT_PRICE = (spot.getTVL() / spot.totalSupply()) * AMPL_TARGET |
| 22 | + * |
| 23 | + * We get the AMPL target price from Ampleforth's CPI oracle, |
| 24 | + * which is also used by the protocol to adjust AMPL supply through rebasing. |
| 25 | + * |
| 26 | + */ |
| 27 | +contract SpotCDRPricer is ISpotPricingStrategy { |
| 28 | + //------------------------------------------------------------------------- |
| 29 | + // Libraries |
| 30 | + using Math for uint256; |
| 31 | + |
| 32 | + //------------------------------------------------------------------------- |
| 33 | + // Constants & Immutables |
| 34 | + |
| 35 | + uint256 private constant DECIMALS = 18; |
| 36 | + uint256 private constant ONE = (10 ** DECIMALS); |
| 37 | + uint256 public constant CL_ORACLE_DECIMALS = 8; |
| 38 | + uint256 public constant CL_ORACLE_STALENESS_THRESHOLD_SEC = 3600 * 48; // 2 days |
| 39 | + uint256 public constant USD_UPPER_BOUND = (101 * ONE) / 100; // 1.01$ |
| 40 | + uint256 public constant USD_LOWER_BOUND = (99 * ONE) / 100; // 0.99$ |
| 41 | + |
| 42 | + /// @notice Address of the SPOT (perpetual tranche) ERC-20 token contract. |
| 43 | + IPerpetualTranche public immutable SPOT; |
| 44 | + |
| 45 | + /// @notice Address of the AMPL ERC-20 token contract. |
| 46 | + IERC20 public immutable AMPL; |
| 47 | + |
| 48 | + /// @notice Address of the USD token market price oracle. |
| 49 | + IChainlinkOracle public immutable USD_ORACLE; |
| 50 | + |
| 51 | + /// @notice Number of decimals representing the prices returned by the chainlink oracle. |
| 52 | + uint256 public immutable USD_ORACLE_DECIMALS; |
| 53 | + |
| 54 | + /// @notice Address of the Ampleforth CPI oracle. (provides the inflation-adjusted target price for AMPL). |
| 55 | + IAmpleforthOracle public immutable AMPL_CPI_ORACLE; |
| 56 | + |
| 57 | + /// @notice Number of decimals representing the prices returned by the ampleforth oracle. |
| 58 | + uint256 public immutable AMPL_CPI_ORACLE_DECIMALS; |
| 59 | + |
| 60 | + //----------------------------------------------------------------------------- |
| 61 | + // Constructor |
| 62 | + |
| 63 | + /// @notice Contract constructor. |
| 64 | + /// @param spot Address of the SPOT token. |
| 65 | + /// @param usdOracle Address of the USD token market price oracle token. |
| 66 | + /// @param cpiOracle Address of the Ampleforth CPI oracle. |
| 67 | + constructor( |
| 68 | + IPerpetualTranche spot, |
| 69 | + IChainlinkOracle usdOracle, |
| 70 | + IAmpleforthOracle cpiOracle |
| 71 | + ) { |
| 72 | + SPOT = spot; |
| 73 | + AMPL = IERC20(address(spot.underlying())); |
| 74 | + |
| 75 | + USD_ORACLE = usdOracle; |
| 76 | + USD_ORACLE_DECIMALS = usdOracle.decimals(); |
| 77 | + |
| 78 | + AMPL_CPI_ORACLE = cpiOracle; |
| 79 | + AMPL_CPI_ORACLE_DECIMALS = cpiOracle.DECIMALS(); |
| 80 | + } |
| 81 | + |
| 82 | + //-------------------------------------------------------------------------- |
| 83 | + // External methods |
| 84 | + |
| 85 | + /// @return p The price of the usd token in dollars. |
| 86 | + /// @return v True if the price is valid and can be used by downstream consumers. |
| 87 | + function usdPrice() external view override returns (uint256, bool) { |
| 88 | + (uint256 p, bool v) = _getCLOracleData(USD_ORACLE, USD_ORACLE_DECIMALS); |
| 89 | + // If the market price of the USD coin deviated too much from 1$, |
| 90 | + // it's an indication of some systemic issue with the USD token |
| 91 | + // and thus its price should be considered unreliable. |
| 92 | + return (ONE, (v && p < USD_UPPER_BOUND && p > USD_LOWER_BOUND)); |
| 93 | + } |
| 94 | + |
| 95 | + /// @return p The price of the spot token in dollar coins. |
| 96 | + /// @return v True if the price is valid and can be used by downstream consumers. |
| 97 | + function perpPrice() external override returns (uint256, bool) { |
| 98 | + // NOTE: Since {DECIMALS} == {AMPL_CPI_ORACLE_DECIMALS} == 18 |
| 99 | + // we don't adjust the returned values. |
| 100 | + (uint256 targetPrice, bool targetPriceValid) = AMPL_CPI_ORACLE.getData(); |
| 101 | + uint256 p = targetPrice.mulDiv(SPOT.getTVL(), SPOT.totalSupply()); |
| 102 | + return (p, targetPriceValid); |
| 103 | + } |
| 104 | + |
| 105 | + /// @return Number of decimals representing a price of 1.0 USD. |
| 106 | + function decimals() external pure override returns (uint8) { |
| 107 | + return uint8(DECIMALS); |
| 108 | + } |
| 109 | + |
| 110 | + //----------------------------------------------------------------------------- |
| 111 | + // Private methods |
| 112 | + |
| 113 | + /// @dev Fetches most recent report from the given chain link oracle contract. |
| 114 | + /// The data is considered invalid if the latest report is stale. |
| 115 | + function _getCLOracleData( |
| 116 | + IChainlinkOracle oracle, |
| 117 | + uint256 oracleDecimals |
| 118 | + ) private view returns (uint256, bool) { |
| 119 | + (, int256 p, , uint256 updatedAt, ) = oracle.latestRoundData(); |
| 120 | + uint256 price = uint256(p).mulDiv(ONE, 10 ** oracleDecimals); |
| 121 | + return ( |
| 122 | + price, |
| 123 | + (block.timestamp - updatedAt) <= CL_ORACLE_STALENESS_THRESHOLD_SEC |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
0 commit comments