|
| 1 | +// SPDX-License-Identifier: GPL-3.0 |
| 2 | + |
| 3 | +/* |
| 4 | + This file is part of the Enzyme Protocol. |
| 5 | +
|
| 6 | + (c) Enzyme Council <[email protected]> |
| 7 | +
|
| 8 | + For the full license information, please view the LICENSE |
| 9 | + file that was distributed with this source code. |
| 10 | +*/ |
| 11 | + |
| 12 | +pragma solidity 0.8.19; |
| 13 | + |
| 14 | +import {IDispatcher} from "../../../../../../persistent/dispatcher/IDispatcher.sol"; |
| 15 | +import {IPendleV2Market} from "../../../../../../external-interfaces/IPendleV2Market.sol"; |
| 16 | +import {IPendleV2PrincipalToken} from "../../../../../../external-interfaces/IPendleV2PrincipalToken.sol"; |
| 17 | +import {IPendleV2PtAndLpOracle} from "../../../../../../external-interfaces/IPendleV2PtAndLpOracle.sol"; |
| 18 | +import {IPendleV2MarketRegistry} from "./IPendleV2MarketRegistry.sol"; |
| 19 | + |
| 20 | +/// @title PendleV2MarketRegistry Contract |
| 21 | +/// @author Enzyme Council <[email protected]> |
| 22 | +/// @notice A contract for the per-user registration of Pendle v2 markets |
| 23 | +contract PendleV2MarketRegistry is IPendleV2MarketRegistry { |
| 24 | + event MarketForUserUpdated(address indexed user, address indexed marketAddress, uint32 duration); |
| 25 | + |
| 26 | + event PtForUserUpdated(address indexed user, address indexed ptAddress, address indexed marketAddress); |
| 27 | + |
| 28 | + error InsufficientOracleState(bool increaseCardinalityRequired, bool oldestObservationSatisfied); |
| 29 | + |
| 30 | + IPendleV2PtAndLpOracle private immutable PENDLE_PT_AND_LP_ORACLE; |
| 31 | + |
| 32 | + mapping(address => mapping(address => uint32)) private userToMarketToOracleDuration; |
| 33 | + mapping(address => mapping(address => address)) private userToPtToLinkedMarket; |
| 34 | + |
| 35 | + constructor(IPendleV2PtAndLpOracle _pendlePtAndLpOracle) { |
| 36 | + PENDLE_PT_AND_LP_ORACLE = _pendlePtAndLpOracle; |
| 37 | + } |
| 38 | + |
| 39 | + /// @notice Updates the market registry specific to the caller |
| 40 | + /// @param _updateMarketInputs An array of market config inputs to set |
| 41 | + /// @param _skipValidation True to skip optional validation of _updateMarketInputs |
| 42 | + /// @dev See UpdateMarketInput definition for struct param details |
| 43 | + function updateMarketsForCaller(UpdateMarketInput[] calldata _updateMarketInputs, bool _skipValidation) |
| 44 | + external |
| 45 | + override |
| 46 | + { |
| 47 | + address user = msg.sender; |
| 48 | + |
| 49 | + for (uint256 i; i < _updateMarketInputs.length; i++) { |
| 50 | + UpdateMarketInput memory marketInput = _updateMarketInputs[i]; |
| 51 | + |
| 52 | + // Does not validate zero-duration, which is a valid oracle deactivation |
| 53 | + if (marketInput.duration > 0 && !_skipValidation) { |
| 54 | + __validateMarketConfig({_marketAddress: marketInput.marketAddress, _duration: marketInput.duration}); |
| 55 | + } |
| 56 | + |
| 57 | + // Store the market duration |
| 58 | + userToMarketToOracleDuration[user][marketInput.marketAddress] = marketInput.duration; |
| 59 | + emit MarketForUserUpdated(user, marketInput.marketAddress, marketInput.duration); |
| 60 | + |
| 61 | + // Handle PT-market link |
| 62 | + (, IPendleV2PrincipalToken pt,) = IPendleV2Market(marketInput.marketAddress).readTokens(); |
| 63 | + bool ptIsLinkedToMarket = |
| 64 | + getPtOracleMarketForUser({_user: user, _ptAddress: address(pt)}) == marketInput.marketAddress; |
| 65 | + |
| 66 | + if (marketInput.duration > 0) { |
| 67 | + // If new duration is non-zero, cache PT-market link (i.e., always follow the last active market) |
| 68 | + |
| 69 | + if (!ptIsLinkedToMarket) { |
| 70 | + userToPtToLinkedMarket[user][address(pt)] = marketInput.marketAddress; |
| 71 | + emit PtForUserUpdated(user, address(pt), marketInput.marketAddress); |
| 72 | + } |
| 73 | + } else if (ptIsLinkedToMarket) { |
| 74 | + // If the PT's linked market duration is being set to 0, remove link to the market |
| 75 | + |
| 76 | + // Unlink the PT from the market |
| 77 | + userToPtToLinkedMarket[user][address(pt)] = address(0); |
| 78 | + emit PtForUserUpdated(user, address(pt), address(0)); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// @dev Helper to validate user-input market config. |
| 84 | + /// Only validates the recommended oracle state, |
| 85 | + /// not whether duration provides a sufficiently secure TWAP price. |
| 86 | + /// src: https://docs.pendle.finance/Developers/Integration/HowToIntegratePtAndLpOracle. |
| 87 | + function __validateMarketConfig(address _marketAddress, uint32 _duration) private view { |
| 88 | + (bool increaseCardinalityRequired,, bool oldestObservationSatisfied) = |
| 89 | + PENDLE_PT_AND_LP_ORACLE.getOracleState({_market: _marketAddress, _duration: _duration}); |
| 90 | + |
| 91 | + if (increaseCardinalityRequired || !oldestObservationSatisfied) { |
| 92 | + revert InsufficientOracleState(increaseCardinalityRequired, oldestObservationSatisfied); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /////////////////// |
| 97 | + // STATE GETTERS // |
| 98 | + /////////////////// |
| 99 | + |
| 100 | + // EXTERNAL |
| 101 | + |
| 102 | + /// @notice Gets the oracle market and its duration for a principal token, as-registered by the given user |
| 103 | + /// @param _user The user |
| 104 | + /// @param _ptAddress The principal token |
| 105 | + /// @return marketAddress_ The market |
| 106 | + /// @return duration_ The duration |
| 107 | + function getPtOracleMarketAndDurationForUser(address _user, address _ptAddress) |
| 108 | + external |
| 109 | + view |
| 110 | + returns (address marketAddress_, uint32 duration_) |
| 111 | + { |
| 112 | + marketAddress_ = getPtOracleMarketForUser({_user: _user, _ptAddress: _ptAddress}); |
| 113 | + duration_ = getMarketOracleDurationForUser({_user: _user, _marketAddress: marketAddress_}); |
| 114 | + |
| 115 | + return (marketAddress_, duration_); |
| 116 | + } |
| 117 | + |
| 118 | + // PUBLIC |
| 119 | + |
| 120 | + /// @notice Gets the oracle duration for a market, as-registered by the given user |
| 121 | + /// @param _user The user |
| 122 | + /// @param _marketAddress The market |
| 123 | + /// @return duration_ The duration |
| 124 | + function getMarketOracleDurationForUser(address _user, address _marketAddress) |
| 125 | + public |
| 126 | + view |
| 127 | + returns (uint32 duration_) |
| 128 | + { |
| 129 | + return userToMarketToOracleDuration[_user][_marketAddress]; |
| 130 | + } |
| 131 | + |
| 132 | + /// @notice Gets the linked market for a principal token, as-registered by the given user |
| 133 | + /// @param _user The user |
| 134 | + /// @param _ptAddress The principal token |
| 135 | + /// @return marketAddress_ The market |
| 136 | + function getPtOracleMarketForUser(address _user, address _ptAddress) public view returns (address marketAddress_) { |
| 137 | + return userToPtToLinkedMarket[_user][_ptAddress]; |
| 138 | + } |
| 139 | +} |
0 commit comments