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
*/
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(isActive, Errors.VL_NO_ACTIVE_RESERVE);
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**reserveDecimals) <
reserveConfiguration.getSupplyCapMemory(),
Errors.VL_SUPPLY_CAP_EXCEEDED
);
}
/**
* @dev Validates a withdraw action
* @param reserve The reserve object
@ -66,7 +65,7 @@ library ValidationLogic {
DataTypes.ReserveData storage reserve,
uint256 amount,
uint256 userBalance
) internal view {
) external view {
require(amount != 0, Errors.VL_INVALID_AMOUNT);
require(amount <= userBalance, Errors.VL_NOT_ENOUGH_AVAILABLE_USER_BALANCE);
@ -84,6 +83,7 @@ library ValidationLogic {
uint256 healthFactor;
uint256 totalSupplyStableDebt;
uint256 totalSupplyVariableDebt;
uint256 reserveDecimals;
bool isActive;
bool isFrozen;
bool borrowingEnabled;
@ -121,9 +121,15 @@ library ValidationLogic {
) internal view {
ValidateBorrowLocalVars memory vars;
(vars.isActive, vars.isFrozen, vars.borrowingEnabled, vars.stableRateBorrowingEnabled) = reserve
.configuration
.getFlags();
DataTypes.ReserveConfigurationMap memory reserveConfiguration = reserve.configuration;
(, , , vars.reserveDecimals, ) = reserveConfiguration.getParamsMemory();
(
vars.isActive,
vars.isFrozen,
vars.borrowingEnabled,
vars.stableRateBorrowingEnabled
) = reserveConfiguration.getFlagsMemory();
require(vars.isActive, Errors.VL_NO_ACTIVE_RESERVE);
require(!vars.isFrozen, Errors.VL_RESERVE_FROZEN);
@ -137,22 +143,17 @@ 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);
vars.totalSupplyStableDebt = IERC20(reserve.stableDebtTokenAddress).totalSupply();
vars.totalSupplyVariableDebt = IERC20(reserve.variableDebtTokenAddress).totalSupply();
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**vars.reserveDecimals
) < reserveConfiguration.getBorrowCapMemory(),
Errors.VL_BORROW_CAP_EXCEEDED
);
(
vars.userCollateralBalanceETH,
@ -231,7 +232,7 @@ library ValidationLogic {
address onBehalfOf,
uint256 stableDebt,
uint256 variableDebt
) internal view {
) external view {
bool isActive = reserve.configuration.getActive();
require(isActive, Errors.VL_NO_ACTIVE_RESERVE);
@ -340,9 +341,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);
require(underlyingBalance > 0, Errors.VL_UNDERLYING_BALANCE_NOT_GREATER_THAN_0);
@ -353,7 +352,7 @@ library ValidationLogic {
* @param assets The assets being flashborrowed
* @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);
}