|
| 1 | +// SPDX-License-Identifier: agpl-3.0 |
| 2 | +pragma solidity 0.8.10; |
| 3 | + |
| 4 | +import {IERC20} from '../../contracts/dependencies/openzeppelin/contracts/IERC20.sol'; |
| 5 | +import {IScaledBalanceToken} from '../../contracts/interfaces/IScaledBalanceToken.sol'; |
| 6 | +import {IPriceOracleGetter} from '../../contracts/interfaces/IPriceOracleGetter.sol'; |
| 7 | +import {ReserveConfiguration} from '../../contracts/protocol/libraries/configuration/ReserveConfiguration.sol'; |
| 8 | +import {UserConfiguration} from '../../contracts/protocol/libraries/configuration/UserConfiguration.sol'; |
| 9 | +import {PercentageMath} from '../../contracts/protocol/libraries/math/PercentageMath.sol'; |
| 10 | +import {WadRayMath} from '../../contracts/protocol/libraries/math/WadRayMath.sol'; |
| 11 | +import {DataTypes} from '../../contracts/protocol/libraries/types/DataTypes.sol'; |
| 12 | +import {ReserveLogic} from '../../contracts/protocol/libraries/logic/ReserveLogic.sol'; |
| 13 | + |
| 14 | +/** |
| 15 | + * @title GenericLogic library |
| 16 | + * @author Aave |
| 17 | + * @notice Implements protocol-level logic to calculate and validate the state of a user |
| 18 | + */ |
| 19 | +contract GenericLogic { |
| 20 | + using ReserveLogic for DataTypes.ReserveData; |
| 21 | + using WadRayMath for uint256; |
| 22 | + using PercentageMath for uint256; |
| 23 | + using ReserveConfiguration for DataTypes.ReserveConfigurationMap; |
| 24 | + using UserConfiguration for DataTypes.UserConfigurationMap; |
| 25 | + |
| 26 | + mapping(address => DataTypes.ReserveData) public reservesData; |
| 27 | + mapping(uint256 => address) public reserves; |
| 28 | + mapping(uint8 => DataTypes.EModeCategory) public eModeCategories; |
| 29 | + //DataTypes.CalculateUserAccountDataParams public params; |
| 30 | + DataTypes.UserConfigurationMap public userConfig; |
| 31 | + uint256 public reservesCount; |
| 32 | + address public user; |
| 33 | + address public oracle; |
| 34 | + uint8 public userEModeCategory; |
| 35 | + |
| 36 | + struct CalculateUserAccountDataVars { |
| 37 | + uint256 assetPrice; |
| 38 | + uint256 assetUnit; |
| 39 | + uint256 userBalance; |
| 40 | + uint256 userBalanceInBaseCurrency; |
| 41 | + uint256 userDebt; |
| 42 | + uint256 userStableDebt; |
| 43 | + uint256 userDebtInBaseCurrency; |
| 44 | + uint256 decimals; |
| 45 | + uint256 ltv; |
| 46 | + uint256 liquidationThreshold; |
| 47 | + uint256 i; |
| 48 | + uint256 healthFactor; |
| 49 | + uint256 totalCollateralInBaseCurrency; |
| 50 | + uint256 totalDebtInBaseCurrency; |
| 51 | + uint256 avgLtv; |
| 52 | + uint256 avgLiquidationThreshold; |
| 53 | + uint256 normalizedIncome; |
| 54 | + uint256 normalizedDebt; |
| 55 | + uint256 eModeAssetPrice; |
| 56 | + uint256 eModeLtv; |
| 57 | + uint256 eModeLiqThreshold; |
| 58 | + uint256 eModeAssetCategory; |
| 59 | + address eModePriceSource; |
| 60 | + address currentReserveAddress; |
| 61 | + bool hasZeroLtvCollateral; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @notice Calculates the user data across the reserves. |
| 66 | + * @dev It includes the total liquidity/collateral/borrow balances in the base currency used by the price feed, |
| 67 | + * the average Loan To Value, the average Liquidation Ratio, and the Health factor. |
| 68 | + * @return The total collateral of the user in the base currency used by the price feed |
| 69 | + * @return The total debt of the user in the base currency used by the price feed |
| 70 | + * @return The average ltv of the user |
| 71 | + * @return The average liquidation threshold of the user |
| 72 | + * @return The health factor of the user |
| 73 | + * @return True if the ltv is zero, false otherwise |
| 74 | + **/ |
| 75 | + function calculateUserAccountData() |
| 76 | + public |
| 77 | + returns ( |
| 78 | + uint256, |
| 79 | + uint256, |
| 80 | + uint256, |
| 81 | + uint256, |
| 82 | + uint256, |
| 83 | + bool |
| 84 | + ) |
| 85 | + { |
| 86 | + if (userConfig.isEmpty()) { |
| 87 | + return (0, 0, 0, 0, type(uint256).max, false); |
| 88 | + } |
| 89 | + |
| 90 | + CalculateUserAccountDataVars memory vars; |
| 91 | + |
| 92 | + if (userEModeCategory != 0) { |
| 93 | + vars.eModePriceSource = eModeCategories[userEModeCategory].priceSource; |
| 94 | + vars.eModeLtv = eModeCategories[userEModeCategory].ltv; |
| 95 | + vars.eModeLiqThreshold = eModeCategories[userEModeCategory].liquidationThreshold; |
| 96 | + |
| 97 | + if (vars.eModePriceSource != address(0)) { |
| 98 | + vars.eModeAssetPrice = IPriceOracleGetter(oracle).getAssetPrice( |
| 99 | + vars.eModePriceSource |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + while (vars.i < reservesCount) { |
| 105 | + if (!userConfig.isUsingAsCollateralOrBorrowing(vars.i)) { |
| 106 | + unchecked { |
| 107 | + ++vars.i; |
| 108 | + } |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + vars.currentReserveAddress = reserves[vars.i]; |
| 113 | + |
| 114 | + if (vars.currentReserveAddress == address(0)) { |
| 115 | + unchecked { |
| 116 | + ++vars.i; |
| 117 | + } |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + DataTypes.ReserveData storage currentReserve = reservesData[vars.currentReserveAddress]; |
| 122 | + |
| 123 | + ( |
| 124 | + vars.ltv, |
| 125 | + vars.liquidationThreshold, |
| 126 | + , |
| 127 | + vars.decimals, |
| 128 | + , |
| 129 | + vars.eModeAssetCategory |
| 130 | + ) = currentReserve.configuration.getParams(); |
| 131 | + |
| 132 | + unchecked { |
| 133 | + vars.assetUnit = 10**vars.decimals; |
| 134 | + } |
| 135 | + vars.assetPrice = vars.eModeAssetPrice > 0 |
| 136 | + ? vars.eModeAssetPrice |
| 137 | + : IPriceOracleGetter(oracle).getAssetPrice(vars.currentReserveAddress); |
| 138 | + |
| 139 | + if (vars.liquidationThreshold != 0 && userConfig.isUsingAsCollateral(vars.i)) { |
| 140 | + vars.normalizedIncome = currentReserve.getNormalizedIncome(); |
| 141 | + vars.userBalance = IScaledBalanceToken(currentReserve.aTokenAddress).scaledBalanceOf( |
| 142 | + user |
| 143 | + ); |
| 144 | + vars.userBalance = vars.userBalance.rayMul(vars.normalizedIncome); |
| 145 | + |
| 146 | + vars.userBalanceInBaseCurrency = (vars.assetPrice * vars.userBalance); |
| 147 | + unchecked { |
| 148 | + vars.userBalanceInBaseCurrency /= vars.assetUnit; |
| 149 | + } |
| 150 | + vars.totalCollateralInBaseCurrency = |
| 151 | + vars.totalCollateralInBaseCurrency + |
| 152 | + vars.userBalanceInBaseCurrency; |
| 153 | + |
| 154 | + vars.hasZeroLtvCollateral = vars.hasZeroLtvCollateral || vars.ltv == 0; |
| 155 | + |
| 156 | + vars.avgLtv = vars.ltv > 0 |
| 157 | + ? vars.avgLtv + |
| 158 | + vars.userBalanceInBaseCurrency * |
| 159 | + ( |
| 160 | + (userEModeCategory == 0 || vars.eModeAssetCategory != userEModeCategory) |
| 161 | + ? vars.ltv |
| 162 | + : vars.eModeLtv |
| 163 | + ) |
| 164 | + : vars.avgLtv; |
| 165 | + |
| 166 | + vars.avgLiquidationThreshold = |
| 167 | + vars.avgLiquidationThreshold + |
| 168 | + vars.userBalanceInBaseCurrency * |
| 169 | + ( |
| 170 | + (userEModeCategory == 0 || vars.eModeAssetCategory != userEModeCategory) |
| 171 | + ? vars.liquidationThreshold |
| 172 | + : vars.eModeLiqThreshold |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + if (userConfig.isBorrowing(vars.i)) { |
| 177 | + vars.userStableDebt = IERC20(currentReserve.stableDebtTokenAddress).balanceOf(user); |
| 178 | + vars.userDebt = IScaledBalanceToken(currentReserve.variableDebtTokenAddress) |
| 179 | + .scaledBalanceOf(user); |
| 180 | + |
| 181 | + if (vars.userDebt > 0) { |
| 182 | + vars.normalizedDebt = currentReserve.getNormalizedDebt(); |
| 183 | + vars.userDebt = vars.userDebt.rayMul(vars.normalizedDebt); |
| 184 | + } |
| 185 | + vars.userDebt = vars.userDebt + vars.userStableDebt; |
| 186 | + vars.userDebtInBaseCurrency = (vars.assetPrice * vars.userDebt); |
| 187 | + unchecked { |
| 188 | + vars.userDebtInBaseCurrency /= vars.assetUnit; |
| 189 | + } |
| 190 | + vars.totalDebtInBaseCurrency = vars.totalDebtInBaseCurrency + vars.userDebtInBaseCurrency; |
| 191 | + } |
| 192 | + |
| 193 | + unchecked { |
| 194 | + ++vars.i; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + unchecked { |
| 199 | + vars.avgLtv = vars.totalCollateralInBaseCurrency > 0 |
| 200 | + ? vars.avgLtv / vars.totalCollateralInBaseCurrency |
| 201 | + : 0; |
| 202 | + vars.avgLiquidationThreshold = vars.totalCollateralInBaseCurrency > 0 |
| 203 | + ? vars.avgLiquidationThreshold / vars.totalCollateralInBaseCurrency |
| 204 | + : 0; |
| 205 | + } |
| 206 | + |
| 207 | + vars.healthFactor = (vars.totalDebtInBaseCurrency == 0) |
| 208 | + ? type(uint256).max |
| 209 | + : (vars.totalCollateralInBaseCurrency.percentMul(vars.avgLiquidationThreshold)).wadDiv( |
| 210 | + vars.totalDebtInBaseCurrency |
| 211 | + ); |
| 212 | + return ( |
| 213 | + vars.totalCollateralInBaseCurrency, |
| 214 | + vars.totalDebtInBaseCurrency, |
| 215 | + vars.avgLtv, |
| 216 | + vars.avgLiquidationThreshold, |
| 217 | + vars.healthFactor, |
| 218 | + vars.hasZeroLtvCollateral |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * @notice Calculates the maximum amount that can be borrowed depending on the available collateral, the total debt and the |
| 224 | + * average Loan To Value |
| 225 | + * @param totalCollateralInBaseCurrency The total collateral in the base currency used by the price feed |
| 226 | + * @param totalDebtInBaseCurrency The total borrow balance in the base currency used by the price feed |
| 227 | + * @param ltv The average loan to value |
| 228 | + * @return The amount available to borrow in the base currency of the used by the price feed |
| 229 | + **/ |
| 230 | + function calculateAvailableBorrows( |
| 231 | + uint256 totalCollateralInBaseCurrency, |
| 232 | + uint256 totalDebtInBaseCurrency, |
| 233 | + uint256 ltv |
| 234 | + ) public pure returns (uint256) { |
| 235 | + uint256 availableBorrowsInBaseCurrency = totalCollateralInBaseCurrency.percentMul(ltv); |
| 236 | + |
| 237 | + if (availableBorrowsInBaseCurrency < totalDebtInBaseCurrency) { |
| 238 | + return 0; |
| 239 | + } |
| 240 | + |
| 241 | + availableBorrowsInBaseCurrency = availableBorrowsInBaseCurrency - totalDebtInBaseCurrency; |
| 242 | + return availableBorrowsInBaseCurrency; |
| 243 | + } |
| 244 | +} |
0 commit comments