From 07db321b4dc7abedc3150f71acbe32db8e306a84 Mon Sep 17 00:00:00 2001 From: The3D Date: Mon, 21 Sep 2020 19:52:22 +0200 Subject: [PATCH 1/3] updated rebalance conditions,tests --- .../IReserveInterestRateStrategy.sol | 6 ++- .../DefaultReserveInterestRateStrategy.sol | 4 ++ contracts/lendingpool/LendingPool.sol | 51 ++++++++++++------- .../scenarios/rebalance-stable-rate.json | 43 +++------------- 4 files changed, 51 insertions(+), 53 deletions(-) diff --git a/contracts/interfaces/IReserveInterestRateStrategy.sol b/contracts/interfaces/IReserveInterestRateStrategy.sol index 48311e70..a69160b3 100644 --- a/contracts/interfaces/IReserveInterestRateStrategy.sol +++ b/contracts/interfaces/IReserveInterestRateStrategy.sol @@ -10,9 +10,13 @@ interface IReserveInterestRateStrategy { /** * @dev returns the base variable borrow rate, in rays */ - function baseVariableBorrowRate() external view returns (uint256); + /** + * @dev returns the maximum variable borrow rate + */ + function getMaxVariableBorrowRate() external view returns (uint256); + /** * @dev calculates the liquidity, stable, and variable rates depending on the current utilization rate * and the base parameters diff --git a/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol b/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol index c2303254..64b90a44 100644 --- a/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol +++ b/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol @@ -89,6 +89,10 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy { return _baseVariableBorrowRate; } + function getMaxVariableBorrowRate() external override view returns (uint256) { + return _baseVariableBorrowRate.add(_variableRateSlope1).add(_variableRateSlope2); + } + /** * @dev calculates the interest rates depending on the available liquidity and the total borrowed. * @param reserve the address of the reserve diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 86c47374..bd651647 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -12,6 +12,7 @@ import {IAToken} from '../tokenization/interfaces/IAToken.sol'; import {Helpers} from '../libraries/helpers/Helpers.sol'; import {Errors} from '../libraries/helpers/Errors.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol'; +import {PercentageMath} from '../libraries/math/PercentageMath.sol'; import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol'; import {GenericLogic} from '../libraries/logic/GenericLogic.sol'; import {ValidationLogic} from '../libraries/logic/ValidationLogic.sol'; @@ -26,6 +27,7 @@ import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol'; import {LendingPoolStorage} from './LendingPoolStorage.sol'; +import {IReserveInterestRateStrategy} from '../interfaces/IReserveInterestRateStrategy.sol'; /** * @title LendingPool contract @@ -36,10 +38,12 @@ import {LendingPoolStorage} from './LendingPoolStorage.sol'; contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage { using SafeMath for uint256; using WadRayMath for uint256; + using PercentageMath for uint256; using SafeERC20 for IERC20; //main configuration parameters - uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5; + 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 FLASHLOAN_PREMIUM_TOTAL = 9; @@ -354,26 +358,39 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; - IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress); + IERC20 stableDebtToken = IERC20(reserve.stableDebtTokenAddress); + IERC20 variableDebtToken = IERC20(reserve.variableDebtTokenAddress); - uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); + uint256 stableBorrowBalance = IERC20(stableDebtToken).balanceOf(user); // user must be borrowing on asset at a stable rate require(stableBorrowBalance > 0, Errors.NOT_ENOUGH_STABLE_BORROW_BALANCE); - uint256 rebalanceDownRateThreshold = WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA).rayMul( - reserve.currentStableBorrowRate - ); - - //1. user stable borrow rate is below the current liquidity rate. The loan needs to be rebalanced, - //as this situation can be abused (user putting back the borrowed liquidity in the same reserve to earn on it) - //2. user stable rate is above the market avg borrow rate of a certain delta, and utilization rate is low. - //In this case, the user is paying an interest that is too high, and needs to be rescaled down. - - uint256 userStableRate = stableDebtToken.getUserStableRate(user); + //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 utilizationRate = totalBorrows == 0 + ? 0 + : totalBorrows.rayDiv(availableLiquidity.add(totalBorrows)); require( - userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold, + utilizationRate >= REBALANCE_UP_USAGE_RATIO_THRESHOLD, + Errors.INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET + ); + + //if the liquidity rate is below REBALANCE_UP_THRESHOLD of the max variable APR at 95% usage, + //then we allow rebalancing of the stable rate positions. + + uint256 currentLiquidityRate = reserve.currentLiquidityRate; + uint256 maxVariableBorrowRate = IReserveInterestRateStrategy( + reserve + .interestRateStrategyAddress + ) + .getMaxVariableBorrowRate(); + + require( + currentLiquidityRate <= + maxVariableBorrowRate.percentMul(REBALANCE_UP_LIQUIDITY_RATE_THRESHOLD), Errors.INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET ); @@ -381,8 +398,8 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage reserve.updateCumulativeIndexesAndTimestamp(); - stableDebtToken.burn(user, stableBorrowBalance); - stableDebtToken.mint(user, stableBorrowBalance, reserve.currentStableBorrowRate); + IStableDebtToken(address(stableDebtToken)).burn(user, stableBorrowBalance); + IStableDebtToken(address(stableDebtToken)).mint(user, stableBorrowBalance, reserve.currentStableBorrowRate); reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); @@ -507,7 +524,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage _flashLiquidationLocked = false; } - struct FlashLoanLocalVars { + struct FlashLoanLocalVars { uint256 premium; uint256 amountPlusPremium; IFlashLoanReceiver receiver; diff --git a/test/helpers/scenarios/rebalance-stable-rate.json b/test/helpers/scenarios/rebalance-stable-rate.json index 1200ff3e..891de2b7 100644 --- a/test/helpers/scenarios/rebalance-stable-rate.json +++ b/test/helpers/scenarios/rebalance-stable-rate.json @@ -19,7 +19,7 @@ ] }, { - "description": "User 0 deposits 1000 DAI, user 1 deposits 1 ETH, borrows 100 DAI at a variable rate, user 0 rebalances user 1 (revert expected)", + "description": "User 0 deposits 1000 DAI, user 1 deposits 5 ETH, borrows 600 DAI at a variable rate, user 0 rebalances user 1 (revert expected)", "actions": [ { "name": "mint", @@ -51,7 +51,7 @@ "name": "mint", "args": { "reserve": "WETH", - "amount": "1", + "amount": "5", "user": "1" }, "expected": "success" @@ -69,7 +69,7 @@ "args": { "reserve": "WETH", - "amount": "1", + "amount": "5", "user": "1" }, "expected": "success" @@ -78,7 +78,7 @@ "name": "borrow", "args": { "reserve": "DAI", - "amount": "100", + "amount": "600", "borrowRateMode": "variable", "user": "1", "timeTravel": "365" @@ -122,7 +122,7 @@ ] }, { - "description": "User 2 deposits ETH and borrows the remaining DAI, causing the stable rates to rise (liquidity rate < user 1 borrow rate). User 0 tries to rebalance user 1 (revert expected)", + "description": "User 2 deposits ETH and borrows the remaining DAI, causing the stable rates to rise (usage ratio = 94%). User 0 tries to rebalance user 1 (revert expected)", "actions": [ { "name": "mint", @@ -155,7 +155,7 @@ "name": "borrow", "args": { "reserve": "DAI", - "amount": "100", + "amount": "340", "borrowRateMode": "variable", "user": "2" }, @@ -174,40 +174,13 @@ ] }, { - "description": "User 2 borrows more DAI, causing the liquidity rate to rise above user 1 stable borrow rate User 0 rebalances user 1", + "description": "User 2 borrows the remaining DAI (usage ratio = 100%). User 0 rebalances user 1", "actions": [ - { - "name": "mint", - "args": { - "reserve": "WETH", - "amount": "3", - "user": "2" - }, - "expected": "success" - }, - { - "name": "approve", - "args": { - "reserve": "WETH", - "user": "2" - }, - "expected": "success" - }, - { - "name": "deposit", - "args": { - "reserve": "WETH", - - "amount": "3", - "user": "2" - }, - "expected": "success" - }, { "name": "borrow", "args": { "reserve": "DAI", - "amount": "700", + "amount": "60", "borrowRateMode": "variable", "user": "2" }, From 2e30bb8b858bd33c00df00b74ca797947747cccb Mon Sep 17 00:00:00 2001 From: The3D Date: Mon, 21 Sep 2020 21:15:12 +0200 Subject: [PATCH 2/3] 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 68b6182d..4d5ef9e8 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 ed4df31e..fb3d7972 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 5d201efd..4ffa49f2 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 69c9c6ce..d5bdfee8 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 54fe7433..0a77c9e8 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; From 12f1dbd0dcebdaf9e6f9a140c4e1ec3fec7ccd8d Mon Sep 17 00:00:00 2001 From: The3D Date: Mon, 21 Sep 2020 22:08:44 +0200 Subject: [PATCH 3/3] Readded calculateAvailableBorrowsETH --- contracts/interfaces/ILendingPool.sol | 1 + contracts/lendingpool/LendingPool.sol | 25 +++++-- .../configuration/ReserveConfiguration.sol | 1 - contracts/libraries/logic/GenericLogic.sol | 24 +++++++ contracts/tokenization/StableDebtToken.sol | 1 - test/helpers/actions.ts | 5 -- .../scenarios/rebalance-stable-rate.json | 66 +++++++++++++++++-- test/scenario.spec.ts | 2 +- 8 files changed, 104 insertions(+), 21 deletions(-) diff --git a/contracts/interfaces/ILendingPool.sol b/contracts/interfaces/ILendingPool.sol index c98291c5..66d4542a 100644 --- a/contracts/interfaces/ILendingPool.sol +++ b/contracts/interfaces/ILendingPool.sol @@ -367,6 +367,7 @@ interface ILendingPool { returns ( uint256 totalCollateralETH, uint256 totalBorrowsETH, + uint256 availableBorrowsETH, uint256 currentLiquidationThreshold, uint256 ltv, uint256 healthFactor diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 4d5ef9e8..95af83b1 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -29,7 +29,6 @@ import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol'; import {LendingPoolStorage} from './LendingPoolStorage.sol'; import {IReserveInterestRateStrategy} from '../interfaces/IReserveInterestRateStrategy.sol'; - /** * @title LendingPool contract * @notice Implements the actions of the LendingPool, and exposes accessory methods to fetch the users and reserve data @@ -161,6 +160,14 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage emit Withdraw(asset, msg.sender, amount); } + /** + * @dev returns the borrow allowance of the user + * @param asset The underlying asset of the debt token + * @param fromUser The user to giving allowance + * @param toUser The user to give allowance to + * @param interestRateMode Type of debt: 1 for stable, 2 for variable + * @return the current allowance of toUser + **/ function getBorrowAllowance( address fromUser, address toUser, @@ -363,15 +370,13 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage IERC20 stableDebtToken = IERC20(reserve.stableDebtTokenAddress); IERC20 variableDebtToken = IERC20(reserve.variableDebtTokenAddress); + address aTokenAddress = reserve.aTokenAddress; uint256 stableBorrowBalance = IERC20(stableDebtToken).balanceOf(user); - // user must be borrowing on asset at a stable rate - 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()).wadToRay(); - uint256 availableLiquidity = IERC20(asset).balanceOf(reserve.aTokenAddress).wadToRay(); + uint256 availableLiquidity = IERC20(asset).balanceOf(aTokenAddress).wadToRay(); uint256 usageRatio = totalBorrows == 0 ? 0 : totalBorrows.rayDiv(availableLiquidity.add(totalBorrows)); @@ -398,11 +403,10 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage IStableDebtToken(address(stableDebtToken)).burn(user, stableBorrowBalance); IStableDebtToken(address(stableDebtToken)).mint(user, stableBorrowBalance, reserve.currentStableBorrowRate); - reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); + reserve.updateInterestRates(asset, aTokenAddress, 0, 0); emit RebalanceStableBorrowRate(asset, user); - return; } /** @@ -731,6 +735,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage returns ( uint256 totalCollateralETH, uint256 totalBorrowsETH, + uint256 availableBorrowsETH, uint256 currentLiquidationThreshold, uint256 ltv, uint256 healthFactor @@ -749,6 +754,12 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage _reservesList, _addressesProvider.getPriceOracle() ); + + availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH( + totalCollateralETH, + totalBorrowsETH, + ltv + ); } function getUserReserveData(address asset, address user) diff --git a/contracts/libraries/configuration/ReserveConfiguration.sol b/contracts/libraries/configuration/ReserveConfiguration.sol index fb3d7972..66fa21f4 100644 --- a/contracts/libraries/configuration/ReserveConfiguration.sol +++ b/contracts/libraries/configuration/ReserveConfiguration.sol @@ -6,7 +6,6 @@ import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {ReserveLogic} from '../logic/ReserveLogic.sol'; import {WadRayMath} from '../math/WadRayMath.sol'; import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; -import "@nomiclabs/buidler/console.sol"; /** * @title ReserveConfiguration library diff --git a/contracts/libraries/logic/GenericLogic.sol b/contracts/libraries/logic/GenericLogic.sol index 82993d28..4b64e85f 100644 --- a/contracts/libraries/logic/GenericLogic.sol +++ b/contracts/libraries/logic/GenericLogic.sol @@ -253,4 +253,28 @@ library GenericLogic { return (collateralBalanceETH.percentMul(liquidationThreshold)).wadDiv(borrowBalanceETH); } + + /** + * @dev calculates the equivalent amount in ETH that an user can borrow, depending on the available collateral and the + * average Loan To Value. + * @param collateralBalanceETH the total collateral balance + * @param borrowBalanceETH the total borrow balance + * @param ltv the average loan to value + * @return the amount available to borrow in ETH for the user + **/ + + function calculateAvailableBorrowsETH( + uint256 collateralBalanceETH, + uint256 borrowBalanceETH, + uint256 ltv + ) internal pure returns (uint256) { + uint256 availableBorrowsETH = collateralBalanceETH.percentMul(ltv); //ltv is in percentage + + if (availableBorrowsETH < borrowBalanceETH) { + return 0; + } + + availableBorrowsETH = availableBorrowsETH.sub(borrowBalanceETH); + return availableBorrowsETH; + } } diff --git a/contracts/tokenization/StableDebtToken.sol b/contracts/tokenization/StableDebtToken.sol index 1974d51e..160d0f88 100644 --- a/contracts/tokenization/StableDebtToken.sol +++ b/contracts/tokenization/StableDebtToken.sol @@ -8,7 +8,6 @@ import {DebtTokenBase} from './base/DebtTokenBase.sol'; import {MathUtils} from '../libraries/math/MathUtils.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {IStableDebtToken} from './interfaces/IStableDebtToken.sol'; -import "@nomiclabs/buidler/console.sol"; /** * @title contract StableDebtToken diff --git a/test/helpers/actions.ts b/test/helpers/actions.ts index d5bdfee8..69c9c6ce 100644 --- a/test/helpers/actions.ts +++ b/test/helpers/actions.ts @@ -636,11 +636,6 @@ 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/helpers/scenarios/rebalance-stable-rate.json b/test/helpers/scenarios/rebalance-stable-rate.json index 891de2b7..70ea820a 100644 --- a/test/helpers/scenarios/rebalance-stable-rate.json +++ b/test/helpers/scenarios/rebalance-stable-rate.json @@ -78,8 +78,8 @@ "name": "borrow", "args": { "reserve": "DAI", - "amount": "600", - "borrowRateMode": "variable", + "amount": "250", + "borrowRateMode": "stable", "user": "1", "timeTravel": "365" }, @@ -98,14 +98,68 @@ ] }, { - "description": "User 1 swaps to stable, user 0 tries to rebalance but the conditions are not met (revert expected)", + "description": "User 1 borrows another 200 at stable, user 0 tries to rebalance but the conditions are not met (revert expected)", "actions": [ { - "name": "swapBorrowRateMode", + "name": "borrow", "args": { "reserve": "DAI", + "amount": "200", + "borrowRateMode": "stable", "user": "1", - "borrowRateMode": "variable" + "timeTravel": "365" + }, + "expected": "success" + }, + { + "name": "rebalanceStableBorrowRate", + "args": { + "reserve": "DAI", + "user": "0", + "target": "1" + }, + "expected": "revert", + "revertMessage": "Interest rate rebalance conditions were not met" + } + ] + }, + { + "description": "User 1 borrows another 200 at stable, user 0 tries to rebalance but the conditions are not met (revert expected)", + "actions": [ + { + "name": "borrow", + "args": { + "reserve": "DAI", + "amount": "200", + "borrowRateMode": "stable", + "user": "1", + "timeTravel": "365" + }, + "expected": "success" + }, + { + "name": "rebalanceStableBorrowRate", + "args": { + "reserve": "DAI", + "user": "0", + "target": "1" + }, + "expected": "revert", + "revertMessage": "Interest rate rebalance conditions were not met" + } + ] + }, + { + "description": "User 1 borrows another 100 at stable, user 0 tries to rebalance but the conditions are not met (revert expected)", + "actions": [ + { + "name": "borrow", + "args": { + "reserve": "DAI", + "amount": "100", + "borrowRateMode": "stable", + "user": "1", + "timeTravel": "365" }, "expected": "success" }, @@ -155,7 +209,7 @@ "name": "borrow", "args": { "reserve": "DAI", - "amount": "340", + "amount": "190", "borrowRateMode": "variable", "user": "2" }, diff --git a/test/scenario.spec.ts b/test/scenario.spec.ts index 0a77c9e8..54fe7433 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[] = ['rebalance-stable-rate.json']; +const selectedScenarios: string[] = []; fs.readdirSync(scenarioFolder).forEach((file) => { if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;