|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +// Gearbox Protocol. Generalized leverage for DeFi protocols |
| 3 | +// (c) Gearbox Foundation, 2024. |
| 4 | +pragma solidity ^0.8.17; |
| 5 | + |
| 6 | +import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; |
| 7 | + |
| 8 | +import {WAD, SECONDS_PER_YEAR} from "@gearbox-protocol/core-v2/contracts/libraries/Constants.sol"; |
| 9 | +import {PriceFeedType} from "@gearbox-protocol/sdk-gov/contracts/PriceFeedType.sol"; |
| 10 | + |
| 11 | +import {IPriceFeed} from "@gearbox-protocol/core-v2/contracts/interfaces/IPriceFeed.sol"; |
| 12 | +import {IPendleMarket} from "../../interfaces/pendle/IPendleMarket.sol"; |
| 13 | +import {IPendleYT, IPendleSY} from "../../interfaces/pendle/IPendleTokens.sol"; |
| 14 | +import {PriceFeedValidationTrait} from "@gearbox-protocol/core-v3/contracts/traits/PriceFeedValidationTrait.sol"; |
| 15 | +import {SanityCheckTrait} from "@gearbox-protocol/core-v3/contracts/traits/SanityCheckTrait.sol"; |
| 16 | +import {PriceFeedParams} from "../PriceFeedParams.sol"; |
| 17 | +import {FixedPoint} from "../../libraries/FixedPoint.sol"; |
| 18 | +import {LogExpMath} from "../../libraries/LogExpMath.sol"; |
| 19 | + |
| 20 | +/// @title Pendle PT price feed based on Pendle market TWAPs |
| 21 | +/// @notice The PT price is derived from the Pendle market's ln(impliedRate) TWAP: |
| 22 | +/// 1) The average implied rate is computed as (ln(IR)_(now) - ln(IR)_(now - timeWindow)) / timeWindow; |
| 23 | +/// 2) The PT to asset rate is computed as 1 / (e ^ (ln(IR)_avg * timeToExpiry / secondsPerYear)); |
| 24 | +/// 3) The PT price is ptToAssetRate * assetPrice; |
| 25 | +contract PendleTWAPPTPriceFeed is IPriceFeed, PriceFeedValidationTrait, SanityCheckTrait { |
| 26 | + uint256 public constant override version = 3_00; |
| 27 | + PriceFeedType public constant override priceFeedType = PriceFeedType.PENDLE_PT_TWAP_ORACLE; |
| 28 | + uint8 public constant override decimals = 8; |
| 29 | + string public description; |
| 30 | + |
| 31 | + /// @notice Indicates whether the consuming PriceOracle can skip the sanity checks. Set to `true` |
| 32 | + /// since this price feed performs sanity checks locally |
| 33 | + bool public constant override skipPriceCheck = true; |
| 34 | + |
| 35 | + /// @notice Address of the pendle market where the PT is traded |
| 36 | + address public immutable market; |
| 37 | + |
| 38 | + /// @notice Address of the Pendle SY connected to the PT |
| 39 | + address public immutable sy; |
| 40 | + |
| 41 | + /// @notice Address of the Pendle YT connected to the YT |
| 42 | + address public immutable yt; |
| 43 | + |
| 44 | + /// @notice Timestamp of the market (and PT) expiry |
| 45 | + uint256 public immutable expiry; |
| 46 | + |
| 47 | + /// @notice Underlying price feed |
| 48 | + address public immutable priceFeed; |
| 49 | + uint32 public immutable stalenessPeriod; |
| 50 | + bool public immutable skipCheck; |
| 51 | + |
| 52 | + /// @notice The size of the TWAP observation window |
| 53 | + uint32 public immutable twapWindow; |
| 54 | + |
| 55 | + constructor(address _market, address _priceFeed, uint32 _stalenessPeriod, uint32 _twapWindow) { |
| 56 | + market = _market; |
| 57 | + expiry = IPendleMarket(_market).expiry(); |
| 58 | + priceFeed = _priceFeed; |
| 59 | + stalenessPeriod = _stalenessPeriod; |
| 60 | + skipCheck = _validatePriceFeed(priceFeed, stalenessPeriod); |
| 61 | + twapWindow = _twapWindow; |
| 62 | + |
| 63 | + address pt; |
| 64 | + |
| 65 | + (sy, pt, yt) = IPendleMarket(_market).readTokens(); |
| 66 | + |
| 67 | + string memory ptName = IERC20Metadata(pt).name(); |
| 68 | + |
| 69 | + description = string(abi.encodePacked(ptName, " Pendle Market TWAP * ", IPriceFeed(priceFeed).description())); |
| 70 | + } |
| 71 | + |
| 72 | + /// @dev Gets the ln(impliedRate) from the market TWAP |
| 73 | + function _getMarketLnImpliedRate() internal view returns (uint256) { |
| 74 | + uint32[] memory secondAgos = new uint32[](2); |
| 75 | + secondAgos[1] = twapWindow; |
| 76 | + |
| 77 | + uint216[] memory cumulativeLIR = IPendleMarket(market).observe(secondAgos); |
| 78 | + |
| 79 | + return (cumulativeLIR[0] - cumulativeLIR[1]) / twapWindow; |
| 80 | + } |
| 81 | + |
| 82 | + /// @dev Computes the PT to asset rate from the market implied rate TWAP |
| 83 | + function _getPTToAssetRate() internal view returns (uint256) { |
| 84 | + uint256 assetToPTRate = |
| 85 | + uint256(LogExpMath.exp(int256(_getMarketLnImpliedRate() * (expiry - block.timestamp) / SECONDS_PER_YEAR))); |
| 86 | + |
| 87 | + return FixedPoint.divDown(WAD, assetToPTRate); |
| 88 | + } |
| 89 | + |
| 90 | + /// @dev Retrieves the current SY and YT indices |
| 91 | + function _getSYandPYIndex() internal view returns (uint256 syIndex, uint256 pyIndex) { |
| 92 | + syIndex = IPendleSY(sy).exchangeRate(); |
| 93 | + uint256 pyIndexStored = IPendleYT(yt).pyIndexStored(); |
| 94 | + |
| 95 | + if (IPendleYT(yt).doCacheIndexSameBlock() && IPendleYT(yt).pyIndexLastUpdatedBlock() == block.number) { |
| 96 | + pyIndex = pyIndexStored; |
| 97 | + } else { |
| 98 | + pyIndex = syIndex >= pyIndexStored ? syIndex : pyIndexStored; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// @notice Returns the USD price of the PT token with 8 decimals |
| 103 | + function latestRoundData() external view override returns (uint80, int256, uint256, uint256, uint80) { |
| 104 | + int256 answer = _getValidatedPrice(priceFeed, stalenessPeriod, skipCheck); |
| 105 | + |
| 106 | + if (expiry > block.timestamp) { |
| 107 | + answer = int256(FixedPoint.mulDown(uint256(answer), _getPTToAssetRate())); |
| 108 | + } |
| 109 | + |
| 110 | + (uint256 syIndex, uint256 pyIndex) = _getSYandPYIndex(); |
| 111 | + |
| 112 | + if (syIndex < pyIndex) { |
| 113 | + answer = int256(uint256(answer) * syIndex / pyIndex); |
| 114 | + } |
| 115 | + |
| 116 | + return (0, answer, 0, 0, 0); |
| 117 | + } |
| 118 | +} |
0 commit comments