fix: cached reserve parameters in validateDeposit and validateBorrow

This commit is contained in:
Hadrien Charlanes 2021-05-25 11:22:13 +02:00
parent 56bf996885
commit 7a40b944df

View File

@ -41,21 +41,20 @@ library ValidationLogic {
* @param amount The amount to be deposited * @param amount The amount to be deposited
*/ */
function validateDeposit(DataTypes.ReserveData storage reserve, uint256 amount) internal view { function validateDeposit(DataTypes.ReserveData storage reserve, uint256 amount) internal view {
(bool isActive, bool isFrozen, , ) = reserve.configuration.getFlags(); DataTypes.ReserveConfigurationMap memory reserveConfiguration = reserve.configuration;
(bool isActive, bool isFrozen, , ) = reserveConfiguration.getFlagsMemory();
(, , , uint256 reserveDecimals, ) = reserveConfiguration.getParamsMemory();
require(amount != 0, Errors.VL_INVALID_AMOUNT); require(amount != 0, Errors.VL_INVALID_AMOUNT);
require(isActive, Errors.VL_NO_ACTIVE_RESERVE); require(isActive, Errors.VL_NO_ACTIVE_RESERVE);
require(!isFrozen, Errors.VL_RESERVE_FROZEN); require(!isFrozen, Errors.VL_RESERVE_FROZEN);
require( require(
IERC20(reserve.aTokenAddress) IERC20(reserve.aTokenAddress).totalSupply().add(amount).div(10**reserveDecimals) <
.totalSupply() reserveConfiguration.getSupplyCapMemory(),
.add(amount)
.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
@ -66,7 +65,7 @@ library ValidationLogic {
DataTypes.ReserveData storage reserve, DataTypes.ReserveData storage reserve,
uint256 amount, uint256 amount,
uint256 userBalance uint256 userBalance
) internal view { ) external view {
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);
@ -84,6 +83,7 @@ library ValidationLogic {
uint256 healthFactor; uint256 healthFactor;
uint256 totalSupplyStableDebt; uint256 totalSupplyStableDebt;
uint256 totalSupplyVariableDebt; uint256 totalSupplyVariableDebt;
uint256 reserveDecimals;
bool isActive; bool isActive;
bool isFrozen; bool isFrozen;
bool borrowingEnabled; bool borrowingEnabled;
@ -121,9 +121,15 @@ library ValidationLogic {
) internal view { ) internal view {
ValidateBorrowLocalVars memory vars; ValidateBorrowLocalVars memory vars;
(vars.isActive, vars.isFrozen, vars.borrowingEnabled, vars.stableRateBorrowingEnabled) = reserve DataTypes.ReserveConfigurationMap memory reserveConfiguration = reserve.configuration;
.configuration (, , , vars.reserveDecimals, ) = reserveConfiguration.getParamsMemory();
.getFlags();
(
vars.isActive,
vars.isFrozen,
vars.borrowingEnabled,
vars.stableRateBorrowingEnabled
) = reserveConfiguration.getFlagsMemory();
require(vars.isActive, Errors.VL_NO_ACTIVE_RESERVE); require(vars.isActive, Errors.VL_NO_ACTIVE_RESERVE);
require(!vars.isFrozen, Errors.VL_RESERVE_FROZEN); require(!vars.isFrozen, Errors.VL_RESERVE_FROZEN);
@ -138,21 +144,16 @@ library ValidationLogic {
Errors.VL_INVALID_INTEREST_RATE_MODE_SELECTED Errors.VL_INVALID_INTEREST_RATE_MODE_SELECTED
); );
(vars.totalSupplyStableDebt, ) = IStableDebtToken(reserve.stableDebtTokenAddress) vars.totalSupplyStableDebt = IERC20(reserve.stableDebtTokenAddress).totalSupply();
.getTotalSupplyAndAvgRate();
vars.totalSupplyVariableDebt = IVariableDebtToken(reserve.variableDebtTokenAddress)
.scaledTotalSupply()
.rayMul(reserve.variableBorrowIndex);
vars.totalSupplyVariableDebt = IERC20(reserve.variableDebtTokenAddress).totalSupply();
require( require(
vars.totalSupplyStableDebt vars.totalSupplyStableDebt.add(vars.totalSupplyVariableDebt).add(amount).div(
.add(vars.totalSupplyVariableDebt) 10**vars.reserveDecimals
.add(amount) ) < reserveConfiguration.getBorrowCapMemory(),
.div(10 ** reserve.configuration.getDecimals()) Errors.VL_BORROW_CAP_EXCEEDED
< reserve.configuration.getBorrowCap(), );
Errors.VL_BORROW_CAP_EXCEEDED);
( (
vars.userCollateralBalanceETH, vars.userCollateralBalanceETH,
@ -231,7 +232,7 @@ library ValidationLogic {
address onBehalfOf, address onBehalfOf,
uint256 stableDebt, uint256 stableDebt,
uint256 variableDebt uint256 variableDebt
) internal view { ) external view {
bool isActive = reserve.configuration.getActive(); bool isActive = reserve.configuration.getActive();
require(isActive, Errors.VL_NO_ACTIVE_RESERVE); require(isActive, Errors.VL_NO_ACTIVE_RESERVE);
@ -340,9 +341,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);
require(underlyingBalance > 0, Errors.VL_UNDERLYING_BALANCE_NOT_GREATER_THAN_0); require(underlyingBalance > 0, Errors.VL_UNDERLYING_BALANCE_NOT_GREATER_THAN_0);
@ -353,7 +352,7 @@ library ValidationLogic {
* @param assets The assets being flashborrowed * @param assets The assets being flashborrowed
* @param amounts The amounts for each asset being borrowed * @param amounts The amounts for each asset being borrowed
**/ **/
function validateFlashloan(address[] memory assets, uint256[] memory amounts) internal pure { function validateFlashloan(address[] memory assets, uint256[] memory amounts) external pure {
require(assets.length == amounts.length, Errors.VL_INCONSISTENT_FLASHLOAN_PARAMS); require(assets.length == amounts.length, Errors.VL_INCONSISTENT_FLASHLOAN_PARAMS);
} }