From 2e30bb8b858bd33c00df00b74ca797947747cccb Mon Sep 17 00:00:00 2001 From: The3D Date: Mon, 21 Sep 2020 21:15:12 +0200 Subject: [PATCH] Fixed error on rebalance conditions, changed style of internal functions --- contracts/lendingpool/LendingPool.sol | 47 +++++++++---------- .../configuration/ReserveConfiguration.sol | 2 +- contracts/libraries/logic/ValidationLogic.sol | 4 +- test/helpers/actions.ts | 5 ++ test/scenario.spec.ts | 2 +- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 68b6182d6..4d5ef9e81 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -35,7 +35,6 @@ import {IReserveInterestRateStrategy} from '../interfaces/IReserveInterestRateSt * @notice Implements the actions of the LendingPool, and exposes accessory methods to fetch the users and reserve data * @author Aave **/ - contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage { using SafeMath for uint256; using WadRayMath for uint256; @@ -45,7 +44,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage //main configuration parameters uint256 public constant REBALANCE_UP_LIQUIDITY_RATE_THRESHOLD = 4000; uint256 public constant REBALANCE_UP_USAGE_RATIO_THRESHOLD = 0.95 * 1e27; //usage ratio of 95% - uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; + uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 2500; uint256 public constant FLASHLOAN_PREMIUM_TOTAL = 9; uint256 public constant MAX_NUMBER_RESERVES = 128; uint256 public constant LENDINGPOOL_REVISION = 0x2; @@ -53,13 +52,12 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage /** * @dev only lending pools configurator can use functions affected by this modifier **/ - function onlyLendingPoolConfigurator() internal view { + function _onlyLendingPoolConfigurator() internal view { require( _addressesProvider.getLendingPoolConfigurator() == msg.sender, Errors.CALLER_NOT_LENDING_POOL_CONFIGURATOR ); } - /** * @dev Function to make a function callable only when the contract is not paused. * @@ -67,7 +65,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage * * - The contract must not be paused. */ - function whenNotPaused() internal view { + function _whenNotPaused() internal view { require(!_paused, Errors.IS_PAUSED); } @@ -97,7 +95,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage address onBehalfOf, uint16 referralCode ) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; ValidationLogic.validateDeposit(reserve, amount); @@ -126,7 +124,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage * @param amount the underlying amount to be redeemed **/ function withdraw(address asset, uint256 amount) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; address aToken = reserve.aTokenAddress; @@ -142,7 +140,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage ValidationLogic.validateWithdraw( asset, - aToken, amountToWithdraw, userBalance, _reserves, @@ -187,7 +184,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage uint256 interestRateMode, uint256 amount ) external override { - whenNotPaused(); + _whenNotPaused(); address debtToken = _reserves[asset].getDebtTokenAddress(interestRateMode); _borrowAllowance[debtToken][msg.sender][user] = amount; @@ -210,7 +207,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage uint16 referralCode, address onBehalfOf ) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; if (onBehalfOf != msg.sender) { @@ -250,7 +247,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage uint256 rateMode, address onBehalfOf ) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; @@ -307,7 +304,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage * @param rateMode the rate mode that the user wants to swap **/ function swapBorrowRateMode(address asset, uint256 rateMode) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve); @@ -360,7 +357,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage **/ function rebalanceStableBorrowRate(address asset, address user) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; @@ -373,8 +370,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage require(stableBorrowBalance > 0, Errors.NOT_ENOUGH_STABLE_BORROW_BALANCE); //if the utilization rate is below 95%, no rebalances are needed - uint256 totalBorrows = stableDebtToken.totalSupply().add(variableDebtToken.totalSupply()); - uint256 availableLiquidity = IERC20(reserve.aTokenAddress).totalSupply(); + uint256 totalBorrows = stableDebtToken.totalSupply().add(variableDebtToken.totalSupply()).wadToRay(); + uint256 availableLiquidity = IERC20(asset).balanceOf(reserve.aTokenAddress).wadToRay(); uint256 usageRatio = totalBorrows == 0 ? 0 : totalBorrows.rayDiv(availableLiquidity.add(totalBorrows)); @@ -414,7 +411,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage * @param useAsCollateral true if the user wants to user the deposit as collateral, false otherwise. **/ function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; ValidationLogic.validateSetUseReserveAsCollateral( @@ -451,7 +448,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage uint256 purchaseAmount, bool receiveAToken ) external override { - whenNotPaused(); + _whenNotPaused(); address collateralManager = _addressesProvider.getLendingPoolCollateralManager(); //solium-disable-next-line @@ -495,7 +492,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage address receiver, bytes calldata params ) external override { - whenNotPaused(); + _whenNotPaused(); require(!_flashLiquidationLocked, Errors.REENTRANCY_NOT_ALLOWED); _flashLiquidationLocked = true; @@ -551,7 +548,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage bytes calldata params, uint16 referralCode ) external override { - whenNotPaused(); + _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; FlashLoanLocalVars memory vars; @@ -613,7 +610,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage uint256 amountToSwap, bytes calldata params ) external override { - whenNotPaused(); + _whenNotPaused(); address collateralManager = _addressesProvider.getLendingPoolCollateralManager(); //solium-disable-next-line @@ -805,7 +802,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage address variableDebtAddress, address interestRateStrategyAddress ) external override { - onlyLendingPoolConfigurator(); + _onlyLendingPoolConfigurator(); _reserves[asset].init( aTokenAddress, stableDebtAddress, @@ -825,12 +822,12 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage external override { - onlyLendingPoolConfigurator(); + _onlyLendingPoolConfigurator(); _reserves[asset].interestRateStrategyAddress = rateStrategyAddress; } function setConfiguration(address asset, uint256 configuration) external override { - onlyLendingPoolConfigurator(); + _onlyLendingPoolConfigurator(); _reserves[asset].configuration.data = configuration; } @@ -986,7 +983,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage address user, uint256 amount ) external override view returns (bool) { - whenNotPaused(); + _whenNotPaused(); return GenericLogic.balanceDecreaseAllowed( asset, @@ -1004,7 +1001,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage * @param val the boolean value to set the current pause state of LendingPool */ function setPause(bool val) external override { - onlyLendingPoolConfigurator(); + _onlyLendingPoolConfigurator(); _paused = val; if (_paused) { diff --git a/contracts/libraries/configuration/ReserveConfiguration.sol b/contracts/libraries/configuration/ReserveConfiguration.sol index ed4df31ea..fb3d79724 100644 --- a/contracts/libraries/configuration/ReserveConfiguration.sol +++ b/contracts/libraries/configuration/ReserveConfiguration.sol @@ -143,7 +143,7 @@ library ReserveConfiguration { * @param self the reserve configuration * @param active the active state **/ - function setActive(ReserveConfiguration.Map memory self, bool active) internal { + function setActive(ReserveConfiguration.Map memory self, bool active) internal pure { self.data = (self.data & ACTIVE_MASK) | (uint256(active ? 1 : 0) << 56); } diff --git a/contracts/libraries/logic/ValidationLogic.sol b/contracts/libraries/logic/ValidationLogic.sol index 5d201efd3..4ffa49f21 100644 --- a/contracts/libraries/logic/ValidationLogic.sol +++ b/contracts/libraries/logic/ValidationLogic.sol @@ -34,7 +34,7 @@ library ValidationLogic { * @param reserve the reserve state on which the user is depositing * @param amount the amount to be deposited */ - function validateDeposit(ReserveLogic.ReserveData storage reserve, uint256 amount) internal view { + function validateDeposit(ReserveLogic.ReserveData storage reserve, uint256 amount) external view { (bool isActive, bool isFreezed, , ) = reserve.configuration.getFlags(); require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0); @@ -45,13 +45,11 @@ library ValidationLogic { /** * @dev validates a withdraw action. * @param reserveAddress the address of the reserve - * @param aTokenAddress the address of the aToken for the reserve * @param amount the amount to be withdrawn * @param userBalance the balance of the user */ function validateWithdraw( address reserveAddress, - address aTokenAddress, uint256 amount, uint256 userBalance, mapping(address => ReserveLogic.ReserveData) storage reservesData, diff --git a/test/helpers/actions.ts b/test/helpers/actions.ts index 69c9c6ced..d5bdfee8f 100644 --- a/test/helpers/actions.ts +++ b/test/helpers/actions.ts @@ -636,6 +636,11 @@ export const rebalanceStableBorrowRate = async ( testEnv ); + console.log("avl liquidity ", reserveDataBefore.availableLiquidity.toFixed()); + console.log("Total borrows stable ", reserveDataBefore.totalStableDebt.toFixed()); + console.log("Total borrows variable ", reserveDataBefore.totalVariableDebt.toFixed()); + console.log("Usage ratio ", reserveDataBefore.utilizationRate.toFixed()); + if (expectedResult === 'success') { const txResult = await waitForTx( await pool.connect(user.signer).rebalanceStableBorrowRate(reserve, target.address) diff --git a/test/scenario.spec.ts b/test/scenario.spec.ts index 54fe74334..0a77c9e85 100644 --- a/test/scenario.spec.ts +++ b/test/scenario.spec.ts @@ -10,7 +10,7 @@ import {executeStory} from './helpers/scenario-engine'; const scenarioFolder = './test/helpers/scenarios/'; -const selectedScenarios: string[] = []; +const selectedScenarios: string[] = ['rebalance-stable-rate.json']; fs.readdirSync(scenarioFolder).forEach((file) => { if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;