Fixed error on rebalance conditions, changed style of internal functions

This commit is contained in:
The3D 2020-09-21 21:15:12 +02:00
parent 332cdff67a
commit 2e30bb8b85
5 changed files with 30 additions and 30 deletions

View File

@ -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 * @notice Implements the actions of the LendingPool, and exposes accessory methods to fetch the users and reserve data
* @author Aave * @author Aave
**/ **/
contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage { contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage {
using SafeMath for uint256; using SafeMath for uint256;
using WadRayMath for uint256; using WadRayMath for uint256;
@ -45,7 +44,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
//main configuration parameters //main configuration parameters
uint256 public constant REBALANCE_UP_LIQUIDITY_RATE_THRESHOLD = 4000; 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 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 FLASHLOAN_PREMIUM_TOTAL = 9;
uint256 public constant MAX_NUMBER_RESERVES = 128; uint256 public constant MAX_NUMBER_RESERVES = 128;
uint256 public constant LENDINGPOOL_REVISION = 0x2; 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 * @dev only lending pools configurator can use functions affected by this modifier
**/ **/
function onlyLendingPoolConfigurator() internal view { function _onlyLendingPoolConfigurator() internal view {
require( require(
_addressesProvider.getLendingPoolConfigurator() == msg.sender, _addressesProvider.getLendingPoolConfigurator() == msg.sender,
Errors.CALLER_NOT_LENDING_POOL_CONFIGURATOR Errors.CALLER_NOT_LENDING_POOL_CONFIGURATOR
); );
} }
/** /**
* @dev Function to make a function callable only when the contract is not paused. * @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. * - The contract must not be paused.
*/ */
function whenNotPaused() internal view { function _whenNotPaused() internal view {
require(!_paused, Errors.IS_PAUSED); require(!_paused, Errors.IS_PAUSED);
} }
@ -97,7 +95,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
address onBehalfOf, address onBehalfOf,
uint16 referralCode uint16 referralCode
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
ValidationLogic.validateDeposit(reserve, amount); ValidationLogic.validateDeposit(reserve, amount);
@ -126,7 +124,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
* @param amount the underlying amount to be redeemed * @param amount the underlying amount to be redeemed
**/ **/
function withdraw(address asset, uint256 amount) external override { function withdraw(address asset, uint256 amount) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
address aToken = reserve.aTokenAddress; address aToken = reserve.aTokenAddress;
@ -142,7 +140,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
ValidationLogic.validateWithdraw( ValidationLogic.validateWithdraw(
asset, asset,
aToken,
amountToWithdraw, amountToWithdraw,
userBalance, userBalance,
_reserves, _reserves,
@ -187,7 +184,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
uint256 interestRateMode, uint256 interestRateMode,
uint256 amount uint256 amount
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
address debtToken = _reserves[asset].getDebtTokenAddress(interestRateMode); address debtToken = _reserves[asset].getDebtTokenAddress(interestRateMode);
_borrowAllowance[debtToken][msg.sender][user] = amount; _borrowAllowance[debtToken][msg.sender][user] = amount;
@ -210,7 +207,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
uint16 referralCode, uint16 referralCode,
address onBehalfOf address onBehalfOf
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
if (onBehalfOf != msg.sender) { if (onBehalfOf != msg.sender) {
@ -250,7 +247,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
uint256 rateMode, uint256 rateMode,
address onBehalfOf address onBehalfOf
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; 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 * @param rateMode the rate mode that the user wants to swap
**/ **/
function swapBorrowRateMode(address asset, uint256 rateMode) external override { function swapBorrowRateMode(address asset, uint256 rateMode) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve); (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 { function rebalanceStableBorrowRate(address asset, address user) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; 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); require(stableBorrowBalance > 0, Errors.NOT_ENOUGH_STABLE_BORROW_BALANCE);
//if the utilization rate is below 95%, no rebalances are needed //if the utilization rate is below 95%, no rebalances are needed
uint256 totalBorrows = stableDebtToken.totalSupply().add(variableDebtToken.totalSupply()); uint256 totalBorrows = stableDebtToken.totalSupply().add(variableDebtToken.totalSupply()).wadToRay();
uint256 availableLiquidity = IERC20(reserve.aTokenAddress).totalSupply(); uint256 availableLiquidity = IERC20(asset).balanceOf(reserve.aTokenAddress).wadToRay();
uint256 usageRatio = totalBorrows == 0 uint256 usageRatio = totalBorrows == 0
? 0 ? 0
: totalBorrows.rayDiv(availableLiquidity.add(totalBorrows)); : 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. * @param useAsCollateral true if the user wants to user the deposit as collateral, false otherwise.
**/ **/
function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external override { function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
ValidationLogic.validateSetUseReserveAsCollateral( ValidationLogic.validateSetUseReserveAsCollateral(
@ -451,7 +448,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
uint256 purchaseAmount, uint256 purchaseAmount,
bool receiveAToken bool receiveAToken
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
address collateralManager = _addressesProvider.getLendingPoolCollateralManager(); address collateralManager = _addressesProvider.getLendingPoolCollateralManager();
//solium-disable-next-line //solium-disable-next-line
@ -495,7 +492,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
address receiver, address receiver,
bytes calldata params bytes calldata params
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
require(!_flashLiquidationLocked, Errors.REENTRANCY_NOT_ALLOWED); require(!_flashLiquidationLocked, Errors.REENTRANCY_NOT_ALLOWED);
_flashLiquidationLocked = true; _flashLiquidationLocked = true;
@ -551,7 +548,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
bytes calldata params, bytes calldata params,
uint16 referralCode uint16 referralCode
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
ReserveLogic.ReserveData storage reserve = _reserves[asset]; ReserveLogic.ReserveData storage reserve = _reserves[asset];
FlashLoanLocalVars memory vars; FlashLoanLocalVars memory vars;
@ -613,7 +610,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
uint256 amountToSwap, uint256 amountToSwap,
bytes calldata params bytes calldata params
) external override { ) external override {
whenNotPaused(); _whenNotPaused();
address collateralManager = _addressesProvider.getLendingPoolCollateralManager(); address collateralManager = _addressesProvider.getLendingPoolCollateralManager();
//solium-disable-next-line //solium-disable-next-line
@ -805,7 +802,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
address variableDebtAddress, address variableDebtAddress,
address interestRateStrategyAddress address interestRateStrategyAddress
) external override { ) external override {
onlyLendingPoolConfigurator(); _onlyLendingPoolConfigurator();
_reserves[asset].init( _reserves[asset].init(
aTokenAddress, aTokenAddress,
stableDebtAddress, stableDebtAddress,
@ -825,12 +822,12 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
external external
override override
{ {
onlyLendingPoolConfigurator(); _onlyLendingPoolConfigurator();
_reserves[asset].interestRateStrategyAddress = rateStrategyAddress; _reserves[asset].interestRateStrategyAddress = rateStrategyAddress;
} }
function setConfiguration(address asset, uint256 configuration) external override { function setConfiguration(address asset, uint256 configuration) external override {
onlyLendingPoolConfigurator(); _onlyLendingPoolConfigurator();
_reserves[asset].configuration.data = configuration; _reserves[asset].configuration.data = configuration;
} }
@ -986,7 +983,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
address user, address user,
uint256 amount uint256 amount
) external override view returns (bool) { ) external override view returns (bool) {
whenNotPaused(); _whenNotPaused();
return return
GenericLogic.balanceDecreaseAllowed( GenericLogic.balanceDecreaseAllowed(
asset, asset,
@ -1004,7 +1001,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage
* @param val the boolean value to set the current pause state of LendingPool * @param val the boolean value to set the current pause state of LendingPool
*/ */
function setPause(bool val) external override { function setPause(bool val) external override {
onlyLendingPoolConfigurator(); _onlyLendingPoolConfigurator();
_paused = val; _paused = val;
if (_paused) { if (_paused) {

View File

@ -143,7 +143,7 @@ library ReserveConfiguration {
* @param self the reserve configuration * @param self the reserve configuration
* @param active the active state * @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); self.data = (self.data & ACTIVE_MASK) | (uint256(active ? 1 : 0) << 56);
} }

View File

@ -34,7 +34,7 @@ library ValidationLogic {
* @param reserve the reserve state on which the user is depositing * @param reserve the reserve state on which the user is depositing
* @param amount the amount to be deposited * @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(); (bool isActive, bool isFreezed, , ) = reserve.configuration.getFlags();
require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0); require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0);
@ -45,13 +45,11 @@ library ValidationLogic {
/** /**
* @dev validates a withdraw action. * @dev validates a withdraw action.
* @param reserveAddress the address of the reserve * @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 amount the amount to be withdrawn
* @param userBalance the balance of the user * @param userBalance the balance of the user
*/ */
function validateWithdraw( function validateWithdraw(
address reserveAddress, address reserveAddress,
address aTokenAddress,
uint256 amount, uint256 amount,
uint256 userBalance, uint256 userBalance,
mapping(address => ReserveLogic.ReserveData) storage reservesData, mapping(address => ReserveLogic.ReserveData) storage reservesData,

View File

@ -636,6 +636,11 @@ export const rebalanceStableBorrowRate = async (
testEnv 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') { if (expectedResult === 'success') {
const txResult = await waitForTx( const txResult = await waitForTx(
await pool.connect(user.signer).rebalanceStableBorrowRate(reserve, target.address) await pool.connect(user.signer).rebalanceStableBorrowRate(reserve, target.address)

View File

@ -10,7 +10,7 @@ import {executeStory} from './helpers/scenario-engine';
const scenarioFolder = './test/helpers/scenarios/'; const scenarioFolder = './test/helpers/scenarios/';
const selectedScenarios: string[] = []; const selectedScenarios: string[] = ['rebalance-stable-rate.json'];
fs.readdirSync(scenarioFolder).forEach((file) => { fs.readdirSync(scenarioFolder).forEach((file) => {
if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return; if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;