fix: various fixes and refactorings on the implementation

This commit is contained in:
The3D 2021-07-01 16:59:14 +02:00
parent 716d1ce68d
commit 0fe470656e
2 changed files with 16 additions and 13 deletions

View File

@ -282,7 +282,9 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
DataTypes.ReserveData storage reserve = _reserves[asset]; DataTypes.ReserveData storage reserve = _reserves[asset];
DataTypes.ReserveCache memory reserveCache = reserve.cache(); DataTypes.ReserveCache memory reserveCache = reserve.cache();
ValidationLogic.validateSetUseReserveAsCollateral(reserveCache); uint256 userBalance = IERC20(reserveCache.aTokenAddress).balanceOf(msg.sender);
ValidationLogic.validateSetUseReserveAsCollateral(reserveCache, userBalance);
_usersConfig[msg.sender].setUsingAsCollateral(reserve.id, useAsCollateral); _usersConfig[msg.sender].setUsingAsCollateral(reserve.id, useAsCollateral);
@ -291,7 +293,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
} else { } else {
ValidationLogic.validateHFAndExposureCap( ValidationLogic.validateHFAndExposureCap(
asset, asset,
IERC20(reserveCache.aTokenAddress).balanceOf(msg.sender), userBalance,
msg.sender, msg.sender,
_reserves, _reserves,
_usersConfig[msg.sender], _usersConfig[msg.sender],
@ -633,7 +635,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
if (fromConfig.isBorrowingAny()) { if (fromConfig.isBorrowingAny()) {
ValidationLogic.validateHFAndExposureCap( ValidationLogic.validateHFAndExposureCap(
asset, asset,
0, amount,
from, from,
_reserves, _reserves,
_usersConfig[from], _usersConfig[from],

View File

@ -388,17 +388,16 @@ library ValidationLogic {
* @dev Validates the action of setting an asset as collateral * @dev Validates the action of setting an asset as collateral
* @param reserveCache The cached data of the reserve * @param reserveCache The cached data of the reserve
*/ */
function validateSetUseReserveAsCollateral(DataTypes.ReserveCache memory reserveCache) function validateSetUseReserveAsCollateral(
external DataTypes.ReserveCache memory reserveCache,
view uint256 userBalance
{ ) external pure {
uint256 underlyingBalance = IERC20(reserveCache.aTokenAddress).balanceOf(msg.sender);
(bool isActive, , , , bool isPaused) = reserveCache.reserveConfiguration.getFlagsMemory(); (bool isActive, , , , bool isPaused) = reserveCache.reserveConfiguration.getFlagsMemory();
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);
require(underlyingBalance > 0, Errors.VL_UNDERLYING_BALANCE_NOT_GREATER_THAN_0); require(userBalance > 0, Errors.VL_UNDERLYING_BALANCE_NOT_GREATER_THAN_0);
} }
/** /**
@ -519,6 +518,8 @@ library ValidationLogic {
/** /**
* @dev Validates the health factor of a user and the exposure cap for the asset being withdrawn * @dev Validates the health factor of a user and the exposure cap for the asset being withdrawn
* @param asset The asset for which the exposure cap will be validated
* @param expCapOffset The offset to consider on the total atoken supply of asset when validating the exposure cap
* @param from The user from which the aTokens are being transferred * @param from The user from which the aTokens are being transferred
* @param reservesData The state of all the reserves * @param reservesData The state of all the reserves
* @param userConfig The state of the user for the specific reserve * @param userConfig The state of the user for the specific reserve
@ -527,8 +528,8 @@ library ValidationLogic {
* @param oracle The price oracle * @param oracle The price oracle
*/ */
function validateHFAndExposureCap( function validateHFAndExposureCap(
address collateral, address asset,
uint256 collateralToBeWithdrawn, uint256 expCapOffset,
address from, address from,
mapping(address => DataTypes.ReserveData) storage reservesData, mapping(address => DataTypes.ReserveData) storage reservesData,
DataTypes.UserConfigurationMap storage userConfig, DataTypes.UserConfigurationMap storage userConfig,
@ -537,7 +538,7 @@ library ValidationLogic {
address oracle address oracle
) external view { ) external view {
validateHFAndExposureCapLocalVars memory vars; validateHFAndExposureCapLocalVars memory vars;
DataTypes.ReserveData memory reserve = reservesData[collateral]; DataTypes.ReserveData memory reserve = reservesData[asset];
(, , vars.ltv, , vars.healthFactor, vars.uncappedLtv) = GenericLogic.calculateUserAccountData( (, , vars.ltv, , vars.healthFactor, vars.uncappedLtv) = GenericLogic.calculateUserAccountData(
from, from,
reservesData, reservesData,
@ -559,7 +560,7 @@ library ValidationLogic {
vars.totalSupplyAtoken = IERC20(reserve.aTokenAddress).totalSupply(); vars.totalSupplyAtoken = IERC20(reserve.aTokenAddress).totalSupply();
(, , , vars.reserveDecimals, ) = reserve.configuration.getParamsMemory(); (, , , vars.reserveDecimals, ) = reserve.configuration.getParamsMemory();
bool isAssetCapped = bool isAssetCapped =
vars.totalSupplyAtoken.sub(collateralToBeWithdrawn).div(10**vars.reserveDecimals) >= vars.totalSupplyAtoken.sub(expCapOffset).div(10**vars.reserveDecimals) >=
vars.exposureCap; vars.exposureCap;
require(isAssetCapped, Errors.VL_COLLATERAL_EXPOSURE_CAP_EXCEEDED); require(isAssetCapped, Errors.VL_COLLATERAL_EXPOSURE_CAP_EXCEEDED);
} }