feat: added collateral withdrawal check regarding exposure cap

This commit is contained in:
Hadrien Charlanes 2021-06-03 11:24:01 +02:00
parent 3b0f7b18c0
commit 5a8572d568
2 changed files with 22 additions and 5 deletions

View File

@ -372,7 +372,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
if (useAsCollateral) {
emit ReserveUsedAsCollateralEnabled(asset, msg.sender);
} else {
ValidationLogic.validateHealthFactor(
ValidationLogic.validateWithdrawCollateral(
asset,
msg.sender,
_reserves,
_usersConfig[msg.sender],
@ -729,7 +730,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
if (fromConfig.isUsingAsCollateral(reserveId)) {
if (fromConfig.isBorrowingAny()) {
ValidationLogic.validateHealthFactor(
ValidationLogic.validateWithdrawCollateral(
asset,
from,
_reserves,
_usersConfig[from],
@ -966,7 +968,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
if (userConfig.isUsingAsCollateral(reserve.id)) {
if (userConfig.isBorrowingAny()) {
ValidationLogic.validateHealthFactor(
ValidationLogic.validateWithdrawCollateral(
asset,
msg.sender,
_reserves,
userConfig,

View File

@ -446,7 +446,8 @@ library ValidationLogic {
* @param reservesCount The number of available reserves
* @param oracle The price oracle
*/
function validateHealthFactor(
function validateWithdrawCollateral(
address collateral,
address from,
mapping(address => DataTypes.ReserveData) storage reservesData,
DataTypes.UserConfigurationMap storage userConfig,
@ -454,7 +455,8 @@ library ValidationLogic {
uint256 reservesCount,
address oracle
) internal view {
(, , , , uint256 healthFactor) =
DataTypes.ReserveData memory reserve = reservesData[collateral];
(, , uint256 ltv, , uint256 healthFactor) =
GenericLogic.calculateUserAccountData(
from,
reservesData,
@ -464,10 +466,22 @@ library ValidationLogic {
oracle
);
uint256 exposureCap = reserve.configuration.getExposureCapMemory();
uint256 totalSupplyStableDebt = IERC20(reserve.stableDebtTokenAddress).totalSupply();
uint256 totalSupplyVariableDebt = IERC20(reserve.variableDebtTokenAddress).totalSupply();
(, , , uint256 reserveDecimals, ) = reserve.configuration.getParamsMemory();
require(
healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD,
Errors.VL_HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD
);
require(
exposureCap == 0 ||
ltv == 0 ||
totalSupplyStableDebt.add(totalSupplyVariableDebt).div(10**reserveDecimals) < exposureCap,
Errors.VL_SUPPLY_CAP_EXCEEDED
);
}
/**