From e18bd375cca6b0e4f1bc8a115022c215cfa5a458 Mon Sep 17 00:00:00 2001 From: Hadrien Charlanes Date: Tue, 18 May 2021 09:51:38 +0200 Subject: [PATCH] refactor: refactor validation logic after merging validateHealthFactor --- .../protocol/lendingpool/LendingPool.sol | 3 +- .../libraries/logic/ValidationLogic.sol | 78 +++++++++---------- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/contracts/protocol/lendingpool/LendingPool.sol b/contracts/protocol/lendingpool/LendingPool.sol index ad7f8948..d3ef2c26 100644 --- a/contracts/protocol/lendingpool/LendingPool.sol +++ b/contracts/protocol/lendingpool/LendingPool.sol @@ -720,6 +720,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage ) external override whenNotPaused { require(msg.sender == _reserves[asset].aTokenAddress, Errors.LP_CALLER_MUST_BE_AN_ATOKEN); + ValidationLogic.validateTransfer(_reserves[asset]); + uint256 reserveId = _reserves[asset].id; if (from != to) { @@ -728,7 +730,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage if (fromConfig.isUsingAsCollateral(reserveId)) { if (fromConfig.isBorrowingAny()) { ValidationLogic.validateHealthFactor( - asset, from, _reserves, _usersConfig[from], diff --git a/contracts/protocol/libraries/logic/ValidationLogic.sol b/contracts/protocol/libraries/logic/ValidationLogic.sol index 24915aa5..e356a0d4 100644 --- a/contracts/protocol/libraries/logic/ValidationLogic.sol +++ b/contracts/protocol/libraries/logic/ValidationLogic.sol @@ -48,15 +48,13 @@ library ValidationLogic { require(!isPaused, Errors.VL_RESERVE_PAUSED); require(!isFrozen, Errors.VL_RESERVE_FROZEN); require( - IERC20(reserve.aTokenAddress) - .totalSupply() - .add(amount) - .div(10 ** reserve.configuration.getDecimals()) < reserve.configuration.getSupplyCap(), + IERC20(reserve.aTokenAddress).totalSupply().add(amount).div( + 10**reserve.configuration.getDecimals() + ) < reserve.configuration.getSupplyCap(), Errors.VL_SUPPLY_CAP_EXCEEDED ); } - /** * @dev Validates a withdraw action * @param reserve The reserve object @@ -71,7 +69,7 @@ library ValidationLogic { require(amount != 0, Errors.VL_INVALID_AMOUNT); require(amount <= userBalance, Errors.VL_NOT_ENOUGH_AVAILABLE_USER_BALANCE); - (bool isActive, , , , bool isPaused) = reservesData[reserveAddress].configuration.getFlags(); + (bool isActive, , , , bool isPaused) = reserve.configuration.getFlags(); require(isActive, Errors.VL_NO_ACTIVE_RESERVE); require(!isPaused, Errors.VL_RESERVE_PAUSED); } @@ -124,9 +122,13 @@ library ValidationLogic { ) internal view { ValidateBorrowLocalVars memory vars; - (vars.isActive, vars.isFrozen, vars.borrowingEnabled, vars.stableRateBorrowingEnabled, vars.isPaused) = reserve - .configuration - .getFlags(); + ( + vars.isActive, + vars.isFrozen, + vars.borrowingEnabled, + vars.stableRateBorrowingEnabled, + vars.isPaused + ) = reserve.configuration.getFlags(); require(vars.isActive, Errors.VL_NO_ACTIVE_RESERVE); require(!vars.isPaused, Errors.VL_RESERVE_PAUSED); @@ -141,22 +143,20 @@ library ValidationLogic { uint256(DataTypes.InterestRateMode.STABLE) == interestRateMode, Errors.VL_INVALID_INTEREST_RATE_MODE_SELECTED ); - + (vars.totalSupplyStableDebt, ) = IStableDebtToken(reserve.stableDebtTokenAddress) .getTotalSupplyAndAvgRate(); vars.totalSupplyVariableDebt = IVariableDebtToken(reserve.variableDebtTokenAddress) - .scaledTotalSupply() - .rayMul(reserve.variableBorrowIndex); - - + .scaledTotalSupply() + .rayMul(reserve.variableBorrowIndex); + require( - vars.totalSupplyStableDebt - .add(vars.totalSupplyVariableDebt) - .add(amount) - .div(10 ** reserve.configuration.getDecimals()) - < reserve.configuration.getBorrowCap(), - Errors.VL_BORROW_CAP_EXCEEDED); + vars.totalSupplyStableDebt.add(vars.totalSupplyVariableDebt).add(amount).div( + 10**reserve.configuration.getDecimals() + ) < reserve.configuration.getBorrowCap(), + Errors.VL_BORROW_CAP_EXCEEDED + ); ( vars.userCollateralBalanceETH, @@ -271,7 +271,8 @@ library ValidationLogic { uint256 variableDebt, DataTypes.InterestRateMode currentRateMode ) external view { - (bool isActive, bool isFrozen, , bool stableRateEnabled, bool isPaused) = reserve.configuration.getFlags(); + (bool isActive, bool isFrozen, , bool stableRateEnabled, bool isPaused) = + reserve.configuration.getFlags(); require(isActive, Errors.VL_NO_ACTIVE_RESERVE); require(!isPaused, Errors.VL_RESERVE_PAUSED); @@ -346,9 +347,7 @@ library ValidationLogic { * @dev Validates the action of setting an asset as collateral * @param reserve The state of the reserve that the user is enabling or disabling as collateral */ - function validateSetUseReserveAsCollateral( - DataTypes.ReserveData storage reserve - ) external view { + function validateSetUseReserveAsCollateral(DataTypes.ReserveData storage reserve) external view { uint256 underlyingBalance = IERC20(reserve.aTokenAddress).balanceOf(msg.sender); bool isPaused = reserve.configuration.getPaused(); @@ -363,16 +362,13 @@ library ValidationLogic { * @param amounts The amounts for each asset being borrowed **/ function validateFlashloan( - address[] memory assets, + address[] memory assets, uint256[] memory amounts, mapping(address => DataTypes.ReserveData) storage reservesData ) internal view { - for (uint i = 0; i < assets.length; i++) { - require( - !reservesData[assets[i]].configuration.getPaused(), - Errors.VL_RESERVE_PAUSED - ); - } + for (uint256 i = 0; i < assets.length; i++) { + require(!reservesData[assets[i]].configuration.getPaused(), Errors.VL_RESERVE_PAUSED); + } require(assets.length == amounts.length, Errors.VL_INCONSISTENT_FLASHLOAN_PARAMS); } @@ -401,13 +397,8 @@ library ValidationLogic { Errors.VL_NO_ACTIVE_RESERVE ); } - if ( - collateralReserve.configuration.getPaused() || principalReserve.configuration.getPaused() - ) { - return ( - uint256(Errors.CollateralManagerErrors.PAUSED_RESERVE), - Errors.VL_RESERVE_PAUSED - ); + if (collateralReserve.configuration.getPaused() || principalReserve.configuration.getPaused()) { + return (uint256(Errors.CollateralManagerErrors.PAUSED_RESERVE), Errors.VL_RESERVE_PAUSED); } if (userHealthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) { @@ -449,7 +440,6 @@ library ValidationLogic { * @param oracle The price oracle */ function validateHealthFactor( - address reserveAddress, address from, mapping(address => DataTypes.ReserveData) storage reservesData, DataTypes.UserConfigurationMap storage userConfig, @@ -457,8 +447,6 @@ library ValidationLogic { uint256 reservesCount, address oracle ) internal view { - bool isPaused = reservesData[reserveAddress].configuration.getPaused(); - require(!isPaused, Errors.VL_RESERVE_PAUSED); (, , , , uint256 healthFactor) = GenericLogic.calculateUserAccountData( from, @@ -474,4 +462,12 @@ library ValidationLogic { Errors.VL_HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD ); } + + /** + * @dev Validates a transfer action + * @param reserve The reserve object + */ + function validateTransfer(DataTypes.ReserveData storage reserve) internal view { + require(!reserve.configuration.getPaused(), Errors.VL_RESERVE_PAUSED); + } }