|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +pragma solidity 0.8.23; |
| 3 | + |
| 4 | +import {Errors} from "contracts/common/Errors.sol"; |
| 5 | +import {LPConfig, PoolConfig} from "contracts/common/PoolConfig.sol"; |
| 6 | +import {BaseTranchesPolicy} from "contracts/liquidity/BaseTranchesPolicy.sol"; |
| 7 | +import {SENIOR_TRANCHE, DAYS_IN_A_YEAR, HUNDRED_PERCENT_IN_BPS} from "contracts/common/SharedDefs.sol"; |
| 8 | +import {ICalendar} from "contracts/common/interfaces/ICalendar.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @notice Tranche policy where the yield for the senior tranche is fixed as long as |
| 12 | + * the risk loss does not make it impossible. |
| 13 | + */ |
| 14 | +contract FixedSeniorYieldTranchePolicyForTest is BaseTranchesPolicy { |
| 15 | + /** |
| 16 | + * @notice Tracks the amount of assets and unpaid yield for the senior tranche. |
| 17 | + * @param totalAssets The total assets in the senior tranche. |
| 18 | + * @param unpaidYield The amount of unpaid yield to the senior tranche. |
| 19 | + * @param lastUpdatedDate The last time the tracker was updated. |
| 20 | + */ |
| 21 | + struct SeniorYieldTracker { |
| 22 | + uint96 totalAssets; |
| 23 | + uint96 unpaidYield; |
| 24 | + uint64 lastUpdatedDate; |
| 25 | + } |
| 26 | + |
| 27 | + SeniorYieldTracker public seniorYieldTracker; |
| 28 | + ICalendar public calender; |
| 29 | + |
| 30 | + /** |
| 31 | + * @notice The senior yield tracker has been refreshed. |
| 32 | + * @param totalAssets The total assets in the senior tranche after the refresh. |
| 33 | + * @param unpaidYield The amount of unpaid yield to the senior tranche after the refresh. |
| 34 | + * @param lastUpdatedDate The last time the tracker was updated after the refresh. |
| 35 | + */ |
| 36 | + event YieldTrackerRefreshed(uint256 totalAssets, uint256 unpaidYield, uint256 lastUpdatedDate); |
| 37 | + |
| 38 | + /// @inheritdoc BaseTranchesPolicy |
| 39 | + function refreshYieldTracker(uint96[2] memory assets) external override { |
| 40 | + if (msg.sender != address(poolConfig) && msg.sender != pool) { |
| 41 | + revert Errors.AuthorizedContractCallerRequired(); |
| 42 | + } |
| 43 | + |
| 44 | + (SeniorYieldTracker memory tracker, bool updated) = _getLatestYieldTracker(); |
| 45 | + if (tracker.totalAssets != assets[SENIOR_TRANCHE]) { |
| 46 | + tracker.totalAssets = assets[SENIOR_TRANCHE]; |
| 47 | + updated = true; |
| 48 | + } |
| 49 | + if (updated) { |
| 50 | + seniorYieldTracker = tracker; |
| 51 | + emit YieldTrackerRefreshed( |
| 52 | + tracker.totalAssets, |
| 53 | + tracker.unpaidYield, |
| 54 | + tracker.lastUpdatedDate |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + function _calcProfitForSeniorTranche( |
| 60 | + uint256 profit, |
| 61 | + uint96[2] memory assets |
| 62 | + ) internal virtual override returns (uint256 seniorProfit, uint256 remainingProfit) { |
| 63 | + (SeniorYieldTracker memory tracker, ) = _getLatestYieldTracker(); |
| 64 | + |
| 65 | + seniorProfit = tracker.unpaidYield > profit ? profit : tracker.unpaidYield; |
| 66 | + remainingProfit = profit - seniorProfit; |
| 67 | + |
| 68 | + tracker.unpaidYield -= uint96(seniorProfit); |
| 69 | + tracker.totalAssets = uint96(assets[SENIOR_TRANCHE] + seniorProfit); |
| 70 | + seniorYieldTracker = tracker; |
| 71 | + |
| 72 | + emit YieldTrackerRefreshed( |
| 73 | + tracker.totalAssets, |
| 74 | + tracker.unpaidYield, |
| 75 | + tracker.lastUpdatedDate |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + function _updatePoolConfigData(PoolConfig poolConfig_) internal virtual override { |
| 80 | + super._updatePoolConfigData(poolConfig_); |
| 81 | + |
| 82 | + address addr = poolConfig_.calendar(); |
| 83 | + assert(addr != address(0)); |
| 84 | + calender = ICalendar(addr); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @notice Calculates the amount of yield that the senior tranche should have earned until the current |
| 89 | + * block timestamp. |
| 90 | + * @return The (potentially) updated SeniorYieldTracker. |
| 91 | + * @return updated Whether the SeniorYieldTracker has been updated. |
| 92 | + */ |
| 93 | + function _getLatestYieldTracker() |
| 94 | + internal |
| 95 | + view |
| 96 | + returns (SeniorYieldTracker memory, bool updated) |
| 97 | + { |
| 98 | + SeniorYieldTracker memory tracker = seniorYieldTracker; |
| 99 | + uint256 startOfNextDay = calender.getStartOfNextDay(block.timestamp); |
| 100 | + if (tracker.lastUpdatedDate < startOfNextDay) { |
| 101 | + uint256 daysDiff = calender.getDaysDiff(tracker.lastUpdatedDate, startOfNextDay); |
| 102 | + LPConfig memory lpConfig = poolConfig.getLPConfig(); |
| 103 | + tracker.unpaidYield += uint96( |
| 104 | + (tracker.totalAssets * lpConfig.fixedSeniorYieldInBps * daysDiff) / |
| 105 | + (DAYS_IN_A_YEAR * HUNDRED_PERCENT_IN_BPS) |
| 106 | + ); |
| 107 | + tracker.lastUpdatedDate = uint64(startOfNextDay); |
| 108 | + updated = true; |
| 109 | + } |
| 110 | + |
| 111 | + return (tracker, updated); |
| 112 | + } |
| 113 | +} |
0 commit comments