Skip to content

Commit

Permalink
Fixed error on rebalance conditions, changed style of internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
The3D committed Sep 21, 2020
1 parent 332cdff commit 2e30bb8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
47 changes: 22 additions & 25 deletions contracts/lendingpool/LendingPool.sol
Expand Up @@ -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;
Expand All @@ -45,29 +44,28 @@ 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;

/**
* @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.
*
* Requirements:
*
* - The contract must not be paused.
*/
function whenNotPaused() internal view {
function _whenNotPaused() internal view {
require(!_paused, Errors.IS_PAUSED);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -142,7 +140,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage

ValidationLogic.validateWithdraw(
asset,
aToken,
amountToWithdraw,
userBalance,
_reserves,
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -250,7 +247,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
uint256 rateMode,
address onBehalfOf
) external override {
whenNotPaused();
_whenNotPaused();

ReserveLogic.ReserveData storage reserve = _reserves[asset];

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];

Expand All @@ -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));
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -805,7 +802,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
address variableDebtAddress,
address interestRateStrategyAddress
) external override {
onlyLendingPoolConfigurator();
_onlyLendingPoolConfigurator();
_reserves[asset].init(
aTokenAddress,
stableDebtAddress,
Expand All @@ -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;
}

Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/libraries/configuration/ReserveConfiguration.sol
Expand Up @@ -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);
}

Expand Down
4 changes: 1 addition & 3 deletions contracts/libraries/logic/ValidationLogic.sol
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions test/helpers/actions.ts
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/scenario.spec.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 2e30bb8

Please sign in to comment.