From 68ff74a3a62e6cddfc292498141a8712a5f84b03 Mon Sep 17 00:00:00 2001 From: Hadrien Charlanes Date: Thu, 3 Jun 2021 11:53:04 +0200 Subject: [PATCH] feat: added exposure cap logic in calculateUserAccountData --- contracts/protocol/libraries/helpers/Errors.sol | 1 + .../protocol/libraries/logic/GenericLogic.sol | 14 +++++++++++--- .../protocol/libraries/logic/ValidationLogic.sol | 2 +- helpers/types.ts | 2 ++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/contracts/protocol/libraries/helpers/Errors.sol b/contracts/protocol/libraries/helpers/Errors.sol index 7314f43c..ccee2a7b 100644 --- a/contracts/protocol/libraries/helpers/Errors.sol +++ b/contracts/protocol/libraries/helpers/Errors.sol @@ -110,6 +110,7 @@ library Errors { string public constant VL_RESERVE_PAUSED = '86'; string public constant LPC_CALLER_NOT_RISK_OR_POOL_ADMIN = '87'; string public constant RC_INVALID_EXPOSURE_CAP = '88'; + string public constant VL_COLLATERAL_EXPOSURE_CAP_EXCEEDED = '89'; enum CollateralManagerErrors { NO_ERROR, diff --git a/contracts/protocol/libraries/logic/GenericLogic.sol b/contracts/protocol/libraries/logic/GenericLogic.sol index 381fe804..395300ee 100644 --- a/contracts/protocol/libraries/logic/GenericLogic.sol +++ b/contracts/protocol/libraries/logic/GenericLogic.sol @@ -47,10 +47,12 @@ library GenericLogic { uint256 reservesLength; uint256 normalizedIncome; uint256 normalizedDebt; + uint256 exposureCap; bool healthFactorBelowThreshold; address currentReserveAddress; bool usageAsCollateralEnabled; bool userUsesReserveAsCollateral; + bool exposureCapped; } /** @@ -112,8 +114,14 @@ library GenericLogic { vars.userBalanceETH = vars.assetPrice.mul(vars.userBalance).div(vars.assetUnit); vars.totalCollateralInETH = vars.totalCollateralInETH.add(vars.userBalanceETH); - - vars.avgLtv = vars.avgLtv.add(vars.userBalanceETH.mul(vars.ltv)); + vars.exposureCap = currentReserve.configuration.getExposureCap(); + vars.exposureCapped = + IERC20(currentReserve.stableDebtTokenAddress) + .totalSupply() + .add(IERC20(currentReserve.variableDebtTokenAddress).totalSupply()) + .div(10**vars.decimals) > + vars.exposureCap; + vars.avgLtv = vars.avgLtv.add(vars.exposureCapped ? 0 : vars.userBalanceETH.mul(vars.ltv)); vars.avgLiquidationThreshold = vars.avgLiquidationThreshold.add( vars.userBalanceETH.mul(vars.liquidationThreshold) ); @@ -132,7 +140,7 @@ library GenericLogic { IERC20(currentReserve.stableDebtTokenAddress).balanceOf(user) ); vars.userDebtETH = vars.assetPrice.mul(vars.userDebt).div(vars.assetUnit); - vars.totalDebtInETH = vars.totalDebtInETH.add(vars.userDebtETH); + vars.totalDebtInETH = vars.totalDebtInETH.add(vars.userDebtETH); } } diff --git a/contracts/protocol/libraries/logic/ValidationLogic.sol b/contracts/protocol/libraries/logic/ValidationLogic.sol index dab27950..14217e6d 100644 --- a/contracts/protocol/libraries/logic/ValidationLogic.sol +++ b/contracts/protocol/libraries/logic/ValidationLogic.sol @@ -480,7 +480,7 @@ library ValidationLogic { exposureCap == 0 || ltv == 0 || totalSupplyStableDebt.add(totalSupplyVariableDebt).div(10**reserveDecimals) < exposureCap, - Errors.VL_SUPPLY_CAP_EXCEEDED + Errors.VL_COLLATERAL_EXPOSURE_CAP_EXCEEDED ); } diff --git a/helpers/types.ts b/helpers/types.ts index adcb8977..8cca7b81 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -184,6 +184,8 @@ export enum ProtocolErrors { LPC_CALLER_NOT_EMERGENCY_OR_POOL_ADMIN = '85', VL_RESERVE_PAUSED = '86', LPC_CALLER_NOT_RISK_OR_POOL_ADMIN = '87', + RC_INVALID_EXPOSURE_CAP = '88', + VL_COLLATERAL_EXPOSURE_CAP_EXCEEDED = '89', // old