refactor: refactor validation logic after merging validateHealthFactor

This commit is contained in:
Hadrien Charlanes 2021-05-18 09:51:38 +02:00
parent 2f388c1b49
commit e18bd375cc
2 changed files with 39 additions and 42 deletions

View File

@ -720,6 +720,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
) external override whenNotPaused { ) external override whenNotPaused {
require(msg.sender == _reserves[asset].aTokenAddress, Errors.LP_CALLER_MUST_BE_AN_ATOKEN); require(msg.sender == _reserves[asset].aTokenAddress, Errors.LP_CALLER_MUST_BE_AN_ATOKEN);
ValidationLogic.validateTransfer(_reserves[asset]);
uint256 reserveId = _reserves[asset].id; uint256 reserveId = _reserves[asset].id;
if (from != to) { if (from != to) {
@ -728,7 +730,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
if (fromConfig.isUsingAsCollateral(reserveId)) { if (fromConfig.isUsingAsCollateral(reserveId)) {
if (fromConfig.isBorrowingAny()) { if (fromConfig.isBorrowingAny()) {
ValidationLogic.validateHealthFactor( ValidationLogic.validateHealthFactor(
asset,
from, from,
_reserves, _reserves,
_usersConfig[from], _usersConfig[from],

View File

@ -48,15 +48,13 @@ library ValidationLogic {
require(!isPaused, Errors.VL_RESERVE_PAUSED); require(!isPaused, Errors.VL_RESERVE_PAUSED);
require(!isFrozen, Errors.VL_RESERVE_FROZEN); require(!isFrozen, Errors.VL_RESERVE_FROZEN);
require( require(
IERC20(reserve.aTokenAddress) IERC20(reserve.aTokenAddress).totalSupply().add(amount).div(
.totalSupply() 10**reserve.configuration.getDecimals()
.add(amount) ) < reserve.configuration.getSupplyCap(),
.div(10 ** reserve.configuration.getDecimals()) < reserve.configuration.getSupplyCap(),
Errors.VL_SUPPLY_CAP_EXCEEDED Errors.VL_SUPPLY_CAP_EXCEEDED
); );
} }
/** /**
* @dev Validates a withdraw action * @dev Validates a withdraw action
* @param reserve The reserve object * @param reserve The reserve object
@ -71,7 +69,7 @@ library ValidationLogic {
require(amount != 0, Errors.VL_INVALID_AMOUNT); require(amount != 0, Errors.VL_INVALID_AMOUNT);
require(amount <= userBalance, Errors.VL_NOT_ENOUGH_AVAILABLE_USER_BALANCE); 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(isActive, Errors.VL_NO_ACTIVE_RESERVE);
require(!isPaused, Errors.VL_RESERVE_PAUSED); require(!isPaused, Errors.VL_RESERVE_PAUSED);
} }
@ -124,9 +122,13 @@ library ValidationLogic {
) internal view { ) internal view {
ValidateBorrowLocalVars memory vars; ValidateBorrowLocalVars memory vars;
(vars.isActive, vars.isFrozen, vars.borrowingEnabled, vars.stableRateBorrowingEnabled, vars.isPaused) = reserve (
.configuration vars.isActive,
.getFlags(); vars.isFrozen,
vars.borrowingEnabled,
vars.stableRateBorrowingEnabled,
vars.isPaused
) = reserve.configuration.getFlags();
require(vars.isActive, Errors.VL_NO_ACTIVE_RESERVE); require(vars.isActive, Errors.VL_NO_ACTIVE_RESERVE);
require(!vars.isPaused, Errors.VL_RESERVE_PAUSED); require(!vars.isPaused, Errors.VL_RESERVE_PAUSED);
@ -141,22 +143,20 @@ library ValidationLogic {
uint256(DataTypes.InterestRateMode.STABLE) == interestRateMode, uint256(DataTypes.InterestRateMode.STABLE) == interestRateMode,
Errors.VL_INVALID_INTEREST_RATE_MODE_SELECTED Errors.VL_INVALID_INTEREST_RATE_MODE_SELECTED
); );
(vars.totalSupplyStableDebt, ) = IStableDebtToken(reserve.stableDebtTokenAddress) (vars.totalSupplyStableDebt, ) = IStableDebtToken(reserve.stableDebtTokenAddress)
.getTotalSupplyAndAvgRate(); .getTotalSupplyAndAvgRate();
vars.totalSupplyVariableDebt = IVariableDebtToken(reserve.variableDebtTokenAddress) vars.totalSupplyVariableDebt = IVariableDebtToken(reserve.variableDebtTokenAddress)
.scaledTotalSupply() .scaledTotalSupply()
.rayMul(reserve.variableBorrowIndex); .rayMul(reserve.variableBorrowIndex);
require( require(
vars.totalSupplyStableDebt vars.totalSupplyStableDebt.add(vars.totalSupplyVariableDebt).add(amount).div(
.add(vars.totalSupplyVariableDebt) 10**reserve.configuration.getDecimals()
.add(amount) ) < reserve.configuration.getBorrowCap(),
.div(10 ** reserve.configuration.getDecimals()) Errors.VL_BORROW_CAP_EXCEEDED
< reserve.configuration.getBorrowCap(), );
Errors.VL_BORROW_CAP_EXCEEDED);
( (
vars.userCollateralBalanceETH, vars.userCollateralBalanceETH,
@ -271,7 +271,8 @@ library ValidationLogic {
uint256 variableDebt, uint256 variableDebt,
DataTypes.InterestRateMode currentRateMode DataTypes.InterestRateMode currentRateMode
) external view { ) 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(isActive, Errors.VL_NO_ACTIVE_RESERVE);
require(!isPaused, Errors.VL_RESERVE_PAUSED); require(!isPaused, Errors.VL_RESERVE_PAUSED);
@ -346,9 +347,7 @@ library ValidationLogic {
* @dev Validates the action of setting an asset as collateral * @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 * @param reserve The state of the reserve that the user is enabling or disabling as collateral
*/ */
function validateSetUseReserveAsCollateral( function validateSetUseReserveAsCollateral(DataTypes.ReserveData storage reserve) external view {
DataTypes.ReserveData storage reserve
) external view {
uint256 underlyingBalance = IERC20(reserve.aTokenAddress).balanceOf(msg.sender); uint256 underlyingBalance = IERC20(reserve.aTokenAddress).balanceOf(msg.sender);
bool isPaused = reserve.configuration.getPaused(); bool isPaused = reserve.configuration.getPaused();
@ -363,16 +362,13 @@ library ValidationLogic {
* @param amounts The amounts for each asset being borrowed * @param amounts The amounts for each asset being borrowed
**/ **/
function validateFlashloan( function validateFlashloan(
address[] memory assets, address[] memory assets,
uint256[] memory amounts, uint256[] memory amounts,
mapping(address => DataTypes.ReserveData) storage reservesData mapping(address => DataTypes.ReserveData) storage reservesData
) internal view { ) internal view {
for (uint i = 0; i < assets.length; i++) { for (uint256 i = 0; i < assets.length; i++) {
require( require(!reservesData[assets[i]].configuration.getPaused(), Errors.VL_RESERVE_PAUSED);
!reservesData[assets[i]].configuration.getPaused(), }
Errors.VL_RESERVE_PAUSED
);
}
require(assets.length == amounts.length, Errors.VL_INCONSISTENT_FLASHLOAN_PARAMS); require(assets.length == amounts.length, Errors.VL_INCONSISTENT_FLASHLOAN_PARAMS);
} }
@ -401,13 +397,8 @@ library ValidationLogic {
Errors.VL_NO_ACTIVE_RESERVE Errors.VL_NO_ACTIVE_RESERVE
); );
} }
if ( if (collateralReserve.configuration.getPaused() || principalReserve.configuration.getPaused()) {
collateralReserve.configuration.getPaused() || principalReserve.configuration.getPaused() return (uint256(Errors.CollateralManagerErrors.PAUSED_RESERVE), Errors.VL_RESERVE_PAUSED);
) {
return (
uint256(Errors.CollateralManagerErrors.PAUSED_RESERVE),
Errors.VL_RESERVE_PAUSED
);
} }
if (userHealthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) { if (userHealthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) {
@ -449,7 +440,6 @@ library ValidationLogic {
* @param oracle The price oracle * @param oracle The price oracle
*/ */
function validateHealthFactor( function validateHealthFactor(
address reserveAddress,
address from, address from,
mapping(address => DataTypes.ReserveData) storage reservesData, mapping(address => DataTypes.ReserveData) storage reservesData,
DataTypes.UserConfigurationMap storage userConfig, DataTypes.UserConfigurationMap storage userConfig,
@ -457,8 +447,6 @@ library ValidationLogic {
uint256 reservesCount, uint256 reservesCount,
address oracle address oracle
) internal view { ) internal view {
bool isPaused = reservesData[reserveAddress].configuration.getPaused();
require(!isPaused, Errors.VL_RESERVE_PAUSED);
(, , , , uint256 healthFactor) = (, , , , uint256 healthFactor) =
GenericLogic.calculateUserAccountData( GenericLogic.calculateUserAccountData(
from, from,
@ -474,4 +462,12 @@ library ValidationLogic {
Errors.VL_HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD 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);
}
} }