From abe967c707c6e90af19df4accd242551ffbdf051 Mon Sep 17 00:00:00 2001 From: pol <> Date: Tue, 25 Aug 2020 15:32:22 +0200 Subject: [PATCH 01/11] Updated require message errors with constant string numbers to reduce gas --- contracts/lendingpool/LendingPool.sol | 1402 +++++++++++++------------ 1 file changed, 711 insertions(+), 691 deletions(-) diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index ec587920..ea384125 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -6,7 +6,7 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import { - VersionedInitializable + VersionedInitializable } from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {IAToken} from '../tokenization/interfaces/IAToken.sol'; @@ -32,774 +32,794 @@ import {ILendingPool} from '../interfaces/ILendingPool.sol'; **/ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { - using SafeMath for uint256; - using WadRayMath for uint256; - using ReserveLogic for ReserveLogic.ReserveData; - using ReserveConfiguration for ReserveConfiguration.Map; - using UserConfiguration for UserConfiguration.Map; - using SafeERC20 for IERC20; + using SafeMath for uint256; + using WadRayMath for uint256; + using ReserveLogic for ReserveLogic.ReserveData; + using ReserveConfiguration for ReserveConfiguration.Map; + using UserConfiguration for UserConfiguration.Map; + using SafeERC20 for IERC20; - //main configuration parameters - uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5; - uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; - uint256 public constant FLASHLOAN_FEE_TOTAL = 9; + //main configuration parameters + uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5; + uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; + uint256 public constant FLASHLOAN_FEE_TOTAL = 9; - ILendingPoolAddressesProvider internal _addressesProvider; + //require error messages + string private constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '1'; // 'User does not have any stable rate loan for this reserve' + string private constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '2'; // 'Interest rate rebalance conditions were not met' + string private constant LIQUIDATION_CALL_FAILED = '3'; // 'Liquidation call failed' + string private constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '4'; // 'There is not enough liquidity available to borrow' + string private constant REQUESTED_AMOUNT_TO_SMALL = '5'; // 'The requested amount is too small for a FlashLoan.' + string private constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '6'; // 'The actual balance of the protocol is inconsistent' - mapping(address => ReserveLogic.ReserveData) internal _reserves; - mapping(address => UserConfiguration.Map) internal _usersConfig; + ILendingPoolAddressesProvider internal _addressesProvider; - address[] internal _reservesList; + mapping(address => ReserveLogic.ReserveData) internal _reserves; + mapping(address => UserConfiguration.Map) internal _usersConfig; - /** - * @dev only lending pools configurator can use functions affected by this modifier - **/ - modifier onlyLendingPoolConfigurator { - require(_addressesProvider.getLendingPoolConfigurator() == msg.sender, '30'); - _; - } + address[] internal _reservesList; - uint256 public constant UINT_MAX_VALUE = uint256(-1); - - uint256 public constant LENDINGPOOL_REVISION = 0x2; - - function getRevision() internal override pure returns (uint256) { - return LENDINGPOOL_REVISION; - } - - /** - * @dev this function is invoked by the proxy contract when the LendingPool contract is added to the - * AddressesProvider. - * @param provider the address of the LendingPoolAddressesProvider registry - **/ - function initialize(ILendingPoolAddressesProvider provider) public initializer { - _addressesProvider = provider; - } - - /** - * @dev deposits The underlying asset into the reserve. A corresponding amount of the overlying asset (aTokens) - * is minted. - * @param asset the address of the reserve - * @param amount the amount to be deposited - * @param referralCode integrators are assigned a referral code and can potentially receive rewards. - **/ - function deposit( - address asset, - uint256 amount, - uint16 referralCode - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - - ValidationLogic.validateDeposit(reserve, amount); - - address aToken = reserve.aTokenAddress; - - reserve.updateCumulativeIndexesAndTimestamp(); - reserve.updateInterestRates(asset, aToken, amount, 0); - - bool isFirstDeposit = IAToken(aToken).balanceOf(msg.sender) == 0; - if (isFirstDeposit) { - _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, true); + /** + * @dev only lending pools configurator can use functions affected by this modifier + **/ + modifier onlyLendingPoolConfigurator { + require(_addressesProvider.getLendingPoolConfigurator() == msg.sender, '30'); + _; } - //minting AToken to user 1:1 with the specific exchange rate - IAToken(aToken).mint(msg.sender, amount); + uint256 public constant UINT_MAX_VALUE = uint256(-1); - //transfer to the aToken contract - IERC20(asset).safeTransferFrom(msg.sender, aToken, amount); + uint256 public constant LENDINGPOOL_REVISION = 0x2; - //solium-disable-next-line - emit Deposit(asset, msg.sender, amount, referralCode); - } - - /** - * @dev withdraws the _reserves of user. - * @param asset the address of the reserve - * @param amount the underlying amount to be redeemed - **/ - function withdraw(address asset, uint256 amount) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - - address aToken = reserve.aTokenAddress; - - uint256 userBalance = IAToken(aToken).balanceOf(msg.sender); - - uint256 amountToWithdraw = amount; - - //if amount is equal to uint(-1), the user wants to redeem everything - if (amount == UINT_MAX_VALUE) { - amountToWithdraw = userBalance; + function getRevision() internal override pure returns (uint256) { + return LENDINGPOOL_REVISION; } - ValidationLogic.validateWithdraw( - asset, - aToken, - amountToWithdraw, - userBalance, - _reserves, - _usersConfig[msg.sender], - _reservesList, - _addressesProvider.getPriceOracle() - ); - - reserve.updateCumulativeIndexesAndTimestamp(); - - reserve.updateInterestRates(asset, aToken, 0, amountToWithdraw); - - if (amountToWithdraw == userBalance) { - _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, false); + /** + * @dev this function is invoked by the proxy contract when the LendingPool contract is added to the + * AddressesProvider. + * @param provider the address of the LendingPoolAddressesProvider registry + **/ + function initialize(ILendingPoolAddressesProvider provider) public initializer { + _addressesProvider = provider; } - IAToken(aToken).burn(msg.sender, msg.sender, amountToWithdraw); + /** + * @dev deposits The underlying asset into the reserve. A corresponding amount of the overlying asset (aTokens) + * is minted. + * @param asset the address of the reserve + * @param amount the amount to be deposited + * @param referralCode integrators are assigned a referral code and can potentially receive rewards. + **/ + function deposit( + address asset, + uint256 amount, + uint16 referralCode + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - //solium-disable-next-line - emit Withdraw(asset, msg.sender, amount); - } + ValidationLogic.validateDeposit(reserve, amount); - /** - * @dev Allows users to borrow a specific amount of the reserve currency, provided that the borrower - * already deposited enough collateral. - * @param asset the address of the reserve - * @param amount the amount to be borrowed - * @param interestRateMode the interest rate mode at which the user wants to borrow. Can be 0 (STABLE) or 1 (VARIABLE) - **/ - function borrow( - address asset, - uint256 amount, - uint256 interestRateMode, - uint16 referralCode - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - UserConfiguration.Map storage userConfig = _usersConfig[msg.sender]; + address aToken = reserve.aTokenAddress; - uint256 amountInETH = IPriceOracleGetter(_addressesProvider.getPriceOracle()) - .getAssetPrice(asset) - .mul(amount) - .div(10**reserve.configuration.getDecimals()); //price is in ether + reserve.updateCumulativeIndexesAndTimestamp(); + reserve.updateInterestRates(asset, aToken, amount, 0); - ValidationLogic.validateBorrow( - reserve, - asset, - amount, - amountInETH, - interestRateMode, - MAX_STABLE_RATE_BORROW_SIZE_PERCENT, - _reserves, - _usersConfig[msg.sender], - _reservesList, - _addressesProvider.getPriceOracle() - ); + bool isFirstDeposit = IAToken(aToken).balanceOf(msg.sender) == 0; + if (isFirstDeposit) { + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, true); + } - //caching the current stable borrow rate - uint256 userStableRate = reserve.currentStableBorrowRate; + //minting AToken to user 1:1 with the specific exchange rate + IAToken(aToken).mint(msg.sender, amount); - reserve.updateCumulativeIndexesAndTimestamp(); + //transfer to the aToken contract + IERC20(asset).safeTransferFrom(msg.sender, aToken, amount); - if (ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE) { - IStableDebtToken(reserve.stableDebtTokenAddress).mint(msg.sender, amount, userStableRate); - } else { - IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount); + //solium-disable-next-line + emit Deposit(asset, msg.sender, amount, referralCode); } - address aToken = reserve.aTokenAddress; - reserve.updateInterestRates(asset, aToken, 0, amount); + /** + * @dev withdraws the _reserves of user. + * @param asset the address of the reserve + * @param amount the underlying amount to be redeemed + **/ + function withdraw(address asset, uint256 amount) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - uint256 reserveIndex = reserve.index; - if (!userConfig.isBorrowing(reserveIndex)) { - userConfig.setBorrowing(reserveIndex, true); + address aToken = reserve.aTokenAddress; + + uint256 userBalance = IAToken(aToken).balanceOf(msg.sender); + + uint256 amountToWithdraw = amount; + + //if amount is equal to uint(-1), the user wants to redeem everything + if (amount == UINT_MAX_VALUE) { + amountToWithdraw = userBalance; + } + + ValidationLogic.validateWithdraw( + asset, + aToken, + amountToWithdraw, + userBalance, + _reserves, + _usersConfig[msg.sender], + _reservesList, + _addressesProvider.getPriceOracle() + ); + + reserve.updateCumulativeIndexesAndTimestamp(); + + reserve.updateInterestRates(asset, aToken, 0, amountToWithdraw); + + if (amountToWithdraw == userBalance) { + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, false); + } + + IAToken(aToken).burn(msg.sender, msg.sender, amountToWithdraw); + + //solium-disable-next-line + emit Withdraw(asset, msg.sender, amount); } - //if we reached this point, we can transfer - IAToken(aToken).transferUnderlyingTo(msg.sender, amount); + /** + * @dev Allows users to borrow a specific amount of the reserve currency, provided that the borrower + * already deposited enough collateral. + * @param asset the address of the reserve + * @param amount the amount to be borrowed + * @param interestRateMode the interest rate mode at which the user wants to borrow. Can be 0 (STABLE) or 1 (VARIABLE) + **/ + function borrow( + address asset, + uint256 amount, + uint256 interestRateMode, + uint16 referralCode + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + UserConfiguration.Map storage userConfig = _usersConfig[msg.sender]; - emit Borrow( - asset, - msg.sender, - amount, - interestRateMode, - ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE - ? userStableRate - : reserve.currentVariableBorrowRate, - referralCode - ); - } + uint256 amountInETH = IPriceOracleGetter(_addressesProvider.getPriceOracle()) + .getAssetPrice(asset) + .mul(amount) + .div(10**reserve.configuration.getDecimals()); //price is in ether - /** - * @notice repays a borrow on the specific reserve, for the specified amount (or for the whole amount, if uint256(-1) is specified). - * @dev the target user is defined by onBehalfOf. If there is no repayment on behalf of another account, - * onBehalfOf must be equal to msg.sender. - * @param asset the address of the reserve on which the user borrowed - * @param amount the amount to repay, or uint256(-1) if the user wants to repay everything - * @param onBehalfOf the address for which msg.sender is repaying. - **/ - function repay( - address asset, - uint256 amount, - uint256 rateMode, - address onBehalfOf - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + ValidationLogic.validateBorrow( + reserve, + asset, + amount, + amountInETH, + interestRateMode, + MAX_STABLE_RATE_BORROW_SIZE_PERCENT, + _reserves, + _usersConfig[msg.sender], + _reservesList, + _addressesProvider.getPriceOracle() + ); - (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(onBehalfOf, reserve); + //caching the current stable borrow rate + uint256 userStableRate = reserve.currentStableBorrowRate; - ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); + reserve.updateCumulativeIndexesAndTimestamp(); - //default to max amount - uint256 paybackAmount = interestRateMode == ReserveLogic.InterestRateMode.STABLE - ? stableDebt - : variableDebt; + if ( + ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE + ) { + IStableDebtToken(reserve.stableDebtTokenAddress).mint( + msg.sender, + amount, + userStableRate + ); + } else { + IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount); + } - if (amount != UINT_MAX_VALUE && amount < paybackAmount) { - paybackAmount = amount; + address aToken = reserve.aTokenAddress; + reserve.updateInterestRates(asset, aToken, 0, amount); + + uint256 reserveIndex = reserve.index; + if (!userConfig.isBorrowing(reserveIndex)) { + userConfig.setBorrowing(reserveIndex, true); + } + + //if we reached this point, we can transfer + IAToken(aToken).transferUnderlyingTo(msg.sender, amount); + + emit Borrow( + asset, + msg.sender, + amount, + interestRateMode, + ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE + ? userStableRate + : reserve.currentVariableBorrowRate, + referralCode + ); } - ValidationLogic.validateRepay( - reserve, - amount, - interestRateMode, - onBehalfOf, - stableDebt, - variableDebt - ); + /** + * @notice repays a borrow on the specific reserve, for the specified amount (or for the whole amount, if uint256(-1) is specified). + * @dev the target user is defined by onBehalfOf. If there is no repayment on behalf of another account, + * onBehalfOf must be equal to msg.sender. + * @param asset the address of the reserve on which the user borrowed + * @param amount the amount to repay, or uint256(-1) if the user wants to repay everything + * @param onBehalfOf the address for which msg.sender is repaying. + **/ + function repay( + address asset, + uint256 amount, + uint256 rateMode, + address onBehalfOf + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - reserve.updateCumulativeIndexesAndTimestamp(); + (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt( + onBehalfOf, + reserve + ); - //burns an equivalent amount of debt tokens - if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { - IStableDebtToken(reserve.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount); - } else { - IVariableDebtToken(reserve.variableDebtTokenAddress).burn(onBehalfOf, paybackAmount); + ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); + + //default to max amount + uint256 paybackAmount = interestRateMode == ReserveLogic.InterestRateMode.STABLE + ? stableDebt + : variableDebt; + + if (amount != UINT_MAX_VALUE && amount < paybackAmount) { + paybackAmount = amount; + } + + ValidationLogic.validateRepay( + reserve, + amount, + interestRateMode, + onBehalfOf, + stableDebt, + variableDebt + ); + + reserve.updateCumulativeIndexesAndTimestamp(); + + //burns an equivalent amount of debt tokens + if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { + IStableDebtToken(reserve.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount); + } else { + IVariableDebtToken(reserve.variableDebtTokenAddress).burn(onBehalfOf, paybackAmount); + } + + address aToken = reserve.aTokenAddress; + reserve.updateInterestRates(asset, aToken, paybackAmount, 0); + + if (stableDebt.add(variableDebt).sub(paybackAmount) == 0) { + _usersConfig[onBehalfOf].setBorrowing(reserve.index, false); + } + + IERC20(asset).safeTransferFrom(msg.sender, aToken, paybackAmount); + + emit Repay(asset, onBehalfOf, msg.sender, paybackAmount); } - address aToken = reserve.aTokenAddress; - reserve.updateInterestRates(asset, aToken, paybackAmount, 0); + /** + * @dev borrowers can user this function to swap between stable and variable borrow rate modes. + * @param asset the address of the reserve on which the user borrowed + * @param rateMode the rate mode that the user wants to swap + **/ + function swapBorrowRateMode(address asset, uint256 rateMode) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - if (stableDebt.add(variableDebt).sub(paybackAmount) == 0) { - _usersConfig[onBehalfOf].setBorrowing(reserve.index, false); + (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt( + msg.sender, + reserve + ); + + ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); + + ValidationLogic.validateSwapRateMode( + reserve, + _usersConfig[msg.sender], + stableDebt, + variableDebt, + interestRateMode + ); + + reserve.updateCumulativeIndexesAndTimestamp(); + + if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { + //burn stable rate tokens, mint variable rate tokens + IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender, stableDebt); + IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableDebt); + } else { + //do the opposite + IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableDebt); + IStableDebtToken(reserve.stableDebtTokenAddress).mint( + msg.sender, + variableDebt, + reserve.currentStableBorrowRate + ); + } + + reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); + + emit Swap( + asset, + msg.sender, + //solium-disable-next-line + block.timestamp + ); } - IERC20(asset).safeTransferFrom(msg.sender, aToken, paybackAmount); + /** + * @dev rebalances the stable interest rate of a user if current liquidity rate > user stable rate. + * this is regulated by Aave to ensure that the protocol is not abused, and the user is paying a fair + * rate. Anyone can call this function. + * @param asset the address of the reserve + * @param user the address of the user to be rebalanced + **/ + function rebalanceStableBorrowRate(address asset, address user) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - emit Repay(asset, onBehalfOf, msg.sender, paybackAmount); - } + IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress); - /** - * @dev borrowers can user this function to swap between stable and variable borrow rate modes. - * @param asset the address of the reserve on which the user borrowed - * @param rateMode the rate mode that the user wants to swap - **/ - function swapBorrowRateMode(address asset, uint256 rateMode) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); - (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve); + // user must be borrowing on asset at a stable rate + require(stableBorrowBalance > 0, NOT_ENOUGH_STABLE_BORROW_BALANCE); - ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); + uint256 rebalanceDownRateThreshold = reserve.currentStableBorrowRate.rayMul( + WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA) + ); - ValidationLogic.validateSwapRateMode( - reserve, - _usersConfig[msg.sender], - stableDebt, - variableDebt, - interestRateMode - ); + //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. - reserve.updateCumulativeIndexesAndTimestamp(); + uint256 userStableRate = stableDebtToken.getUserStableRate(user); - if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { - //burn stable rate tokens, mint variable rate tokens - IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender, stableDebt); - IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableDebt); - } else { - //do the opposite - IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableDebt); - IStableDebtToken(reserve.stableDebtTokenAddress).mint( - msg.sender, - variableDebt, - reserve.currentStableBorrowRate - ); + require( + userStableRate < reserve.currentLiquidityRate || + userStableRate > rebalanceDownRateThreshold, + INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET + ); + + //burn old debt tokens, mint new ones + + reserve.updateCumulativeIndexesAndTimestamp(); + + stableDebtToken.burn(user, stableBorrowBalance); + stableDebtToken.mint(user, stableBorrowBalance, reserve.currentStableBorrowRate); + + reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); + + emit RebalanceStableBorrowRate(asset, user); + + return; } - reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); + /** + * @dev allows depositors to enable or disable a specific deposit as collateral. + * @param asset the address of the reserve + * @param useAsCollateral true if the user wants to user the deposit as collateral, false otherwise. + **/ + function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) + external + override + nonReentrant + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - emit Swap( - asset, - msg.sender, - //solium-disable-next-line - block.timestamp - ); - } + ValidationLogic.validateSetUseReserveAsCollateral( + reserve, + asset, + _reserves, + _usersConfig[msg.sender], + _reservesList, + _addressesProvider.getPriceOracle() + ); - /** - * @dev rebalances the stable interest rate of a user if current liquidity rate > user stable rate. - * this is regulated by Aave to ensure that the protocol is not abused, and the user is paying a fair - * rate. Anyone can call this function. - * @param asset the address of the reserve - * @param user the address of the user to be rebalanced - **/ - function rebalanceStableBorrowRate(address asset, address user) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, useAsCollateral); - IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress); - - uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); - - // user must be borrowing on asset at a stable rate - require(stableBorrowBalance > 0, 'User does not have any stable rate loan for this reserve'); - - uint256 rebalanceDownRateThreshold = reserve.currentStableBorrowRate.rayMul( - WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA) - ); - - //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); - - require( - userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold, - 'Interest rate rebalance conditions were not met' - ); - - //burn old debt tokens, mint new ones - - reserve.updateCumulativeIndexesAndTimestamp(); - - stableDebtToken.burn(user, stableBorrowBalance); - stableDebtToken.mint(user, stableBorrowBalance, reserve.currentStableBorrowRate); - - reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); - - emit RebalanceStableBorrowRate(asset, user); - - return; - } - - /** - * @dev allows depositors to enable or disable a specific deposit as collateral. - * @param asset the address of the reserve - * @param useAsCollateral true if the user wants to user the deposit as collateral, false otherwise. - **/ - function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) - external - override - nonReentrant - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - - ValidationLogic.validateSetUseReserveAsCollateral( - reserve, - asset, - _reserves, - _usersConfig[msg.sender], - _reservesList, - _addressesProvider.getPriceOracle() - ); - - _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, useAsCollateral); - - if (useAsCollateral) { - emit ReserveUsedAsCollateralEnabled(asset, msg.sender); - } else { - emit ReserveUsedAsCollateralDisabled(asset, msg.sender); + if (useAsCollateral) { + emit ReserveUsedAsCollateralEnabled(asset, msg.sender); + } else { + emit ReserveUsedAsCollateralDisabled(asset, msg.sender); + } } - } - /** - * @dev users can invoke this function to liquidate an undercollateralized position. - * @param asset the address of the collateral to liquidated - * @param asset the address of the principal reserve - * @param user the address of the borrower - * @param purchaseAmount the amount of principal that the liquidator wants to repay - * @param receiveAToken true if the liquidators wants to receive the aTokens, false if - * he wants to receive the underlying asset directly - **/ - function liquidationCall( - address collateral, - address asset, - address user, - uint256 purchaseAmount, - bool receiveAToken - ) external override nonReentrant { - address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager(); + /** + * @dev users can invoke this function to liquidate an undercollateralized position. + * @param asset the address of the collateral to liquidated + * @param asset the address of the principal reserve + * @param user the address of the borrower + * @param purchaseAmount the amount of principal that the liquidator wants to repay + * @param receiveAToken true if the liquidators wants to receive the aTokens, false if + * he wants to receive the underlying asset directly + **/ + function liquidationCall( + address collateral, + address asset, + address user, + uint256 purchaseAmount, + bool receiveAToken + ) external override nonReentrant { + address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager(); - //solium-disable-next-line - (bool success, bytes memory result) = liquidationManager.delegatecall( - abi.encodeWithSignature( - 'liquidationCall(address,address,address,uint256,bool)', - collateral, - asset, - user, - purchaseAmount, - receiveAToken - ) - ); - require(success, 'Liquidation call failed'); + //solium-disable-next-line + (bool success, bytes memory result) = liquidationManager.delegatecall( + abi.encodeWithSignature( + 'liquidationCall(address,address,address,uint256,bool)', + collateral, + asset, + user, + purchaseAmount, + receiveAToken + ) + ); + require(success, LIQUIDATION_CALL_FAILED); - (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); + (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); - if (returnCode != 0) { - //error found - revert(string(abi.encodePacked(returnMessage))); + if (returnCode != 0) { + //error found + revert(string(abi.encodePacked(returnMessage))); + } } - } - /** - * @dev allows smartcontracts to access the liquidity of the pool within one transaction, - * as long as the amount taken plus a fee is returned. NOTE There are security concerns for developers of flashloan receiver contracts - * that must be kept into consideration. For further details please visit https://developers.aave.com - * @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. - * @param asset the address of the principal reserve - * @param amount the amount requested for this flashloan - **/ - function flashLoan( - address receiverAddress, - address asset, - uint256 amount, - bytes calldata params - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + /** + * @dev allows smartcontracts to access the liquidity of the pool within one transaction, + * as long as the amount taken plus a fee is returned. NOTE There are security concerns for developers of flashloan receiver contracts + * that must be kept into consideration. For further details please visit https://developers.aave.com + * @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. + * @param asset the address of the principal reserve + * @param amount the amount requested for this flashloan + **/ + function flashLoan( + address receiverAddress, + address asset, + uint256 amount, + bytes calldata params + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - address aTokenAddress = reserve.aTokenAddress; + address aTokenAddress = reserve.aTokenAddress; - //check that the reserve has enough available liquidity - uint256 availableLiquidityBefore = IERC20(asset).balanceOf(aTokenAddress); + //check that the reserve has enough available liquidity + uint256 availableLiquidityBefore = IERC20(asset).balanceOf(aTokenAddress); - //calculate amount fee - uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); + //calculate amount fee + uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); - require( - availableLiquidityBefore >= amount, - 'There is not enough liquidity available to borrow' - ); - require(amountFee > 0, 'The requested amount is too small for a FlashLoan.'); + require(availableLiquidityBefore >= amount, NOT_ENOUGH_LIQUIDITY_TO_BORROW); + require(amountFee > 0, REQUESTED_AMOUNT_TO_SMALL); - //get the FlashLoanReceiver instance - IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); + //get the FlashLoanReceiver instance + IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); - //transfer funds to the receiver - IAToken(aTokenAddress).transferUnderlyingTo(receiverAddress, amount); + //transfer funds to the receiver + IAToken(aTokenAddress).transferUnderlyingTo(receiverAddress, amount); - //execute action of the receiver - receiver.executeOperation(asset, aTokenAddress, amount, amountFee, params); + //execute action of the receiver + receiver.executeOperation(asset, aTokenAddress, amount, amountFee, params); - //check that the actual balance of the core contract includes the returned amount - uint256 availableLiquidityAfter = IERC20(asset).balanceOf(aTokenAddress); + //check that the actual balance of the core contract includes the returned amount + uint256 availableLiquidityAfter = IERC20(asset).balanceOf(aTokenAddress); - require( - availableLiquidityAfter == availableLiquidityBefore.add(amountFee), - 'The actual balance of the protocol is inconsistent' - ); + require( + availableLiquidityAfter == availableLiquidityBefore.add(amountFee), + INCONSISTENT_PROTOCOL_ACTUAL_BALANCE + ); - //compounding the cumulated interest - reserve.updateCumulativeIndexesAndTimestamp(); + //compounding the cumulated interest + reserve.updateCumulativeIndexesAndTimestamp(); - uint256 totalLiquidityBefore = availableLiquidityBefore - .add(IERC20(reserve.variableDebtTokenAddress).totalSupply()) - .add(IERC20(reserve.stableDebtTokenAddress).totalSupply()); + uint256 totalLiquidityBefore = availableLiquidityBefore + .add(IERC20(reserve.variableDebtTokenAddress).totalSupply()) + .add(IERC20(reserve.stableDebtTokenAddress).totalSupply()); - //compounding the received fee into the reserve - reserve.cumulateToLiquidityIndex(totalLiquidityBefore, amountFee); + //compounding the received fee into the reserve + reserve.cumulateToLiquidityIndex(totalLiquidityBefore, amountFee); - //refresh interest rates - reserve.updateInterestRates(asset, aTokenAddress, amountFee, 0); + //refresh interest rates + reserve.updateInterestRates(asset, aTokenAddress, amountFee, 0); - //solium-disable-next-line - emit FlashLoan(receiverAddress, asset, amount, amountFee); - } - - /** - * @dev accessory functions to fetch data from the core contract - **/ - - function getReserveConfigurationData(address asset) - external - override - view - returns ( - uint256 decimals, - uint256 ltv, - uint256 liquidationThreshold, - uint256 liquidationBonus, - address interestRateStrategyAddress, - bool usageAsCollateralEnabled, - bool borrowingEnabled, - bool stableBorrowRateEnabled, - bool isActive, - bool isFreezed - ) - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - - return ( - reserve.configuration.getDecimals(), - reserve.configuration.getLtv(), - reserve.configuration.getLiquidationThreshold(), - reserve.configuration.getLiquidationBonus(), - reserve.interestRateStrategyAddress, - reserve.configuration.getLtv() != 0, - reserve.configuration.getBorrowingEnabled(), - reserve.configuration.getStableRateBorrowingEnabled(), - reserve.configuration.getActive(), - reserve.configuration.getFrozen() - ); - } - - function getReserveTokensAddresses(address asset) - external - override - view - returns ( - address aTokenAddress, - address stableDebtTokenAddress, - address variableDebtTokenAddress - ) - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - - return ( - reserve.aTokenAddress, - reserve.stableDebtTokenAddress, - reserve.variableDebtTokenAddress - ); - } - - function getReserveData(address asset) - external - override - view - returns ( - uint256 availableLiquidity, - uint256 totalBorrowsStable, - uint256 totalBorrowsVariable, - uint256 liquidityRate, - uint256 variableBorrowRate, - uint256 stableBorrowRate, - uint256 averageStableBorrowRate, - uint256 liquidityIndex, - uint256 variableBorrowIndex, - uint40 lastUpdateTimestamp - ) - { - ReserveLogic.ReserveData memory reserve = _reserves[asset]; - return ( - IERC20(asset).balanceOf(reserve.aTokenAddress), - IERC20(reserve.stableDebtTokenAddress).totalSupply(), - IERC20(reserve.variableDebtTokenAddress).totalSupply(), - reserve.currentLiquidityRate, - reserve.currentVariableBorrowRate, - reserve.currentStableBorrowRate, - IStableDebtToken(reserve.stableDebtTokenAddress).getAverageStableRate(), - reserve.lastLiquidityIndex, - reserve.lastVariableBorrowIndex, - reserve.lastUpdateTimestamp - ); - } - - function getUserAccountData(address user) - external - override - view - returns ( - uint256 totalCollateralETH, - uint256 totalBorrowsETH, - uint256 availableBorrowsETH, - uint256 currentLiquidationThreshold, - uint256 ltv, - uint256 healthFactor - ) - { - ( - totalCollateralETH, - totalBorrowsETH, - ltv, - currentLiquidationThreshold, - healthFactor - ) = GenericLogic.calculateUserAccountData( - user, - _reserves, - _usersConfig[user], - _reservesList, - _addressesProvider.getPriceOracle() - ); - - availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH( - totalCollateralETH, - totalBorrowsETH, - ltv - ); - } - - function getUserReserveData(address asset, address user) - external - override - view - returns ( - uint256 currentATokenBalance, - uint256 currentStableDebt, - uint256 currentVariableDebt, - uint256 principalStableDebt, - uint256 principalVariableDebt, - uint256 stableBorrowRate, - uint256 liquidityRate, - uint256 variableBorrowIndex, - uint40 stableRateLastUpdated, - bool usageAsCollateralEnabled - ) - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - - currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(user); - (currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(user, reserve); - (principalStableDebt, principalVariableDebt) = Helpers.getUserPrincipalDebt(user, reserve); - liquidityRate = reserve.currentLiquidityRate; - stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(user); - stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated( - user - ); - usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.index); - variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex(user); - } - - function getReserves() external override view returns (address[] memory) { - return _reservesList; - } - - receive() external payable { - revert(); - } - - /** - * @dev initializes a reserve - * @param asset the address of the reserve - * @param aTokenAddress the address of the overlying aToken contract - * @param interestRateStrategyAddress the address of the interest rate strategy contract - **/ - function initReserve( - address asset, - address aTokenAddress, - address stableDebtAddress, - address variableDebtAddress, - address interestRateStrategyAddress - ) external override onlyLendingPoolConfigurator { - _reserves[asset].init( - aTokenAddress, - stableDebtAddress, - variableDebtAddress, - interestRateStrategyAddress - ); - _addReserveToList(asset); - } - - /** - * @dev updates the address of the interest rate strategy contract - * @param asset the address of the reserve - * @param rateStrategyAddress the address of the interest rate strategy contract - **/ - - function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress) - external - override - onlyLendingPoolConfigurator - { - _reserves[asset].interestRateStrategyAddress = rateStrategyAddress; - } - - function setConfiguration(address asset, uint256 configuration) - external - override - onlyLendingPoolConfigurator - { - _reserves[asset].configuration.data = configuration; - } - - function getConfiguration(address asset) - external - override - view - returns (ReserveConfiguration.Map memory) - { - return _reserves[asset].configuration; - } - - /** - * @notice internal functions - **/ - - /** - * @dev adds a reserve to the array of the _reserves address - **/ - function _addReserveToList(address asset) internal { - bool reserveAlreadyAdded = false; - for (uint256 i = 0; i < _reservesList.length; i++) - if (_reservesList[i] == asset) { - reserveAlreadyAdded = true; - } - if (!reserveAlreadyAdded) { - _reserves[asset].index = uint8(_reservesList.length); - _reservesList.push(asset); + //solium-disable-next-line + emit FlashLoan(receiverAddress, asset, amount, amountFee); } - } - /** - * @dev returns the normalized income per unit of asset - * @param asset the address of the reserve - * @return the reserve normalized income - */ - function getReserveNormalizedIncome(address asset) external override view returns (uint256) { - return _reserves[asset].getNormalizedIncome(); - } + /** + * @dev accessory functions to fetch data from the core contract + **/ - /** - * @dev returns the normalized variable debt per unit of asset - * @param asset the address of the reserve - * @return the reserve normalized debt - */ - function getReserveNormalizedVariableDebt(address asset) - external - override - view - returns (uint256) - { - return _reserves[asset].getNormalizedDebt(); - } + function getReserveConfigurationData(address asset) + external + override + view + returns ( + uint256 decimals, + uint256 ltv, + uint256 liquidationThreshold, + uint256 liquidationBonus, + address interestRateStrategyAddress, + bool usageAsCollateralEnabled, + bool borrowingEnabled, + bool stableBorrowRateEnabled, + bool isActive, + bool isFreezed + ) + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - /** - * @dev validate if a balance decrease for an asset is allowed - * @param asset the address of the reserve - * @param user the user related to the balance decrease - * @param amount the amount being transferred/redeemed - * @return true if the balance decrease can be allowed, false otherwise - */ - function balanceDecreaseAllowed( - address asset, - address user, - uint256 amount - ) external override view returns (bool) { - return - GenericLogic.balanceDecreaseAllowed( - asset, - user, - amount, - _reserves, - _usersConfig[user], - _reservesList, - _addressesProvider.getPriceOracle() - ); - } + return ( + reserve.configuration.getDecimals(), + reserve.configuration.getLtv(), + reserve.configuration.getLiquidationThreshold(), + reserve.configuration.getLiquidationBonus(), + reserve.interestRateStrategyAddress, + reserve.configuration.getLtv() != 0, + reserve.configuration.getBorrowingEnabled(), + reserve.configuration.getStableRateBorrowingEnabled(), + reserve.configuration.getActive(), + reserve.configuration.getFrozen() + ); + } - /** - * @dev returns the list of the initialized reserves - **/ - function getReservesList() external view returns (address[] memory) { - return _reservesList; - } + function getReserveTokensAddresses(address asset) + external + override + view + returns ( + address aTokenAddress, + address stableDebtTokenAddress, + address variableDebtTokenAddress + ) + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - /** - * @dev returns the addresses provider - **/ - function getAddressesProvider() external view returns (ILendingPoolAddressesProvider) { - return _addressesProvider; - } + return ( + reserve.aTokenAddress, + reserve.stableDebtTokenAddress, + reserve.variableDebtTokenAddress + ); + } + + function getReserveData(address asset) + external + override + view + returns ( + uint256 availableLiquidity, + uint256 totalBorrowsStable, + uint256 totalBorrowsVariable, + uint256 liquidityRate, + uint256 variableBorrowRate, + uint256 stableBorrowRate, + uint256 averageStableBorrowRate, + uint256 liquidityIndex, + uint256 variableBorrowIndex, + uint40 lastUpdateTimestamp + ) + { + ReserveLogic.ReserveData memory reserve = _reserves[asset]; + return ( + IERC20(asset).balanceOf(reserve.aTokenAddress), + IERC20(reserve.stableDebtTokenAddress).totalSupply(), + IERC20(reserve.variableDebtTokenAddress).totalSupply(), + reserve.currentLiquidityRate, + reserve.currentVariableBorrowRate, + reserve.currentStableBorrowRate, + IStableDebtToken(reserve.stableDebtTokenAddress).getAverageStableRate(), + reserve.lastLiquidityIndex, + reserve.lastVariableBorrowIndex, + reserve.lastUpdateTimestamp + ); + } + + function getUserAccountData(address user) + external + override + view + returns ( + uint256 totalCollateralETH, + uint256 totalBorrowsETH, + uint256 availableBorrowsETH, + uint256 currentLiquidationThreshold, + uint256 ltv, + uint256 healthFactor + ) + { + ( + totalCollateralETH, + totalBorrowsETH, + ltv, + currentLiquidationThreshold, + healthFactor + ) = GenericLogic.calculateUserAccountData( + user, + _reserves, + _usersConfig[user], + _reservesList, + _addressesProvider.getPriceOracle() + ); + + availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH( + totalCollateralETH, + totalBorrowsETH, + ltv + ); + } + + function getUserReserveData(address asset, address user) + external + override + view + returns ( + uint256 currentATokenBalance, + uint256 currentStableDebt, + uint256 currentVariableDebt, + uint256 principalStableDebt, + uint256 principalVariableDebt, + uint256 stableBorrowRate, + uint256 liquidityRate, + uint256 variableBorrowIndex, + uint40 stableRateLastUpdated, + bool usageAsCollateralEnabled + ) + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + + currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(user); + (currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(user, reserve); + (principalStableDebt, principalVariableDebt) = Helpers.getUserPrincipalDebt(user, reserve); + liquidityRate = reserve.currentLiquidityRate; + stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(user); + stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated( + user + ); + usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.index); + variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex( + user + ); + } + + function getReserves() external override view returns (address[] memory) { + return _reservesList; + } + + receive() external payable { + revert(); + } + + /** + * @dev initializes a reserve + * @param asset the address of the reserve + * @param aTokenAddress the address of the overlying aToken contract + * @param interestRateStrategyAddress the address of the interest rate strategy contract + **/ + function initReserve( + address asset, + address aTokenAddress, + address stableDebtAddress, + address variableDebtAddress, + address interestRateStrategyAddress + ) external override onlyLendingPoolConfigurator { + _reserves[asset].init( + aTokenAddress, + stableDebtAddress, + variableDebtAddress, + interestRateStrategyAddress + ); + _addReserveToList(asset); + } + + /** + * @dev updates the address of the interest rate strategy contract + * @param asset the address of the reserve + * @param rateStrategyAddress the address of the interest rate strategy contract + **/ + + function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress) + external + override + onlyLendingPoolConfigurator + { + _reserves[asset].interestRateStrategyAddress = rateStrategyAddress; + } + + function setConfiguration(address asset, uint256 configuration) + external + override + onlyLendingPoolConfigurator + { + _reserves[asset].configuration.data = configuration; + } + + function getConfiguration(address asset) + external + override + view + returns (ReserveConfiguration.Map memory) + { + return _reserves[asset].configuration; + } + + /** + * @notice internal functions + **/ + + /** + * @dev adds a reserve to the array of the _reserves address + **/ + function _addReserveToList(address asset) internal { + bool reserveAlreadyAdded = false; + for (uint256 i = 0; i < _reservesList.length; i++) + if (_reservesList[i] == asset) { + reserveAlreadyAdded = true; + } + if (!reserveAlreadyAdded) { + _reserves[asset].index = uint8(_reservesList.length); + _reservesList.push(asset); + } + } + + /** + * @dev returns the normalized income per unit of asset + * @param asset the address of the reserve + * @return the reserve normalized income + */ + function getReserveNormalizedIncome(address asset) external override view returns (uint256) { + return _reserves[asset].getNormalizedIncome(); + } + + /** + * @dev returns the normalized variable debt per unit of asset + * @param asset the address of the reserve + * @return the reserve normalized debt + */ + function getReserveNormalizedVariableDebt(address asset) + external + override + view + returns (uint256) + { + return _reserves[asset].getNormalizedDebt(); + } + + /** + * @dev validate if a balance decrease for an asset is allowed + * @param asset the address of the reserve + * @param user the user related to the balance decrease + * @param amount the amount being transferred/redeemed + * @return true if the balance decrease can be allowed, false otherwise + */ + function balanceDecreaseAllowed( + address asset, + address user, + uint256 amount + ) external override view returns (bool) { + return + GenericLogic.balanceDecreaseAllowed( + asset, + user, + amount, + _reserves, + _usersConfig[user], + _reservesList, + _addressesProvider.getPriceOracle() + ); + } + + /** + * @dev returns the list of the initialized reserves + **/ + function getReservesList() external view returns (address[] memory) { + return _reservesList; + } + + /** + * @dev returns the addresses provider + **/ + function getAddressesProvider() external view returns (ILendingPoolAddressesProvider) { + return _addressesProvider; + } } From 0f5017cc81c7d62bca3a8c33f7c8a01c68720554 Mon Sep 17 00:00:00 2001 From: pol <> Date: Tue, 25 Aug 2020 15:51:52 +0200 Subject: [PATCH 02/11] Updated require message errors with constant string numbers to reduce gas --- .prettierrc | 1 + contracts/lendingpool/LendingPool.sol | 1407 ++++++++--------- .../lendingpool/LendingPoolConfigurator.sol | 8 +- 3 files changed, 703 insertions(+), 713 deletions(-) diff --git a/.prettierrc b/.prettierrc index ec986c7a..2caa9822 100644 --- a/.prettierrc +++ b/.prettierrc @@ -3,6 +3,7 @@ "trailingComma": "es5", "semi": true, "singleQuote": true, + "tabWidth": 2, "overrides": [ { "files": "*.sol", diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index ea384125..be4b4d18 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -6,7 +6,7 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import { - VersionedInitializable + VersionedInitializable } from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {IAToken} from '../tokenization/interfaces/IAToken.sol'; @@ -32,794 +32,779 @@ import {ILendingPool} from '../interfaces/ILendingPool.sol'; **/ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { - using SafeMath for uint256; - using WadRayMath for uint256; - using ReserveLogic for ReserveLogic.ReserveData; - using ReserveConfiguration for ReserveConfiguration.Map; - using UserConfiguration for UserConfiguration.Map; - using SafeERC20 for IERC20; + using SafeMath for uint256; + using WadRayMath for uint256; + using ReserveLogic for ReserveLogic.ReserveData; + using ReserveConfiguration for ReserveConfiguration.Map; + using UserConfiguration for UserConfiguration.Map; + using SafeERC20 for IERC20; - //main configuration parameters - uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5; - uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; - uint256 public constant FLASHLOAN_FEE_TOTAL = 9; + //main configuration parameters + uint256 public constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5; + uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; + uint256 public constant FLASHLOAN_FEE_TOTAL = 9; - //require error messages - string private constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '1'; // 'User does not have any stable rate loan for this reserve' - string private constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '2'; // 'Interest rate rebalance conditions were not met' - string private constant LIQUIDATION_CALL_FAILED = '3'; // 'Liquidation call failed' - string private constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '4'; // 'There is not enough liquidity available to borrow' - string private constant REQUESTED_AMOUNT_TO_SMALL = '5'; // 'The requested amount is too small for a FlashLoan.' - string private constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '6'; // 'The actual balance of the protocol is inconsistent' + //require error messages + string private constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '1'; // 'User does not have any stable rate loan for this reserve' + string private constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '2'; // 'Interest rate rebalance conditions were not met' + string private constant LIQUIDATION_CALL_FAILED = '3'; // 'Liquidation call failed' + string private constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '4'; // 'There is not enough liquidity available to borrow' + string private constant REQUESTED_AMOUNT_TO_SMALL = '5'; // 'The requested amount is too small for a FlashLoan.' + string private constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '6'; // 'The actual balance of the protocol is inconsistent' - ILendingPoolAddressesProvider internal _addressesProvider; + ILendingPoolAddressesProvider internal _addressesProvider; - mapping(address => ReserveLogic.ReserveData) internal _reserves; - mapping(address => UserConfiguration.Map) internal _usersConfig; + mapping(address => ReserveLogic.ReserveData) internal _reserves; + mapping(address => UserConfiguration.Map) internal _usersConfig; - address[] internal _reservesList; + address[] internal _reservesList; - /** - * @dev only lending pools configurator can use functions affected by this modifier - **/ - modifier onlyLendingPoolConfigurator { - require(_addressesProvider.getLendingPoolConfigurator() == msg.sender, '30'); - _; + /** + * @dev only lending pools configurator can use functions affected by this modifier + **/ + modifier onlyLendingPoolConfigurator { + require(_addressesProvider.getLendingPoolConfigurator() == msg.sender, '30'); + _; + } + + uint256 public constant UINT_MAX_VALUE = uint256(-1); + + uint256 public constant LENDINGPOOL_REVISION = 0x2; + + function getRevision() internal override pure returns (uint256) { + return LENDINGPOOL_REVISION; + } + + /** + * @dev this function is invoked by the proxy contract when the LendingPool contract is added to the + * AddressesProvider. + * @param provider the address of the LendingPoolAddressesProvider registry + **/ + function initialize(ILendingPoolAddressesProvider provider) public initializer { + _addressesProvider = provider; + } + + /** + * @dev deposits The underlying asset into the reserve. A corresponding amount of the overlying asset (aTokens) + * is minted. + * @param asset the address of the reserve + * @param amount the amount to be deposited + * @param referralCode integrators are assigned a referral code and can potentially receive rewards. + **/ + function deposit( + address asset, + uint256 amount, + uint16 referralCode + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + + ValidationLogic.validateDeposit(reserve, amount); + + address aToken = reserve.aTokenAddress; + + reserve.updateCumulativeIndexesAndTimestamp(); + reserve.updateInterestRates(asset, aToken, amount, 0); + + bool isFirstDeposit = IAToken(aToken).balanceOf(msg.sender) == 0; + if (isFirstDeposit) { + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, true); } - uint256 public constant UINT_MAX_VALUE = uint256(-1); + //minting AToken to user 1:1 with the specific exchange rate + IAToken(aToken).mint(msg.sender, amount); - uint256 public constant LENDINGPOOL_REVISION = 0x2; + //transfer to the aToken contract + IERC20(asset).safeTransferFrom(msg.sender, aToken, amount); - function getRevision() internal override pure returns (uint256) { - return LENDINGPOOL_REVISION; + //solium-disable-next-line + emit Deposit(asset, msg.sender, amount, referralCode); + } + + /** + * @dev withdraws the _reserves of user. + * @param asset the address of the reserve + * @param amount the underlying amount to be redeemed + **/ + function withdraw(address asset, uint256 amount) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + + address aToken = reserve.aTokenAddress; + + uint256 userBalance = IAToken(aToken).balanceOf(msg.sender); + + uint256 amountToWithdraw = amount; + + //if amount is equal to uint(-1), the user wants to redeem everything + if (amount == UINT_MAX_VALUE) { + amountToWithdraw = userBalance; } - /** - * @dev this function is invoked by the proxy contract when the LendingPool contract is added to the - * AddressesProvider. - * @param provider the address of the LendingPoolAddressesProvider registry - **/ - function initialize(ILendingPoolAddressesProvider provider) public initializer { - _addressesProvider = provider; + ValidationLogic.validateWithdraw( + asset, + aToken, + amountToWithdraw, + userBalance, + _reserves, + _usersConfig[msg.sender], + _reservesList, + _addressesProvider.getPriceOracle() + ); + + reserve.updateCumulativeIndexesAndTimestamp(); + + reserve.updateInterestRates(asset, aToken, 0, amountToWithdraw); + + if (amountToWithdraw == userBalance) { + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, false); } - /** - * @dev deposits The underlying asset into the reserve. A corresponding amount of the overlying asset (aTokens) - * is minted. - * @param asset the address of the reserve - * @param amount the amount to be deposited - * @param referralCode integrators are assigned a referral code and can potentially receive rewards. - **/ - function deposit( - address asset, - uint256 amount, - uint16 referralCode - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + IAToken(aToken).burn(msg.sender, msg.sender, amountToWithdraw); - ValidationLogic.validateDeposit(reserve, amount); + //solium-disable-next-line + emit Withdraw(asset, msg.sender, amount); + } - address aToken = reserve.aTokenAddress; + /** + * @dev Allows users to borrow a specific amount of the reserve currency, provided that the borrower + * already deposited enough collateral. + * @param asset the address of the reserve + * @param amount the amount to be borrowed + * @param interestRateMode the interest rate mode at which the user wants to borrow. Can be 0 (STABLE) or 1 (VARIABLE) + **/ + function borrow( + address asset, + uint256 amount, + uint256 interestRateMode, + uint16 referralCode + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + UserConfiguration.Map storage userConfig = _usersConfig[msg.sender]; - reserve.updateCumulativeIndexesAndTimestamp(); - reserve.updateInterestRates(asset, aToken, amount, 0); + uint256 amountInETH = IPriceOracleGetter(_addressesProvider.getPriceOracle()) + .getAssetPrice(asset) + .mul(amount) + .div(10**reserve.configuration.getDecimals()); //price is in ether - bool isFirstDeposit = IAToken(aToken).balanceOf(msg.sender) == 0; - if (isFirstDeposit) { - _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, true); - } + ValidationLogic.validateBorrow( + reserve, + asset, + amount, + amountInETH, + interestRateMode, + MAX_STABLE_RATE_BORROW_SIZE_PERCENT, + _reserves, + _usersConfig[msg.sender], + _reservesList, + _addressesProvider.getPriceOracle() + ); - //minting AToken to user 1:1 with the specific exchange rate - IAToken(aToken).mint(msg.sender, amount); + //caching the current stable borrow rate + uint256 userStableRate = reserve.currentStableBorrowRate; - //transfer to the aToken contract - IERC20(asset).safeTransferFrom(msg.sender, aToken, amount); + reserve.updateCumulativeIndexesAndTimestamp(); - //solium-disable-next-line - emit Deposit(asset, msg.sender, amount, referralCode); + if (ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE) { + IStableDebtToken(reserve.stableDebtTokenAddress).mint(msg.sender, amount, userStableRate); + } else { + IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount); } - /** - * @dev withdraws the _reserves of user. - * @param asset the address of the reserve - * @param amount the underlying amount to be redeemed - **/ - function withdraw(address asset, uint256 amount) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + address aToken = reserve.aTokenAddress; + reserve.updateInterestRates(asset, aToken, 0, amount); - address aToken = reserve.aTokenAddress; - - uint256 userBalance = IAToken(aToken).balanceOf(msg.sender); - - uint256 amountToWithdraw = amount; - - //if amount is equal to uint(-1), the user wants to redeem everything - if (amount == UINT_MAX_VALUE) { - amountToWithdraw = userBalance; - } - - ValidationLogic.validateWithdraw( - asset, - aToken, - amountToWithdraw, - userBalance, - _reserves, - _usersConfig[msg.sender], - _reservesList, - _addressesProvider.getPriceOracle() - ); - - reserve.updateCumulativeIndexesAndTimestamp(); - - reserve.updateInterestRates(asset, aToken, 0, amountToWithdraw); - - if (amountToWithdraw == userBalance) { - _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, false); - } - - IAToken(aToken).burn(msg.sender, msg.sender, amountToWithdraw); - - //solium-disable-next-line - emit Withdraw(asset, msg.sender, amount); + uint256 reserveIndex = reserve.index; + if (!userConfig.isBorrowing(reserveIndex)) { + userConfig.setBorrowing(reserveIndex, true); } - /** - * @dev Allows users to borrow a specific amount of the reserve currency, provided that the borrower - * already deposited enough collateral. - * @param asset the address of the reserve - * @param amount the amount to be borrowed - * @param interestRateMode the interest rate mode at which the user wants to borrow. Can be 0 (STABLE) or 1 (VARIABLE) - **/ - function borrow( - address asset, - uint256 amount, - uint256 interestRateMode, - uint16 referralCode - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - UserConfiguration.Map storage userConfig = _usersConfig[msg.sender]; + //if we reached this point, we can transfer + IAToken(aToken).transferUnderlyingTo(msg.sender, amount); - uint256 amountInETH = IPriceOracleGetter(_addressesProvider.getPriceOracle()) - .getAssetPrice(asset) - .mul(amount) - .div(10**reserve.configuration.getDecimals()); //price is in ether + emit Borrow( + asset, + msg.sender, + amount, + interestRateMode, + ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE + ? userStableRate + : reserve.currentVariableBorrowRate, + referralCode + ); + } - ValidationLogic.validateBorrow( - reserve, - asset, - amount, - amountInETH, - interestRateMode, - MAX_STABLE_RATE_BORROW_SIZE_PERCENT, - _reserves, - _usersConfig[msg.sender], - _reservesList, - _addressesProvider.getPriceOracle() - ); + /** + * @notice repays a borrow on the specific reserve, for the specified amount (or for the whole amount, if uint256(-1) is specified). + * @dev the target user is defined by onBehalfOf. If there is no repayment on behalf of another account, + * onBehalfOf must be equal to msg.sender. + * @param asset the address of the reserve on which the user borrowed + * @param amount the amount to repay, or uint256(-1) if the user wants to repay everything + * @param onBehalfOf the address for which msg.sender is repaying. + **/ + function repay( + address asset, + uint256 amount, + uint256 rateMode, + address onBehalfOf + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - //caching the current stable borrow rate - uint256 userStableRate = reserve.currentStableBorrowRate; + (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(onBehalfOf, reserve); - reserve.updateCumulativeIndexesAndTimestamp(); + ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); - if ( - ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE - ) { - IStableDebtToken(reserve.stableDebtTokenAddress).mint( - msg.sender, - amount, - userStableRate - ); - } else { - IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount); - } + //default to max amount + uint256 paybackAmount = interestRateMode == ReserveLogic.InterestRateMode.STABLE + ? stableDebt + : variableDebt; - address aToken = reserve.aTokenAddress; - reserve.updateInterestRates(asset, aToken, 0, amount); - - uint256 reserveIndex = reserve.index; - if (!userConfig.isBorrowing(reserveIndex)) { - userConfig.setBorrowing(reserveIndex, true); - } - - //if we reached this point, we can transfer - IAToken(aToken).transferUnderlyingTo(msg.sender, amount); - - emit Borrow( - asset, - msg.sender, - amount, - interestRateMode, - ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE - ? userStableRate - : reserve.currentVariableBorrowRate, - referralCode - ); + if (amount != UINT_MAX_VALUE && amount < paybackAmount) { + paybackAmount = amount; } - /** - * @notice repays a borrow on the specific reserve, for the specified amount (or for the whole amount, if uint256(-1) is specified). - * @dev the target user is defined by onBehalfOf. If there is no repayment on behalf of another account, - * onBehalfOf must be equal to msg.sender. - * @param asset the address of the reserve on which the user borrowed - * @param amount the amount to repay, or uint256(-1) if the user wants to repay everything - * @param onBehalfOf the address for which msg.sender is repaying. - **/ - function repay( - address asset, - uint256 amount, - uint256 rateMode, - address onBehalfOf - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + ValidationLogic.validateRepay( + reserve, + amount, + interestRateMode, + onBehalfOf, + stableDebt, + variableDebt + ); - (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt( - onBehalfOf, - reserve - ); + reserve.updateCumulativeIndexesAndTimestamp(); - ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); - - //default to max amount - uint256 paybackAmount = interestRateMode == ReserveLogic.InterestRateMode.STABLE - ? stableDebt - : variableDebt; - - if (amount != UINT_MAX_VALUE && amount < paybackAmount) { - paybackAmount = amount; - } - - ValidationLogic.validateRepay( - reserve, - amount, - interestRateMode, - onBehalfOf, - stableDebt, - variableDebt - ); - - reserve.updateCumulativeIndexesAndTimestamp(); - - //burns an equivalent amount of debt tokens - if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { - IStableDebtToken(reserve.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount); - } else { - IVariableDebtToken(reserve.variableDebtTokenAddress).burn(onBehalfOf, paybackAmount); - } - - address aToken = reserve.aTokenAddress; - reserve.updateInterestRates(asset, aToken, paybackAmount, 0); - - if (stableDebt.add(variableDebt).sub(paybackAmount) == 0) { - _usersConfig[onBehalfOf].setBorrowing(reserve.index, false); - } - - IERC20(asset).safeTransferFrom(msg.sender, aToken, paybackAmount); - - emit Repay(asset, onBehalfOf, msg.sender, paybackAmount); + //burns an equivalent amount of debt tokens + if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { + IStableDebtToken(reserve.stableDebtTokenAddress).burn(onBehalfOf, paybackAmount); + } else { + IVariableDebtToken(reserve.variableDebtTokenAddress).burn(onBehalfOf, paybackAmount); } - /** - * @dev borrowers can user this function to swap between stable and variable borrow rate modes. - * @param asset the address of the reserve on which the user borrowed - * @param rateMode the rate mode that the user wants to swap - **/ - function swapBorrowRateMode(address asset, uint256 rateMode) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + address aToken = reserve.aTokenAddress; + reserve.updateInterestRates(asset, aToken, paybackAmount, 0); - (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt( - msg.sender, - reserve - ); - - ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); - - ValidationLogic.validateSwapRateMode( - reserve, - _usersConfig[msg.sender], - stableDebt, - variableDebt, - interestRateMode - ); - - reserve.updateCumulativeIndexesAndTimestamp(); - - if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { - //burn stable rate tokens, mint variable rate tokens - IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender, stableDebt); - IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableDebt); - } else { - //do the opposite - IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableDebt); - IStableDebtToken(reserve.stableDebtTokenAddress).mint( - msg.sender, - variableDebt, - reserve.currentStableBorrowRate - ); - } - - reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); - - emit Swap( - asset, - msg.sender, - //solium-disable-next-line - block.timestamp - ); + if (stableDebt.add(variableDebt).sub(paybackAmount) == 0) { + _usersConfig[onBehalfOf].setBorrowing(reserve.index, false); } - /** - * @dev rebalances the stable interest rate of a user if current liquidity rate > user stable rate. - * this is regulated by Aave to ensure that the protocol is not abused, and the user is paying a fair - * rate. Anyone can call this function. - * @param asset the address of the reserve - * @param user the address of the user to be rebalanced - **/ - function rebalanceStableBorrowRate(address asset, address user) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + IERC20(asset).safeTransferFrom(msg.sender, aToken, paybackAmount); - IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress); + emit Repay(asset, onBehalfOf, msg.sender, paybackAmount); + } - uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); + /** + * @dev borrowers can user this function to swap between stable and variable borrow rate modes. + * @param asset the address of the reserve on which the user borrowed + * @param rateMode the rate mode that the user wants to swap + **/ + function swapBorrowRateMode(address asset, uint256 rateMode) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - // user must be borrowing on asset at a stable rate - require(stableBorrowBalance > 0, NOT_ENOUGH_STABLE_BORROW_BALANCE); + (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve); - uint256 rebalanceDownRateThreshold = reserve.currentStableBorrowRate.rayMul( - WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA) - ); + ReserveLogic.InterestRateMode interestRateMode = ReserveLogic.InterestRateMode(rateMode); - //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. + ValidationLogic.validateSwapRateMode( + reserve, + _usersConfig[msg.sender], + stableDebt, + variableDebt, + interestRateMode + ); - uint256 userStableRate = stableDebtToken.getUserStableRate(user); + reserve.updateCumulativeIndexesAndTimestamp(); - require( - userStableRate < reserve.currentLiquidityRate || - userStableRate > rebalanceDownRateThreshold, - INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET - ); - - //burn old debt tokens, mint new ones - - reserve.updateCumulativeIndexesAndTimestamp(); - - stableDebtToken.burn(user, stableBorrowBalance); - stableDebtToken.mint(user, stableBorrowBalance, reserve.currentStableBorrowRate); - - reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); - - emit RebalanceStableBorrowRate(asset, user); - - return; + if (interestRateMode == ReserveLogic.InterestRateMode.STABLE) { + //burn stable rate tokens, mint variable rate tokens + IStableDebtToken(reserve.stableDebtTokenAddress).burn(msg.sender, stableDebt); + IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, stableDebt); + } else { + //do the opposite + IVariableDebtToken(reserve.variableDebtTokenAddress).burn(msg.sender, variableDebt); + IStableDebtToken(reserve.stableDebtTokenAddress).mint( + msg.sender, + variableDebt, + reserve.currentStableBorrowRate + ); } - /** - * @dev allows depositors to enable or disable a specific deposit as collateral. - * @param asset the address of the reserve - * @param useAsCollateral true if the user wants to user the deposit as collateral, false otherwise. - **/ - function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) - external - override - nonReentrant - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); - ValidationLogic.validateSetUseReserveAsCollateral( - reserve, - asset, - _reserves, - _usersConfig[msg.sender], - _reservesList, - _addressesProvider.getPriceOracle() - ); + emit Swap( + asset, + msg.sender, + //solium-disable-next-line + block.timestamp + ); + } - _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, useAsCollateral); + /** + * @dev rebalances the stable interest rate of a user if current liquidity rate > user stable rate. + * this is regulated by Aave to ensure that the protocol is not abused, and the user is paying a fair + * rate. Anyone can call this function. + * @param asset the address of the reserve + * @param user the address of the user to be rebalanced + **/ + function rebalanceStableBorrowRate(address asset, address user) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - if (useAsCollateral) { - emit ReserveUsedAsCollateralEnabled(asset, msg.sender); - } else { - emit ReserveUsedAsCollateralDisabled(asset, msg.sender); - } + IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress); + + uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); + + // user must be borrowing on asset at a stable rate + require(stableBorrowBalance > 0, NOT_ENOUGH_STABLE_BORROW_BALANCE); + + uint256 rebalanceDownRateThreshold = reserve.currentStableBorrowRate.rayMul( + WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA) + ); + + //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); + + require( + userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold, + INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET + ); + + //burn old debt tokens, mint new ones + + reserve.updateCumulativeIndexesAndTimestamp(); + + stableDebtToken.burn(user, stableBorrowBalance); + stableDebtToken.mint(user, stableBorrowBalance, reserve.currentStableBorrowRate); + + reserve.updateInterestRates(asset, reserve.aTokenAddress, 0, 0); + + emit RebalanceStableBorrowRate(asset, user); + + return; + } + + /** + * @dev allows depositors to enable or disable a specific deposit as collateral. + * @param asset the address of the reserve + * @param useAsCollateral true if the user wants to user the deposit as collateral, false otherwise. + **/ + function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) + external + override + nonReentrant + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + + ValidationLogic.validateSetUseReserveAsCollateral( + reserve, + asset, + _reserves, + _usersConfig[msg.sender], + _reservesList, + _addressesProvider.getPriceOracle() + ); + + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, useAsCollateral); + + if (useAsCollateral) { + emit ReserveUsedAsCollateralEnabled(asset, msg.sender); + } else { + emit ReserveUsedAsCollateralDisabled(asset, msg.sender); } + } - /** - * @dev users can invoke this function to liquidate an undercollateralized position. - * @param asset the address of the collateral to liquidated - * @param asset the address of the principal reserve - * @param user the address of the borrower - * @param purchaseAmount the amount of principal that the liquidator wants to repay - * @param receiveAToken true if the liquidators wants to receive the aTokens, false if - * he wants to receive the underlying asset directly - **/ - function liquidationCall( - address collateral, - address asset, - address user, - uint256 purchaseAmount, - bool receiveAToken - ) external override nonReentrant { - address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager(); + /** + * @dev users can invoke this function to liquidate an undercollateralized position. + * @param asset the address of the collateral to liquidated + * @param asset the address of the principal reserve + * @param user the address of the borrower + * @param purchaseAmount the amount of principal that the liquidator wants to repay + * @param receiveAToken true if the liquidators wants to receive the aTokens, false if + * he wants to receive the underlying asset directly + **/ + function liquidationCall( + address collateral, + address asset, + address user, + uint256 purchaseAmount, + bool receiveAToken + ) external override nonReentrant { + address liquidationManager = _addressesProvider.getLendingPoolLiquidationManager(); - //solium-disable-next-line - (bool success, bytes memory result) = liquidationManager.delegatecall( - abi.encodeWithSignature( - 'liquidationCall(address,address,address,uint256,bool)', - collateral, - asset, - user, - purchaseAmount, - receiveAToken - ) - ); - require(success, LIQUIDATION_CALL_FAILED); + //solium-disable-next-line + (bool success, bytes memory result) = liquidationManager.delegatecall( + abi.encodeWithSignature( + 'liquidationCall(address,address,address,uint256,bool)', + collateral, + asset, + user, + purchaseAmount, + receiveAToken + ) + ); + require(success, LIQUIDATION_CALL_FAILED); - (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); + (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); - if (returnCode != 0) { - //error found - revert(string(abi.encodePacked(returnMessage))); - } + if (returnCode != 0) { + //error found + revert(string(abi.encodePacked(returnMessage))); } + } - /** - * @dev allows smartcontracts to access the liquidity of the pool within one transaction, - * as long as the amount taken plus a fee is returned. NOTE There are security concerns for developers of flashloan receiver contracts - * that must be kept into consideration. For further details please visit https://developers.aave.com - * @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. - * @param asset the address of the principal reserve - * @param amount the amount requested for this flashloan - **/ - function flashLoan( - address receiverAddress, - address asset, - uint256 amount, - bytes calldata params - ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + /** + * @dev allows smartcontracts to access the liquidity of the pool within one transaction, + * as long as the amount taken plus a fee is returned. NOTE There are security concerns for developers of flashloan receiver contracts + * that must be kept into consideration. For further details please visit https://developers.aave.com + * @param receiverAddress The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. + * @param asset the address of the principal reserve + * @param amount the amount requested for this flashloan + **/ + function flashLoan( + address receiverAddress, + address asset, + uint256 amount, + bytes calldata params + ) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - address aTokenAddress = reserve.aTokenAddress; + address aTokenAddress = reserve.aTokenAddress; - //check that the reserve has enough available liquidity - uint256 availableLiquidityBefore = IERC20(asset).balanceOf(aTokenAddress); + //check that the reserve has enough available liquidity + uint256 availableLiquidityBefore = IERC20(asset).balanceOf(aTokenAddress); - //calculate amount fee - uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); + //calculate amount fee + uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); - require(availableLiquidityBefore >= amount, NOT_ENOUGH_LIQUIDITY_TO_BORROW); - require(amountFee > 0, REQUESTED_AMOUNT_TO_SMALL); + require(availableLiquidityBefore >= amount, NOT_ENOUGH_LIQUIDITY_TO_BORROW); + require(amountFee > 0, REQUESTED_AMOUNT_TO_SMALL); - //get the FlashLoanReceiver instance - IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); + //get the FlashLoanReceiver instance + IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); - //transfer funds to the receiver - IAToken(aTokenAddress).transferUnderlyingTo(receiverAddress, amount); + //transfer funds to the receiver + IAToken(aTokenAddress).transferUnderlyingTo(receiverAddress, amount); - //execute action of the receiver - receiver.executeOperation(asset, aTokenAddress, amount, amountFee, params); + //execute action of the receiver + receiver.executeOperation(asset, aTokenAddress, amount, amountFee, params); - //check that the actual balance of the core contract includes the returned amount - uint256 availableLiquidityAfter = IERC20(asset).balanceOf(aTokenAddress); + //check that the actual balance of the core contract includes the returned amount + uint256 availableLiquidityAfter = IERC20(asset).balanceOf(aTokenAddress); - require( - availableLiquidityAfter == availableLiquidityBefore.add(amountFee), - INCONSISTENT_PROTOCOL_ACTUAL_BALANCE - ); + require( + availableLiquidityAfter == availableLiquidityBefore.add(amountFee), + INCONSISTENT_PROTOCOL_ACTUAL_BALANCE + ); - //compounding the cumulated interest - reserve.updateCumulativeIndexesAndTimestamp(); + //compounding the cumulated interest + reserve.updateCumulativeIndexesAndTimestamp(); - uint256 totalLiquidityBefore = availableLiquidityBefore - .add(IERC20(reserve.variableDebtTokenAddress).totalSupply()) - .add(IERC20(reserve.stableDebtTokenAddress).totalSupply()); + uint256 totalLiquidityBefore = availableLiquidityBefore + .add(IERC20(reserve.variableDebtTokenAddress).totalSupply()) + .add(IERC20(reserve.stableDebtTokenAddress).totalSupply()); - //compounding the received fee into the reserve - reserve.cumulateToLiquidityIndex(totalLiquidityBefore, amountFee); + //compounding the received fee into the reserve + reserve.cumulateToLiquidityIndex(totalLiquidityBefore, amountFee); - //refresh interest rates - reserve.updateInterestRates(asset, aTokenAddress, amountFee, 0); + //refresh interest rates + reserve.updateInterestRates(asset, aTokenAddress, amountFee, 0); - //solium-disable-next-line - emit FlashLoan(receiverAddress, asset, amount, amountFee); + //solium-disable-next-line + emit FlashLoan(receiverAddress, asset, amount, amountFee); + } + + /** + * @dev accessory functions to fetch data from the core contract + **/ + + function getReserveConfigurationData(address asset) + external + override + view + returns ( + uint256 decimals, + uint256 ltv, + uint256 liquidationThreshold, + uint256 liquidationBonus, + address interestRateStrategyAddress, + bool usageAsCollateralEnabled, + bool borrowingEnabled, + bool stableBorrowRateEnabled, + bool isActive, + bool isFreezed + ) + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + + return ( + reserve.configuration.getDecimals(), + reserve.configuration.getLtv(), + reserve.configuration.getLiquidationThreshold(), + reserve.configuration.getLiquidationBonus(), + reserve.interestRateStrategyAddress, + reserve.configuration.getLtv() != 0, + reserve.configuration.getBorrowingEnabled(), + reserve.configuration.getStableRateBorrowingEnabled(), + reserve.configuration.getActive(), + reserve.configuration.getFrozen() + ); + } + + function getReserveTokensAddresses(address asset) + external + override + view + returns ( + address aTokenAddress, + address stableDebtTokenAddress, + address variableDebtTokenAddress + ) + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + + return ( + reserve.aTokenAddress, + reserve.stableDebtTokenAddress, + reserve.variableDebtTokenAddress + ); + } + + function getReserveData(address asset) + external + override + view + returns ( + uint256 availableLiquidity, + uint256 totalBorrowsStable, + uint256 totalBorrowsVariable, + uint256 liquidityRate, + uint256 variableBorrowRate, + uint256 stableBorrowRate, + uint256 averageStableBorrowRate, + uint256 liquidityIndex, + uint256 variableBorrowIndex, + uint40 lastUpdateTimestamp + ) + { + ReserveLogic.ReserveData memory reserve = _reserves[asset]; + return ( + IERC20(asset).balanceOf(reserve.aTokenAddress), + IERC20(reserve.stableDebtTokenAddress).totalSupply(), + IERC20(reserve.variableDebtTokenAddress).totalSupply(), + reserve.currentLiquidityRate, + reserve.currentVariableBorrowRate, + reserve.currentStableBorrowRate, + IStableDebtToken(reserve.stableDebtTokenAddress).getAverageStableRate(), + reserve.lastLiquidityIndex, + reserve.lastVariableBorrowIndex, + reserve.lastUpdateTimestamp + ); + } + + function getUserAccountData(address user) + external + override + view + returns ( + uint256 totalCollateralETH, + uint256 totalBorrowsETH, + uint256 availableBorrowsETH, + uint256 currentLiquidationThreshold, + uint256 ltv, + uint256 healthFactor + ) + { + ( + totalCollateralETH, + totalBorrowsETH, + ltv, + currentLiquidationThreshold, + healthFactor + ) = GenericLogic.calculateUserAccountData( + user, + _reserves, + _usersConfig[user], + _reservesList, + _addressesProvider.getPriceOracle() + ); + + availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH( + totalCollateralETH, + totalBorrowsETH, + ltv + ); + } + + function getUserReserveData(address asset, address user) + external + override + view + returns ( + uint256 currentATokenBalance, + uint256 currentStableDebt, + uint256 currentVariableDebt, + uint256 principalStableDebt, + uint256 principalVariableDebt, + uint256 stableBorrowRate, + uint256 liquidityRate, + uint256 variableBorrowIndex, + uint40 stableRateLastUpdated, + bool usageAsCollateralEnabled + ) + { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + + currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(user); + (currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(user, reserve); + (principalStableDebt, principalVariableDebt) = Helpers.getUserPrincipalDebt(user, reserve); + liquidityRate = reserve.currentLiquidityRate; + stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(user); + stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated( + user + ); + usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.index); + variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex(user); + } + + function getReserves() external override view returns (address[] memory) { + return _reservesList; + } + + receive() external payable { + revert(); + } + + /** + * @dev initializes a reserve + * @param asset the address of the reserve + * @param aTokenAddress the address of the overlying aToken contract + * @param interestRateStrategyAddress the address of the interest rate strategy contract + **/ + function initReserve( + address asset, + address aTokenAddress, + address stableDebtAddress, + address variableDebtAddress, + address interestRateStrategyAddress + ) external override onlyLendingPoolConfigurator { + _reserves[asset].init( + aTokenAddress, + stableDebtAddress, + variableDebtAddress, + interestRateStrategyAddress + ); + _addReserveToList(asset); + } + + /** + * @dev updates the address of the interest rate strategy contract + * @param asset the address of the reserve + * @param rateStrategyAddress the address of the interest rate strategy contract + **/ + + function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress) + external + override + onlyLendingPoolConfigurator + { + _reserves[asset].interestRateStrategyAddress = rateStrategyAddress; + } + + function setConfiguration(address asset, uint256 configuration) + external + override + onlyLendingPoolConfigurator + { + _reserves[asset].configuration.data = configuration; + } + + function getConfiguration(address asset) + external + override + view + returns (ReserveConfiguration.Map memory) + { + return _reserves[asset].configuration; + } + + /** + * @notice internal functions + **/ + + /** + * @dev adds a reserve to the array of the _reserves address + **/ + function _addReserveToList(address asset) internal { + bool reserveAlreadyAdded = false; + for (uint256 i = 0; i < _reservesList.length; i++) + if (_reservesList[i] == asset) { + reserveAlreadyAdded = true; + } + if (!reserveAlreadyAdded) { + _reserves[asset].index = uint8(_reservesList.length); + _reservesList.push(asset); } + } - /** - * @dev accessory functions to fetch data from the core contract - **/ + /** + * @dev returns the normalized income per unit of asset + * @param asset the address of the reserve + * @return the reserve normalized income + */ + function getReserveNormalizedIncome(address asset) external override view returns (uint256) { + return _reserves[asset].getNormalizedIncome(); + } - function getReserveConfigurationData(address asset) - external - override - view - returns ( - uint256 decimals, - uint256 ltv, - uint256 liquidationThreshold, - uint256 liquidationBonus, - address interestRateStrategyAddress, - bool usageAsCollateralEnabled, - bool borrowingEnabled, - bool stableBorrowRateEnabled, - bool isActive, - bool isFreezed - ) - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + /** + * @dev returns the normalized variable debt per unit of asset + * @param asset the address of the reserve + * @return the reserve normalized debt + */ + function getReserveNormalizedVariableDebt(address asset) + external + override + view + returns (uint256) + { + return _reserves[asset].getNormalizedDebt(); + } - return ( - reserve.configuration.getDecimals(), - reserve.configuration.getLtv(), - reserve.configuration.getLiquidationThreshold(), - reserve.configuration.getLiquidationBonus(), - reserve.interestRateStrategyAddress, - reserve.configuration.getLtv() != 0, - reserve.configuration.getBorrowingEnabled(), - reserve.configuration.getStableRateBorrowingEnabled(), - reserve.configuration.getActive(), - reserve.configuration.getFrozen() - ); - } + /** + * @dev validate if a balance decrease for an asset is allowed + * @param asset the address of the reserve + * @param user the user related to the balance decrease + * @param amount the amount being transferred/redeemed + * @return true if the balance decrease can be allowed, false otherwise + */ + function balanceDecreaseAllowed( + address asset, + address user, + uint256 amount + ) external override view returns (bool) { + return + GenericLogic.balanceDecreaseAllowed( + asset, + user, + amount, + _reserves, + _usersConfig[user], + _reservesList, + _addressesProvider.getPriceOracle() + ); + } - function getReserveTokensAddresses(address asset) - external - override - view - returns ( - address aTokenAddress, - address stableDebtTokenAddress, - address variableDebtTokenAddress - ) - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; + /** + * @dev returns the list of the initialized reserves + **/ + function getReservesList() external view returns (address[] memory) { + return _reservesList; + } - return ( - reserve.aTokenAddress, - reserve.stableDebtTokenAddress, - reserve.variableDebtTokenAddress - ); - } - - function getReserveData(address asset) - external - override - view - returns ( - uint256 availableLiquidity, - uint256 totalBorrowsStable, - uint256 totalBorrowsVariable, - uint256 liquidityRate, - uint256 variableBorrowRate, - uint256 stableBorrowRate, - uint256 averageStableBorrowRate, - uint256 liquidityIndex, - uint256 variableBorrowIndex, - uint40 lastUpdateTimestamp - ) - { - ReserveLogic.ReserveData memory reserve = _reserves[asset]; - return ( - IERC20(asset).balanceOf(reserve.aTokenAddress), - IERC20(reserve.stableDebtTokenAddress).totalSupply(), - IERC20(reserve.variableDebtTokenAddress).totalSupply(), - reserve.currentLiquidityRate, - reserve.currentVariableBorrowRate, - reserve.currentStableBorrowRate, - IStableDebtToken(reserve.stableDebtTokenAddress).getAverageStableRate(), - reserve.lastLiquidityIndex, - reserve.lastVariableBorrowIndex, - reserve.lastUpdateTimestamp - ); - } - - function getUserAccountData(address user) - external - override - view - returns ( - uint256 totalCollateralETH, - uint256 totalBorrowsETH, - uint256 availableBorrowsETH, - uint256 currentLiquidationThreshold, - uint256 ltv, - uint256 healthFactor - ) - { - ( - totalCollateralETH, - totalBorrowsETH, - ltv, - currentLiquidationThreshold, - healthFactor - ) = GenericLogic.calculateUserAccountData( - user, - _reserves, - _usersConfig[user], - _reservesList, - _addressesProvider.getPriceOracle() - ); - - availableBorrowsETH = GenericLogic.calculateAvailableBorrowsETH( - totalCollateralETH, - totalBorrowsETH, - ltv - ); - } - - function getUserReserveData(address asset, address user) - external - override - view - returns ( - uint256 currentATokenBalance, - uint256 currentStableDebt, - uint256 currentVariableDebt, - uint256 principalStableDebt, - uint256 principalVariableDebt, - uint256 stableBorrowRate, - uint256 liquidityRate, - uint256 variableBorrowIndex, - uint40 stableRateLastUpdated, - bool usageAsCollateralEnabled - ) - { - ReserveLogic.ReserveData storage reserve = _reserves[asset]; - - currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(user); - (currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(user, reserve); - (principalStableDebt, principalVariableDebt) = Helpers.getUserPrincipalDebt(user, reserve); - liquidityRate = reserve.currentLiquidityRate; - stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(user); - stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated( - user - ); - usageAsCollateralEnabled = _usersConfig[user].isUsingAsCollateral(reserve.index); - variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex( - user - ); - } - - function getReserves() external override view returns (address[] memory) { - return _reservesList; - } - - receive() external payable { - revert(); - } - - /** - * @dev initializes a reserve - * @param asset the address of the reserve - * @param aTokenAddress the address of the overlying aToken contract - * @param interestRateStrategyAddress the address of the interest rate strategy contract - **/ - function initReserve( - address asset, - address aTokenAddress, - address stableDebtAddress, - address variableDebtAddress, - address interestRateStrategyAddress - ) external override onlyLendingPoolConfigurator { - _reserves[asset].init( - aTokenAddress, - stableDebtAddress, - variableDebtAddress, - interestRateStrategyAddress - ); - _addReserveToList(asset); - } - - /** - * @dev updates the address of the interest rate strategy contract - * @param asset the address of the reserve - * @param rateStrategyAddress the address of the interest rate strategy contract - **/ - - function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress) - external - override - onlyLendingPoolConfigurator - { - _reserves[asset].interestRateStrategyAddress = rateStrategyAddress; - } - - function setConfiguration(address asset, uint256 configuration) - external - override - onlyLendingPoolConfigurator - { - _reserves[asset].configuration.data = configuration; - } - - function getConfiguration(address asset) - external - override - view - returns (ReserveConfiguration.Map memory) - { - return _reserves[asset].configuration; - } - - /** - * @notice internal functions - **/ - - /** - * @dev adds a reserve to the array of the _reserves address - **/ - function _addReserveToList(address asset) internal { - bool reserveAlreadyAdded = false; - for (uint256 i = 0; i < _reservesList.length; i++) - if (_reservesList[i] == asset) { - reserveAlreadyAdded = true; - } - if (!reserveAlreadyAdded) { - _reserves[asset].index = uint8(_reservesList.length); - _reservesList.push(asset); - } - } - - /** - * @dev returns the normalized income per unit of asset - * @param asset the address of the reserve - * @return the reserve normalized income - */ - function getReserveNormalizedIncome(address asset) external override view returns (uint256) { - return _reserves[asset].getNormalizedIncome(); - } - - /** - * @dev returns the normalized variable debt per unit of asset - * @param asset the address of the reserve - * @return the reserve normalized debt - */ - function getReserveNormalizedVariableDebt(address asset) - external - override - view - returns (uint256) - { - return _reserves[asset].getNormalizedDebt(); - } - - /** - * @dev validate if a balance decrease for an asset is allowed - * @param asset the address of the reserve - * @param user the user related to the balance decrease - * @param amount the amount being transferred/redeemed - * @return true if the balance decrease can be allowed, false otherwise - */ - function balanceDecreaseAllowed( - address asset, - address user, - uint256 amount - ) external override view returns (bool) { - return - GenericLogic.balanceDecreaseAllowed( - asset, - user, - amount, - _reserves, - _usersConfig[user], - _reservesList, - _addressesProvider.getPriceOracle() - ); - } - - /** - * @dev returns the list of the initialized reserves - **/ - function getReservesList() external view returns (address[] memory) { - return _reservesList; - } - - /** - * @dev returns the addresses provider - **/ - function getAddressesProvider() external view returns (ILendingPoolAddressesProvider) { - return _addressesProvider; - } + /** + * @dev returns the addresses provider + **/ + function getAddressesProvider() external view returns (ILendingPoolAddressesProvider) { + return _addressesProvider; + } } diff --git a/contracts/lendingpool/LendingPoolConfigurator.sol b/contracts/lendingpool/LendingPoolConfigurator.sol index 5a2d732b..8ed719c7 100644 --- a/contracts/lendingpool/LendingPoolConfigurator.sol +++ b/contracts/lendingpool/LendingPoolConfigurator.sol @@ -25,6 +25,10 @@ contract LendingPoolConfigurator is VersionedInitializable { using SafeMath for uint256; using ReserveConfiguration for ReserveConfiguration.Map; + //require error messages + string private constant CALLER_NOT_LENDING_POOL_MANAGER = '1'; // 'The caller must be a lending pool manager' + string private constant RESERVE_LIQUIDITY_NOT_0 = '2'; // 'The liquidity of the reserve needs to be 0' + /** * @dev emitted when a reserve is initialized. * @param asset the address of the reserve @@ -178,7 +182,7 @@ contract LendingPoolConfigurator is VersionedInitializable { modifier onlyLendingPoolManager { require( addressesProvider.getLendingPoolManager() == msg.sender, - 'The caller must be a lending pool manager' + CALLER_NOT_LENDING_POOL_MANAGER ); _; } @@ -425,7 +429,7 @@ contract LendingPoolConfigurator is VersionedInitializable { ) = pool.getReserveData(asset); require( availableLiquidity == 0 && totalBorrowsStable == 0 && totalBorrowsVariable == 0, - 'The liquidity of the reserve needs to be 0' + RESERVE_LIQUIDITY_NOT_0 ); ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); From 5841e51439fd933dfb393fe2ca9ae85f2e98c711 Mon Sep 17 00:00:00 2001 From: pol <> Date: Tue, 25 Aug 2020 15:57:54 +0200 Subject: [PATCH 03/11] Updated require message errors with constant string numbers to reduce gas --- .../configuration/LendingPoolAddressesProviderRegistry.sol | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contracts/configuration/LendingPoolAddressesProviderRegistry.sol b/contracts/configuration/LendingPoolAddressesProviderRegistry.sol index 07d416f8..274ff33f 100644 --- a/contracts/configuration/LendingPoolAddressesProviderRegistry.sol +++ b/contracts/configuration/LendingPoolAddressesProviderRegistry.sol @@ -16,6 +16,9 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP mapping(address => uint256) addressesProviders; address[] addressesProvidersList; + //require error messages + string private constant PROVIDER_NOT_REGISTERED = '1'; // 'Provider is not registered' + /** * @dev returns if an addressesProvider is registered or not * @param provider the addresses provider @@ -63,7 +66,7 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP * @param provider the pool address to be unregistered **/ function unregisterAddressesProvider(address provider) external override onlyOwner { - require(addressesProviders[provider] > 0, 'Provider is not registered'); + require(addressesProviders[provider] > 0, PROVIDER_NOT_REGISTERED); addressesProviders[provider] = 0; emit AddressesProviderUnregistered(provider); } From dbcd78a098e8375444d05af1b85a4dff29a62d2e Mon Sep 17 00:00:00 2001 From: pol <> Date: Tue, 25 Aug 2020 17:27:37 +0200 Subject: [PATCH 04/11] Updated require message errors with constant string numbers to reduce gas --- contracts/libraries/logic/ReserveLogic.sol | 5 +- contracts/libraries/logic/ValidationLogic.sol | 98 +++++++++++-------- 2 files changed, 61 insertions(+), 42 deletions(-) diff --git a/contracts/libraries/logic/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol index 893ca5c5..208d9024 100644 --- a/contracts/libraries/logic/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -21,6 +21,9 @@ library ReserveLogic { using WadRayMath for uint256; using SafeERC20 for IERC20; + //require error messages + string private constant RESERVE_ALREADY_INITIALIZED = '1'; // 'Reserve has already been initialized' + /** * @dev Emitted when the state of a reserve is updated * @param reserve the address of the reserve @@ -180,7 +183,7 @@ library ReserveLogic { address variableDebtTokenAddress, address interestRateStrategyAddress ) external { - require(reserve.aTokenAddress == address(0), 'Reserve has already been initialized'); + require(reserve.aTokenAddress == address(0), RESERVE_ALREADY_INITIALIZED); if (reserve.lastLiquidityIndex == 0) { //if the reserve has not been initialized yet reserve.lastLiquidityIndex = WadRayMath.ray(); diff --git a/contracts/libraries/logic/ValidationLogic.sol b/contracts/libraries/logic/ValidationLogic.sol index abba4592..c262144d 100644 --- a/contracts/libraries/logic/ValidationLogic.sol +++ b/contracts/libraries/logic/ValidationLogic.sol @@ -27,20 +27,39 @@ library ValidationLogic { using ReserveConfiguration for ReserveConfiguration.Map; using UserConfiguration for UserConfiguration.Map; + //require error messages + string private constant AMOUNT_NOT_GREATER_THAN_0 = '1'; // 'Amount must be greater than 0' + string private constant NO_ACTIVE_RESERVE = '2'; // 'Action requires an active reserve' + string private constant NO_UNFREEZED_RESERVE = '3'; // 'Action requires an unfreezed reserve' + string private constant CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4'; // 'The current liquidity is not enough' + string private constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5'; // 'User cannot withdraw more than the available balance' + string private constant TRANSFER_NOT_ALLOWED = '6'; // 'Transfer cannot be allowed.' + string private constant BORROWING_NOT_ENABLED = '7'; // 'Borrowing is not enabled' + string private constant INVALID_INTERESTRATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected' + string private constant COLLATERAL_BALANCE_IS_0 = '9'; // 'The collateral balance is 0' + string private constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10'; // 'Health factor is lesser than the liquidation threshold' + string private constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '11'; // 'There is not enough collateral to cover a new borrow' + string private constant STABLE_BORROWING_NOT_ENABLED = '12'; // stable borrowing not enabled + string private constant CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13'; // collateral is (mostly) the same currency that is being borrowed + string private constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14'; // 'The requested amount is greater than the max loan size in stable rate mode + string private constant NO_DEBT_OF_SELECTED_TYPE = '15'; // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' + string private constant NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed' + string private constant NO_STABLE_RATE_LOAN_IN_RESERVE = '17'; // 'User does not have a stable rate loan in progress on this reserve' + string private constant NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18'; // 'User does not have a variable rate loan in progress on this reserve' + string private constant UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19'; // 'The underlying balance needs to be greater than 0' + string private constant DEPOSIT_ALREADY_IN_USE = '20'; // 'User deposit is already being used as collateral' + /** * @dev validates a deposit. * @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) internal view { (bool isActive, bool isFreezed, , ) = reserve.configuration.getFlags(); - require(amount > 0, 'Amount must be greater than 0'); - require(isActive, 'Action requires an active reserve'); - require(!isFreezed, 'Action requires an unfreezed reserve'); + require(amount > 0, AMOUNT_NOT_GREATER_THAN_0); + require(isActive, NO_ACTIVE_RESERVE); + require(!isFreezed, NO_UNFREEZED_RESERVE); } /** @@ -60,13 +79,13 @@ library ValidationLogic { address[] calldata reserves, address oracle ) external view { - require(amount > 0, 'Amount must be greater than 0'); + require(amount > 0, AMOUNT_NOT_GREATER_THAN_0); uint256 currentAvailableLiquidity = IERC20(reserveAddress).balanceOf(address(aTokenAddress)); - require(currentAvailableLiquidity >= amount, '4'); + require(currentAvailableLiquidity >= amount, CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH); - require(amount <= userBalance, 'User cannot withdraw more than the available balance'); + require(amount <= userBalance, NOT_ENOUGH_AVAILABLE_USER_BALANCE); require( GenericLogic.balanceDecreaseAllowed( @@ -78,7 +97,7 @@ library ValidationLogic { reserves, oracle ), - 'Transfer cannot be allowed.' + TRANSFER_NOT_ALLOWED ); } @@ -138,22 +157,22 @@ library ValidationLogic { vars.stableRateBorrowingEnabled ) = reserve.configuration.getFlags(); - require(vars.isActive, 'Action requires an active reserve'); - require(!vars.isFreezed, 'Action requires an unfreezed reserve'); + require(vars.isActive, NO_ACTIVE_RESERVE); + require(!vars.isFreezed, NO_UNFREEZED_RESERVE); - require(vars.borrowingEnabled, '5'); + require(vars.borrowingEnabled, BORROWING_NOT_ENABLED); //validate interest rate mode require( uint256(ReserveLogic.InterestRateMode.VARIABLE) == interestRateMode || uint256(ReserveLogic.InterestRateMode.STABLE) == interestRateMode, - 'Invalid interest rate mode selected' + INVALID_INTERESTRATE_MODE_SELECTED ); //check that the amount is available in the reserve vars.availableLiquidity = IERC20(reserveAddress).balanceOf(address(reserve.aTokenAddress)); - require(vars.availableLiquidity >= amount, '7'); + require(vars.availableLiquidity >= amount, CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH); ( vars.userCollateralBalanceETH, @@ -169,9 +188,12 @@ library ValidationLogic { oracle ); - require(vars.userCollateralBalanceETH > 0, 'The collateral balance is 0'); + require(vars.userCollateralBalanceETH > 0, COLLATERAL_BALANCE_IS_0); - require(vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD, '8'); + require( + vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD, + HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD + ); //add the current already borrowed amount to the amount requested to calculate the total collateral needed. vars.amountOfCollateralNeededETH = vars.userBorrowBalanceETH.add(amountInETH).percentDiv( @@ -180,7 +202,7 @@ library ValidationLogic { require( vars.amountOfCollateralNeededETH <= vars.userCollateralBalanceETH, - 'There is not enough collateral to cover a new borrow' + COLLATERAL_CANNOT_COVER_NEW_BORROW ); /** @@ -195,20 +217,20 @@ library ValidationLogic { if (vars.rateMode == ReserveLogic.InterestRateMode.STABLE) { //check if the borrow mode is stable and if stable rate borrowing is enabled on this reserve - require(vars.stableRateBorrowingEnabled, '11'); + require(vars.stableRateBorrowingEnabled, STABLE_BORROWING_NOT_ENABLED); require( !userConfig.isUsingAsCollateral(reserve.index) || reserve.configuration.getLtv() == 0 || amount > IERC20(reserve.aTokenAddress).balanceOf(msg.sender), - '12' + CALLATERAL_SAME_AS_BORROWING_CURRENCY ); //calculate the max available loan size in stable rate mode as a percentage of the //available liquidity uint256 maxLoanSizeStable = vars.availableLiquidity.percentMul(maxStableLoanPercent); - require(amount <= maxLoanSizeStable, '13'); + require(amount <= maxLoanSizeStable, AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE); } } @@ -230,21 +252,21 @@ library ValidationLogic { ) external view { bool isActive = reserve.configuration.getActive(); - require(isActive, 'Action requires an active reserve'); + require(isActive, NO_ACTIVE_RESERVE); - require(amountSent > 0, 'Amount must be greater than 0'); + require(amountSent > 0, AMOUNT_NOT_GREATER_THAN_0); require( (stableDebt > 0 && ReserveLogic.InterestRateMode(rateMode) == ReserveLogic.InterestRateMode.STABLE) || (variableDebt > 0 && ReserveLogic.InterestRateMode(rateMode) == ReserveLogic.InterestRateMode.VARIABLE), - '16' + NO_DEBT_OF_SELECTED_TYPE ); require( amountSent != uint256(-1) || msg.sender == onBehalfOf, - 'To repay on behalf of an user an explicit amount to repay is needed' + NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF ); } @@ -265,19 +287,13 @@ library ValidationLogic { ) external view { (bool isActive, bool isFreezed, , bool stableRateEnabled) = reserve.configuration.getFlags(); - require(isActive, 'Action requires an active reserve'); - require(!isFreezed, 'Action requires an unfreezed reserve'); + require(isActive, NO_ACTIVE_RESERVE); + require(!isFreezed, NO_UNFREEZED_RESERVE); if (currentRateMode == ReserveLogic.InterestRateMode.STABLE) { - require( - stableBorrowBalance > 0, - 'User does not have a stable rate loan in progress on this reserve' - ); + require(stableBorrowBalance > 0, NO_STABLE_RATE_LOAN_IN_RESERVE); } else if (currentRateMode == ReserveLogic.InterestRateMode.VARIABLE) { - require( - variableBorrowBalance > 0, - 'User does not have a variable rate loan in progress on this reserve' - ); + require(variableBorrowBalance > 0, NO_VARIABLE_RATE_LOAN_IN_RESERVE); /** * user wants to swap to stable, before swapping we need to ensure that * 1. stable borrow rate is enabled on the reserve @@ -285,17 +301,17 @@ library ValidationLogic { * more collateral than he is borrowing, artificially lowering * the interest rate, borrowing at variable, and switching to stable **/ - require(stableRateEnabled, '11'); + require(stableRateEnabled, STABLE_BORROWING_NOT_ENABLED); require( !userConfig.isUsingAsCollateral(reserve.index) || reserve.configuration.getLtv() == 0 || stableBorrowBalance.add(variableBorrowBalance) > IERC20(reserve.aTokenAddress).balanceOf(msg.sender), - '12' + CALLATERAL_SAME_AS_BORROWING_CURRENCY ); } else { - revert('Invalid interest rate mode selected'); + revert(INVALID_INTERESTRATE_MODE_SELECTED); } } @@ -318,7 +334,7 @@ library ValidationLogic { ) external view { uint256 underlyingBalance = IERC20(reserve.aTokenAddress).balanceOf(msg.sender); - require(underlyingBalance > 0, '22'); + require(underlyingBalance > 0, UNDERLYING_BALANCE_NOT_GREATER_THAN_0); require( GenericLogic.balanceDecreaseAllowed( @@ -330,7 +346,7 @@ library ValidationLogic { reserves, oracle ), - 'User deposit is already being used as collateral' + DEPOSIT_ALREADY_IN_USE ); } } From 7b4812c956a0ba68f3bedf02c47255be441c478b Mon Sep 17 00:00:00 2001 From: pol <> Date: Wed, 2 Sep 2020 15:48:38 +0200 Subject: [PATCH 05/11] Moved error messages to error lib --- contracts/lendingpool/LendingPool.sol | 9 +--- contracts/libraries/helpers/Errors.sol | 48 +++++++++++++++++++ contracts/libraries/logic/ValidationLogic.sol | 23 +-------- contracts/tokenization/AToken.sol | 24 +++++----- helpers/types.ts | 43 ++++++++++++++++- test/atoken-modifiers.spec.ts | 10 ++-- 6 files changed, 108 insertions(+), 49 deletions(-) create mode 100644 contracts/libraries/helpers/Errors.sol diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index be4b4d18..1bf63fce 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -11,6 +11,7 @@ import { import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; 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 {ReserveLogic} from '../libraries/logic/ReserveLogic.sol'; import {GenericLogic} from '../libraries/logic/GenericLogic.sol'; @@ -44,14 +45,6 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { uint256 public constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; uint256 public constant FLASHLOAN_FEE_TOTAL = 9; - //require error messages - string private constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '1'; // 'User does not have any stable rate loan for this reserve' - string private constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '2'; // 'Interest rate rebalance conditions were not met' - string private constant LIQUIDATION_CALL_FAILED = '3'; // 'Liquidation call failed' - string private constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '4'; // 'There is not enough liquidity available to borrow' - string private constant REQUESTED_AMOUNT_TO_SMALL = '5'; // 'The requested amount is too small for a FlashLoan.' - string private constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '6'; // 'The actual balance of the protocol is inconsistent' - ILendingPoolAddressesProvider internal _addressesProvider; mapping(address => ReserveLogic.ReserveData) internal _reserves; diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol new file mode 100644 index 00000000..9821add4 --- /dev/null +++ b/contracts/libraries/helpers/Errors.sol @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: agpl-3.0 +pragma solidity ^0.6.8; + +/** + * @title Errors library + * @author Aave + * @notice Implements error messages. + */ +library Errors { + // require error messages - ValidationLogic + string private constant AMOUNT_NOT_GREATER_THAN_0 = '1'; // 'Amount must be greater than 0' + string private constant NO_ACTIVE_RESERVE = '2'; // 'Action requires an active reserve' + string private constant NO_UNFREEZED_RESERVE = '3'; // 'Action requires an unfreezed reserve' + string private constant CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4'; // 'The current liquidity is not enough' + string private constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5'; // 'User cannot withdraw more than the available balance' + string private constant TRANSFER_NOT_ALLOWED = '6'; // 'Transfer cannot be allowed.' + string private constant BORROWING_NOT_ENABLED = '7'; // 'Borrowing is not enabled' + string private constant INVALID_INTERESTRATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected' + string private constant COLLATERAL_BALANCE_IS_0 = '9'; // 'The collateral balance is 0' + string private constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10'; // 'Health factor is lesser than the liquidation threshold' + string private constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '11'; // 'There is not enough collateral to cover a new borrow' + string private constant STABLE_BORROWING_NOT_ENABLED = '12'; // stable borrowing not enabled + string private constant CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13'; // collateral is (mostly) the same currency that is being borrowed + string private constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14'; // 'The requested amount is greater than the max loan size in stable rate mode + string private constant NO_DEBT_OF_SELECTED_TYPE = '15'; // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' + string private constant NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed' + string private constant NO_STABLE_RATE_LOAN_IN_RESERVE = '17'; // 'User does not have a stable rate loan in progress on this reserve' + string private constant NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18'; // 'User does not have a variable rate loan in progress on this reserve' + string private constant UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19'; // 'The underlying balance needs to be greater than 0' + string private constant DEPOSIT_ALREADY_IN_USE = '20'; // 'User deposit is already being used as collateral' + + // require error messages - LendingPool + string private constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '21'; // 'User does not have any stable rate loan for this reserve' + string private constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '22'; // 'Interest rate rebalance conditions were not met' + string private constant LIQUIDATION_CALL_FAILED = '23'; // 'Liquidation call failed' + string private constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24'; // 'There is not enough liquidity available to borrow' + string private constant REQUESTED_AMOUNT_TO_SMALL = '25'; // 'The requested amount is too small for a FlashLoan.' + string private constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26'; // 'The actual balance of the protocol is inconsistent' + + // require error messages - aToken + string private constant CALLER_MUST_BE_LENDING_POOL = '27'; // 'The caller of this function must be a lending pool' + string private constant TRANSFER_CANNOT_BE_ALLOWED = '28'; // 'Transfer cannot be allowed.' + string private constant NOT_ALLOWED_TO_REDIRECT_INTEREST = '29'; // 'Caller is not allowed to redirect the interest of the user' + string private constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30'; // 'User cannot give allowance to himself' + string private constant TRANSFER_AMOUNT_NOT_GT_0 = '31'; // 'Transferred amount needs to be greater than zero' + string private constant INTEREST_ALREADY_REDIRECTED = '32'; // 'Interest is already redirected to the user' + string private constant NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '33'; // 'Interest stream can only be redirected if there is a valid balance' +} diff --git a/contracts/libraries/logic/ValidationLogic.sol b/contracts/libraries/logic/ValidationLogic.sol index c262144d..c21a8831 100644 --- a/contracts/libraries/logic/ValidationLogic.sol +++ b/contracts/libraries/logic/ValidationLogic.sol @@ -12,6 +12,7 @@ import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; import {UserConfiguration} from '../configuration/UserConfiguration.sol'; import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; +import {Errors} from '../libraries/helpers/Errors.sol'; /** * @title ReserveLogic library @@ -27,28 +28,6 @@ library ValidationLogic { using ReserveConfiguration for ReserveConfiguration.Map; using UserConfiguration for UserConfiguration.Map; - //require error messages - string private constant AMOUNT_NOT_GREATER_THAN_0 = '1'; // 'Amount must be greater than 0' - string private constant NO_ACTIVE_RESERVE = '2'; // 'Action requires an active reserve' - string private constant NO_UNFREEZED_RESERVE = '3'; // 'Action requires an unfreezed reserve' - string private constant CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4'; // 'The current liquidity is not enough' - string private constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5'; // 'User cannot withdraw more than the available balance' - string private constant TRANSFER_NOT_ALLOWED = '6'; // 'Transfer cannot be allowed.' - string private constant BORROWING_NOT_ENABLED = '7'; // 'Borrowing is not enabled' - string private constant INVALID_INTERESTRATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected' - string private constant COLLATERAL_BALANCE_IS_0 = '9'; // 'The collateral balance is 0' - string private constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10'; // 'Health factor is lesser than the liquidation threshold' - string private constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '11'; // 'There is not enough collateral to cover a new borrow' - string private constant STABLE_BORROWING_NOT_ENABLED = '12'; // stable borrowing not enabled - string private constant CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13'; // collateral is (mostly) the same currency that is being borrowed - string private constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14'; // 'The requested amount is greater than the max loan size in stable rate mode - string private constant NO_DEBT_OF_SELECTED_TYPE = '15'; // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' - string private constant NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed' - string private constant NO_STABLE_RATE_LOAN_IN_RESERVE = '17'; // 'User does not have a stable rate loan in progress on this reserve' - string private constant NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18'; // 'User does not have a variable rate loan in progress on this reserve' - string private constant UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19'; // 'The underlying balance needs to be greater than 0' - string private constant DEPOSIT_ALREADY_IN_USE = '20'; // 'User deposit is already being used as collateral' - /** * @dev validates a deposit. * @param reserve the reserve state on which the user is depositing diff --git a/contracts/tokenization/AToken.sol b/contracts/tokenization/AToken.sol index 0deb6dc2..dc871640 100644 --- a/contracts/tokenization/AToken.sol +++ b/contracts/tokenization/AToken.sol @@ -4,6 +4,7 @@ pragma solidity ^0.6.8; import {ERC20} from './ERC20.sol'; import {LendingPool} from '../lendingpool/LendingPool.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol'; +import {Errors} from '../libraries/helpers/Errors'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import { VersionedInitializable @@ -34,12 +35,12 @@ contract AToken is VersionedInitializable, ERC20, IAToken { uint256 public constant ATOKEN_REVISION = 0x1; modifier onlyLendingPool { - require(msg.sender == address(_pool), 'The caller of this function must be a lending pool'); + require(msg.sender == address(_pool), CALLER_MUST_BE_LENDING_POOL); _; } modifier whenTransferAllowed(address from, uint256 amount) { - require(isTransferAllowed(from, amount), 'Transfer cannot be allowed.'); + require(isTransferAllowed(from, amount), TRANSFER_CANNOT_BE_ALLOWED); _; } @@ -100,7 +101,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { function redirectInterestStreamOf(address from, address to) external override { require( msg.sender == _interestRedirectionAllowances[from], - 'Caller is not allowed to redirect the interest of the user' + CALLER_NOT_ALLOWED_TO_REDIRECT_INTEREST ); _redirectInterestStream(from, to); } @@ -112,7 +113,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { * the allowance. **/ function allowInterestRedirectionTo(address to) external override { - require(to != msg.sender, 'User cannot give allowance to himself'); + require(to != msg.sender, CANNOT_GIVE_ALLOWANCE_TO_HIMSELF); _interestRedirectionAllowances[msg.sender] = to; emit InterestRedirectionAllowanceChanged(msg.sender, to); } @@ -434,15 +435,12 @@ contract AToken is VersionedInitializable, ERC20, IAToken { address to, uint256 value ) internal { - require(value > 0, 'Transferred amount needs to be greater than zero'); + require(value > 0, TRANSFER_AMOUNT_NOT_GT_0); //cumulate the balance of the sender - ( - , - uint256 fromBalance, - uint256 fromBalanceIncrease, - uint256 fromIndex - ) = _cumulateBalance(from); + (, uint256 fromBalance, uint256 fromBalanceIncrease, uint256 fromIndex) = _cumulateBalance( + from + ); //cumulate the balance of the receiver (, , uint256 toBalanceIncrease, uint256 toIndex) = _cumulateBalance(to); @@ -486,7 +484,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { function _redirectInterestStream(address from, address to) internal { address currentRedirectionAddress = _interestRedirectionAddresses[from]; - require(to != currentRedirectionAddress, 'Interest is already redirected to the user'); + require(to != currentRedirectionAddress, INTEREST_ALREADY_REDIRECTED); //accumulates the accrued interest to the principal ( @@ -496,7 +494,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { uint256 fromIndex ) = _cumulateBalance(from); - require(fromBalance > 0, 'Interest stream can only be redirected if there is a valid balance'); + require(fromBalance > 0, NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM); //if the user is already redirecting the interest to someone, before changing //the redirection address we substract the redirected balance of the previous diff --git a/helpers/types.ts b/helpers/types.ts index 106e5376..c008816d 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -44,9 +44,50 @@ export enum eContractid { } export enum ProtocolErrors { + // require error messages - ValidationLogic + AMOUNT_NOT_GREATER_THAN_0 = '1', // 'Amount must be greater than 0' + NO_ACTIVE_RESERVE = '2', // 'Action requires an active reserve' + NO_UNFREEZED_RESERVE = '3', // 'Action requires an unfreezed reserve' + CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4', // 'The current liquidity is not enough' + NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5', // 'User cannot withdraw more than the available balance' + TRANSFER_NOT_ALLOWED = '6', // 'Transfer cannot be allowed.' + BORROWING_NOT_ENABLED = '7', // 'Borrowing is not enabled' + INVALID_INTERESTRATE_MODE_SELECTED = '8', // 'Invalid interest rate mode selected' + COLLATERAL_BALANCE_IS_0 = '9', // 'The collateral balance is 0' + HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10', // 'Health factor is lesser than the liquidation threshold' + COLLATERAL_CANNOT_COVER_NEW_BORROW = '11', // 'There is not enough collateral to cover a new borrow' + STABLE_BORROWING_NOT_ENABLED = '12', // stable borrowing not enabled + CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13', // collateral is (mostly) the same currency that is being borrowed + AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14', // 'The requested amount is greater than the max loan size in stable rate mode + NO_DEBT_OF_SELECTED_TYPE = '15', // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' + NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16', // 'To repay on behalf of an user an explicit amount to repay is needed' + NO_STABLE_RATE_LOAN_IN_RESERVE = '17', // 'User does not have a stable rate loan in progress on this reserve' + NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18', // 'User does not have a variable rate loan in progress on this reserve' + UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19', // 'The underlying balance needs to be greater than 0' + DEPOSIT_ALREADY_IN_USE = '20', // 'User deposit is already being used as collateral' + + // require error messages - LendingPool + NOT_ENOUGH_STABLE_BORROW_BALANCE = '21', // 'User does not have any stable rate loan for this reserve' + INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '22', // 'Interest rate rebalance conditions were not met' + LIQUIDATION_CALL_FAILED = '23', // 'Liquidation call failed' + NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24', // 'There is not enough liquidity available to borrow' + REQUESTED_AMOUNT_TO_SMALL = '25', // 'The requested amount is too small for a FlashLoan.' + INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26', // 'The actual balance of the protocol is inconsistent' + + // require error messages - aToken + CALLER_MUST_BE_LENDING_POOL = '27', // 'The caller of this function must be a lending pool' + TRANSFER_CANNOT_BE_ALLOWED = '28', // 'Transfer cannot be allowed.' + NOT_ALLOWED_TO_REDIRECT_INTEREST = '29', // 'Caller is not allowed to redirect the interest of the user' + CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30', // 'User cannot give allowance to himself' + TRANSFER_AMOUNT_NOT_GT_0 = '31', // 'Transferred amount needs to be greater than zero' + INTEREST_ALREADY_REDIRECTED = '32', // 'Interest is already redirected to the user' + NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '33', // 'Interest stream can only be redirected if there is a valid balance' +} + +export enum OLD_ProtocolErrors { INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract', INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract', - INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', + // INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', => CALLER_MUST_BE_LENDING_POOL INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', INVALID_TO_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', diff --git a/test/atoken-modifiers.spec.ts b/test/atoken-modifiers.spec.ts index 97115c0c..406d910a 100644 --- a/test/atoken-modifiers.spec.ts +++ b/test/atoken-modifiers.spec.ts @@ -3,17 +3,17 @@ import {makeSuite, TestEnv} from './helpers/make-suite'; import {ProtocolErrors} from '../helpers/types'; makeSuite('AToken: Modifiers', (testEnv: TestEnv) => { - const {INVALID_POOL_CALLER_MSG_1} = ProtocolErrors; + const {CALLER_MUST_BE_LENDING_POOL} = ProtocolErrors; it('Tries to invoke mint not being the LendingPool', async () => { const {deployer, aDai} = testEnv; - await expect(aDai.mint(deployer.address, '1')).to.be.revertedWith(INVALID_POOL_CALLER_MSG_1); + await expect(aDai.mint(deployer.address, '1')).to.be.revertedWith(CALLER_MUST_BE_LENDING_POOL); }); it('Tries to invoke burn not being the LendingPool', async () => { const {deployer, aDai} = testEnv; await expect(aDai.burn(deployer.address, deployer.address, '1')).to.be.revertedWith( - INVALID_POOL_CALLER_MSG_1 + CALLER_MUST_BE_LENDING_POOL ); }); @@ -21,13 +21,13 @@ makeSuite('AToken: Modifiers', (testEnv: TestEnv) => { const {deployer, users, aDai} = testEnv; await expect( aDai.transferOnLiquidation(deployer.address, users[0].address, '1') - ).to.be.revertedWith(INVALID_POOL_CALLER_MSG_1); + ).to.be.revertedWith(CALLER_MUST_BE_LENDING_POOL); }); it('Tries to invoke transferUnderlyingTo not being the LendingPool', async () => { const {deployer, users, aDai} = testEnv; await expect(aDai.transferUnderlyingTo(deployer.address, '1')).to.be.revertedWith( - INVALID_POOL_CALLER_MSG_1 + CALLER_MUST_BE_LENDING_POOL ); }); }); From 6122826ef41bc82ba08280340bb4f440c7faddf1 Mon Sep 17 00:00:00 2001 From: pol <> Date: Wed, 2 Sep 2020 16:34:15 +0200 Subject: [PATCH 06/11] fixed getting error codes from error lib --- contracts/lendingpool/LendingPool.sol | 12 ++-- contracts/libraries/helpers/Errors.sol | 66 +++++++++---------- contracts/libraries/logic/ValidationLogic.sol | 64 +++++++++--------- contracts/tokenization/AToken.sol | 16 ++--- helpers/types.ts | 22 +++++++ test/stable-token.spec.ts | 6 +- test/variable-debt-token.spec.ts | 6 +- 7 files changed, 107 insertions(+), 85 deletions(-) diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 1bf63fce..01eaad25 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -345,7 +345,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(user); // user must be borrowing on asset at a stable rate - require(stableBorrowBalance > 0, NOT_ENOUGH_STABLE_BORROW_BALANCE); + require(stableBorrowBalance > 0, Errors.NOT_ENOUGH_STABLE_BORROW_BALANCE); uint256 rebalanceDownRateThreshold = reserve.currentStableBorrowRate.rayMul( WadRayMath.ray().add(REBALANCE_DOWN_RATE_DELTA) @@ -360,7 +360,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { require( userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold, - INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET + Errors.INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET ); //burn old debt tokens, mint new ones @@ -436,7 +436,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { receiveAToken ) ); - require(success, LIQUIDATION_CALL_FAILED); + require(success, Errors.LIQUIDATION_CALL_FAILED); (uint256 returnCode, string memory returnMessage) = abi.decode(result, (uint256, string)); @@ -470,8 +470,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { //calculate amount fee uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); - require(availableLiquidityBefore >= amount, NOT_ENOUGH_LIQUIDITY_TO_BORROW); - require(amountFee > 0, REQUESTED_AMOUNT_TO_SMALL); + require(availableLiquidityBefore >= amount, Errors.NOT_ENOUGH_LIQUIDITY_TO_BORROW); + require(amountFee > 0, Errors.REQUESTED_AMOUNT_TO_SMALL); //get the FlashLoanReceiver instance IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); @@ -487,7 +487,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { require( availableLiquidityAfter == availableLiquidityBefore.add(amountFee), - INCONSISTENT_PROTOCOL_ACTUAL_BALANCE + Errors.INCONSISTENT_PROTOCOL_ACTUAL_BALANCE ); //compounding the cumulated interest diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index 9821add4..7db32078 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -8,41 +8,41 @@ pragma solidity ^0.6.8; */ library Errors { // require error messages - ValidationLogic - string private constant AMOUNT_NOT_GREATER_THAN_0 = '1'; // 'Amount must be greater than 0' - string private constant NO_ACTIVE_RESERVE = '2'; // 'Action requires an active reserve' - string private constant NO_UNFREEZED_RESERVE = '3'; // 'Action requires an unfreezed reserve' - string private constant CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4'; // 'The current liquidity is not enough' - string private constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5'; // 'User cannot withdraw more than the available balance' - string private constant TRANSFER_NOT_ALLOWED = '6'; // 'Transfer cannot be allowed.' - string private constant BORROWING_NOT_ENABLED = '7'; // 'Borrowing is not enabled' - string private constant INVALID_INTERESTRATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected' - string private constant COLLATERAL_BALANCE_IS_0 = '9'; // 'The collateral balance is 0' - string private constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10'; // 'Health factor is lesser than the liquidation threshold' - string private constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '11'; // 'There is not enough collateral to cover a new borrow' - string private constant STABLE_BORROWING_NOT_ENABLED = '12'; // stable borrowing not enabled - string private constant CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13'; // collateral is (mostly) the same currency that is being borrowed - string private constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14'; // 'The requested amount is greater than the max loan size in stable rate mode - string private constant NO_DEBT_OF_SELECTED_TYPE = '15'; // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' - string private constant NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed' - string private constant NO_STABLE_RATE_LOAN_IN_RESERVE = '17'; // 'User does not have a stable rate loan in progress on this reserve' - string private constant NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18'; // 'User does not have a variable rate loan in progress on this reserve' - string private constant UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19'; // 'The underlying balance needs to be greater than 0' - string private constant DEPOSIT_ALREADY_IN_USE = '20'; // 'User deposit is already being used as collateral' + string public constant AMOUNT_NOT_GREATER_THAN_0 = '1'; // 'Amount must be greater than 0' + string public constant NO_ACTIVE_RESERVE = '2'; // 'Action requires an active reserve' + string public constant NO_UNFREEZED_RESERVE = '3'; // 'Action requires an unfreezed reserve' + string public constant CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH = '4'; // 'The current liquidity is not enough' + string public constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5'; // 'User cannot withdraw more than the available balance' + string public constant TRANSFER_NOT_ALLOWED = '6'; // 'Transfer cannot be allowed.' + string public constant BORROWING_NOT_ENABLED = '7'; // 'Borrowing is not enabled' + string public constant INVALID_INTERESTRATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected' + string public constant COLLATERAL_BALANCE_IS_0 = '9'; // 'The collateral balance is 0' + string public constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10'; // 'Health factor is lesser than the liquidation threshold' + string public constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '11'; // 'There is not enough collateral to cover a new borrow' + string public constant STABLE_BORROWING_NOT_ENABLED = '12'; // stable borrowing not enabled + string public constant CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13'; // collateral is (mostly) the same currency that is being borrowed + string public constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14'; // 'The requested amount is greater than the max loan size in stable rate mode + string public constant NO_DEBT_OF_SELECTED_TYPE = '15'; // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' + string public constant NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed' + string public constant NO_STABLE_RATE_LOAN_IN_RESERVE = '17'; // 'User does not have a stable rate loan in progress on this reserve' + string public constant NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18'; // 'User does not have a variable rate loan in progress on this reserve' + string public constant UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19'; // 'The underlying balance needs to be greater than 0' + string public constant DEPOSIT_ALREADY_IN_USE = '20'; // 'User deposit is already being used as collateral' // require error messages - LendingPool - string private constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '21'; // 'User does not have any stable rate loan for this reserve' - string private constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '22'; // 'Interest rate rebalance conditions were not met' - string private constant LIQUIDATION_CALL_FAILED = '23'; // 'Liquidation call failed' - string private constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24'; // 'There is not enough liquidity available to borrow' - string private constant REQUESTED_AMOUNT_TO_SMALL = '25'; // 'The requested amount is too small for a FlashLoan.' - string private constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26'; // 'The actual balance of the protocol is inconsistent' + string public constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '21'; // 'User does not have any stable rate loan for this reserve' + string public constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '22'; // 'Interest rate rebalance conditions were not met' + string public constant LIQUIDATION_CALL_FAILED = '23'; // 'Liquidation call failed' + string public constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24'; // 'There is not enough liquidity available to borrow' + string public constant REQUESTED_AMOUNT_TO_SMALL = '25'; // 'The requested amount is too small for a FlashLoan.' + string public constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26'; // 'The actual balance of the protocol is inconsistent' // require error messages - aToken - string private constant CALLER_MUST_BE_LENDING_POOL = '27'; // 'The caller of this function must be a lending pool' - string private constant TRANSFER_CANNOT_BE_ALLOWED = '28'; // 'Transfer cannot be allowed.' - string private constant NOT_ALLOWED_TO_REDIRECT_INTEREST = '29'; // 'Caller is not allowed to redirect the interest of the user' - string private constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30'; // 'User cannot give allowance to himself' - string private constant TRANSFER_AMOUNT_NOT_GT_0 = '31'; // 'Transferred amount needs to be greater than zero' - string private constant INTEREST_ALREADY_REDIRECTED = '32'; // 'Interest is already redirected to the user' - string private constant NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '33'; // 'Interest stream can only be redirected if there is a valid balance' + string public constant CALLER_MUST_BE_LENDING_POOL = '27'; // 'The caller of this function must be a lending pool' + string public constant TRANSFER_CANNOT_BE_ALLOWED = '28'; // 'Transfer cannot be allowed.' + string public constant NOT_ALLOWED_TO_REDIRECT_INTEREST = '29'; // 'Caller is not allowed to redirect the interest of the user' + string public constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30'; // 'User cannot give allowance to himself' + string public constant TRANSFER_AMOUNT_NOT_GT_0 = '31'; // 'Transferred amount needs to be greater than zero' + string public constant INTEREST_ALREADY_REDIRECTED = '32'; // 'Interest is already redirected to the user' + string public constant NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '33'; // 'Interest stream can only be redirected if there is a valid balance' } diff --git a/contracts/libraries/logic/ValidationLogic.sol b/contracts/libraries/logic/ValidationLogic.sol index c21a8831..09a916e5 100644 --- a/contracts/libraries/logic/ValidationLogic.sol +++ b/contracts/libraries/logic/ValidationLogic.sol @@ -12,7 +12,7 @@ import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; import {UserConfiguration} from '../configuration/UserConfiguration.sol'; import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; -import {Errors} from '../libraries/helpers/Errors.sol'; +import {Errors} from '../helpers/Errors.sol'; /** * @title ReserveLogic library @@ -36,9 +36,9 @@ library ValidationLogic { function validateDeposit(ReserveLogic.ReserveData storage reserve, uint256 amount) internal view { (bool isActive, bool isFreezed, , ) = reserve.configuration.getFlags(); - require(amount > 0, AMOUNT_NOT_GREATER_THAN_0); - require(isActive, NO_ACTIVE_RESERVE); - require(!isFreezed, NO_UNFREEZED_RESERVE); + require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0); + require(isActive, Errors.NO_ACTIVE_RESERVE); + require(!isFreezed, Errors.NO_UNFREEZED_RESERVE); } /** @@ -58,13 +58,13 @@ library ValidationLogic { address[] calldata reserves, address oracle ) external view { - require(amount > 0, AMOUNT_NOT_GREATER_THAN_0); + require(amount > 0, Errors.AMOUNT_NOT_GREATER_THAN_0); uint256 currentAvailableLiquidity = IERC20(reserveAddress).balanceOf(address(aTokenAddress)); - require(currentAvailableLiquidity >= amount, CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH); + require(currentAvailableLiquidity >= amount, Errors.CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH); - require(amount <= userBalance, NOT_ENOUGH_AVAILABLE_USER_BALANCE); + require(amount <= userBalance, Errors.NOT_ENOUGH_AVAILABLE_USER_BALANCE); require( GenericLogic.balanceDecreaseAllowed( @@ -76,7 +76,7 @@ library ValidationLogic { reserves, oracle ), - TRANSFER_NOT_ALLOWED + Errors.TRANSFER_NOT_ALLOWED ); } @@ -136,22 +136,22 @@ library ValidationLogic { vars.stableRateBorrowingEnabled ) = reserve.configuration.getFlags(); - require(vars.isActive, NO_ACTIVE_RESERVE); - require(!vars.isFreezed, NO_UNFREEZED_RESERVE); + require(vars.isActive, Errors.NO_ACTIVE_RESERVE); + require(!vars.isFreezed, Errors.NO_UNFREEZED_RESERVE); - require(vars.borrowingEnabled, BORROWING_NOT_ENABLED); + require(vars.borrowingEnabled, Errors.BORROWING_NOT_ENABLED); //validate interest rate mode require( uint256(ReserveLogic.InterestRateMode.VARIABLE) == interestRateMode || uint256(ReserveLogic.InterestRateMode.STABLE) == interestRateMode, - INVALID_INTERESTRATE_MODE_SELECTED + Errors.INVALID_INTERESTRATE_MODE_SELECTED ); //check that the amount is available in the reserve vars.availableLiquidity = IERC20(reserveAddress).balanceOf(address(reserve.aTokenAddress)); - require(vars.availableLiquidity >= amount, CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH); + require(vars.availableLiquidity >= amount, Errors.CURRENT_AVAILABLE_LIQUIDITY_NOT_ENOUGH); ( vars.userCollateralBalanceETH, @@ -167,11 +167,11 @@ library ValidationLogic { oracle ); - require(vars.userCollateralBalanceETH > 0, COLLATERAL_BALANCE_IS_0); + require(vars.userCollateralBalanceETH > 0, Errors.COLLATERAL_BALANCE_IS_0); require( vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD, - HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD + Errors.HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD ); //add the current already borrowed amount to the amount requested to calculate the total collateral needed. @@ -181,7 +181,7 @@ library ValidationLogic { require( vars.amountOfCollateralNeededETH <= vars.userCollateralBalanceETH, - COLLATERAL_CANNOT_COVER_NEW_BORROW + Errors.COLLATERAL_CANNOT_COVER_NEW_BORROW ); /** @@ -196,20 +196,20 @@ library ValidationLogic { if (vars.rateMode == ReserveLogic.InterestRateMode.STABLE) { //check if the borrow mode is stable and if stable rate borrowing is enabled on this reserve - require(vars.stableRateBorrowingEnabled, STABLE_BORROWING_NOT_ENABLED); + require(vars.stableRateBorrowingEnabled, Errors.STABLE_BORROWING_NOT_ENABLED); require( !userConfig.isUsingAsCollateral(reserve.index) || reserve.configuration.getLtv() == 0 || amount > IERC20(reserve.aTokenAddress).balanceOf(msg.sender), - CALLATERAL_SAME_AS_BORROWING_CURRENCY + Errors.CALLATERAL_SAME_AS_BORROWING_CURRENCY ); //calculate the max available loan size in stable rate mode as a percentage of the //available liquidity uint256 maxLoanSizeStable = vars.availableLiquidity.percentMul(maxStableLoanPercent); - require(amount <= maxLoanSizeStable, AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE); + require(amount <= maxLoanSizeStable, Errors.AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE); } } @@ -231,21 +231,21 @@ library ValidationLogic { ) external view { bool isActive = reserve.configuration.getActive(); - require(isActive, NO_ACTIVE_RESERVE); + require(isActive, Errors.NO_ACTIVE_RESERVE); - require(amountSent > 0, AMOUNT_NOT_GREATER_THAN_0); + require(amountSent > 0, Errors.AMOUNT_NOT_GREATER_THAN_0); require( (stableDebt > 0 && ReserveLogic.InterestRateMode(rateMode) == ReserveLogic.InterestRateMode.STABLE) || (variableDebt > 0 && ReserveLogic.InterestRateMode(rateMode) == ReserveLogic.InterestRateMode.VARIABLE), - NO_DEBT_OF_SELECTED_TYPE + Errors.NO_DEBT_OF_SELECTED_TYPE ); require( amountSent != uint256(-1) || msg.sender == onBehalfOf, - NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF + Errors.NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF ); } @@ -266,13 +266,13 @@ library ValidationLogic { ) external view { (bool isActive, bool isFreezed, , bool stableRateEnabled) = reserve.configuration.getFlags(); - require(isActive, NO_ACTIVE_RESERVE); - require(!isFreezed, NO_UNFREEZED_RESERVE); + require(isActive, Errors.NO_ACTIVE_RESERVE); + require(!isFreezed, Errors.NO_UNFREEZED_RESERVE); if (currentRateMode == ReserveLogic.InterestRateMode.STABLE) { - require(stableBorrowBalance > 0, NO_STABLE_RATE_LOAN_IN_RESERVE); + require(stableBorrowBalance > 0, Errors.NO_STABLE_RATE_LOAN_IN_RESERVE); } else if (currentRateMode == ReserveLogic.InterestRateMode.VARIABLE) { - require(variableBorrowBalance > 0, NO_VARIABLE_RATE_LOAN_IN_RESERVE); + require(variableBorrowBalance > 0, Errors.NO_VARIABLE_RATE_LOAN_IN_RESERVE); /** * user wants to swap to stable, before swapping we need to ensure that * 1. stable borrow rate is enabled on the reserve @@ -280,17 +280,17 @@ library ValidationLogic { * more collateral than he is borrowing, artificially lowering * the interest rate, borrowing at variable, and switching to stable **/ - require(stableRateEnabled, STABLE_BORROWING_NOT_ENABLED); + require(stableRateEnabled, Errors.STABLE_BORROWING_NOT_ENABLED); require( !userConfig.isUsingAsCollateral(reserve.index) || reserve.configuration.getLtv() == 0 || stableBorrowBalance.add(variableBorrowBalance) > IERC20(reserve.aTokenAddress).balanceOf(msg.sender), - CALLATERAL_SAME_AS_BORROWING_CURRENCY + Errors.CALLATERAL_SAME_AS_BORROWING_CURRENCY ); } else { - revert(INVALID_INTERESTRATE_MODE_SELECTED); + revert(Errors.INVALID_INTERESTRATE_MODE_SELECTED); } } @@ -313,7 +313,7 @@ library ValidationLogic { ) external view { uint256 underlyingBalance = IERC20(reserve.aTokenAddress).balanceOf(msg.sender); - require(underlyingBalance > 0, UNDERLYING_BALANCE_NOT_GREATER_THAN_0); + require(underlyingBalance > 0, Errors.UNDERLYING_BALANCE_NOT_GREATER_THAN_0); require( GenericLogic.balanceDecreaseAllowed( @@ -325,7 +325,7 @@ library ValidationLogic { reserves, oracle ), - DEPOSIT_ALREADY_IN_USE + Errors.DEPOSIT_ALREADY_IN_USE ); } } diff --git a/contracts/tokenization/AToken.sol b/contracts/tokenization/AToken.sol index dc871640..528e4997 100644 --- a/contracts/tokenization/AToken.sol +++ b/contracts/tokenization/AToken.sol @@ -4,7 +4,7 @@ pragma solidity ^0.6.8; import {ERC20} from './ERC20.sol'; import {LendingPool} from '../lendingpool/LendingPool.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol'; -import {Errors} from '../libraries/helpers/Errors'; +import {Errors} from '../libraries/helpers/Errors.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import { VersionedInitializable @@ -35,12 +35,12 @@ contract AToken is VersionedInitializable, ERC20, IAToken { uint256 public constant ATOKEN_REVISION = 0x1; modifier onlyLendingPool { - require(msg.sender == address(_pool), CALLER_MUST_BE_LENDING_POOL); + require(msg.sender == address(_pool), Errors.CALLER_MUST_BE_LENDING_POOL); _; } modifier whenTransferAllowed(address from, uint256 amount) { - require(isTransferAllowed(from, amount), TRANSFER_CANNOT_BE_ALLOWED); + require(isTransferAllowed(from, amount), Errors.TRANSFER_CANNOT_BE_ALLOWED); _; } @@ -101,7 +101,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { function redirectInterestStreamOf(address from, address to) external override { require( msg.sender == _interestRedirectionAllowances[from], - CALLER_NOT_ALLOWED_TO_REDIRECT_INTEREST + Errors.NOT_ALLOWED_TO_REDIRECT_INTEREST ); _redirectInterestStream(from, to); } @@ -113,7 +113,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { * the allowance. **/ function allowInterestRedirectionTo(address to) external override { - require(to != msg.sender, CANNOT_GIVE_ALLOWANCE_TO_HIMSELF); + require(to != msg.sender, Errors.CANNOT_GIVE_ALLOWANCE_TO_HIMSELF); _interestRedirectionAllowances[msg.sender] = to; emit InterestRedirectionAllowanceChanged(msg.sender, to); } @@ -435,7 +435,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { address to, uint256 value ) internal { - require(value > 0, TRANSFER_AMOUNT_NOT_GT_0); + require(value > 0, Errors.TRANSFER_AMOUNT_NOT_GT_0); //cumulate the balance of the sender (, uint256 fromBalance, uint256 fromBalanceIncrease, uint256 fromIndex) = _cumulateBalance( @@ -484,7 +484,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { function _redirectInterestStream(address from, address to) internal { address currentRedirectionAddress = _interestRedirectionAddresses[from]; - require(to != currentRedirectionAddress, INTEREST_ALREADY_REDIRECTED); + require(to != currentRedirectionAddress, Errors.INTEREST_ALREADY_REDIRECTED); //accumulates the accrued interest to the principal ( @@ -494,7 +494,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { uint256 fromIndex ) = _cumulateBalance(from); - require(fromBalance > 0, NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM); + require(fromBalance > 0, Errors.NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM); //if the user is already redirecting the interest to someone, before changing //the redirection address we substract the redirected balance of the previous diff --git a/helpers/types.ts b/helpers/types.ts index c008816d..478b81cd 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -82,6 +82,28 @@ export enum ProtocolErrors { TRANSFER_AMOUNT_NOT_GT_0 = '31', // 'Transferred amount needs to be greater than zero' INTEREST_ALREADY_REDIRECTED = '32', // 'Interest is already redirected to the user' NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '33', // 'Interest stream can only be redirected if there is a valid balance' + + // old + + INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract', + INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract', + // INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', => CALLER_MUST_BE_LENDING_POOL + INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', + INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', + INVALID_TO_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', + INVALID_OWNER_REVERT_MSG = 'Ownable: caller is not the owner', + INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer', + INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer', + INVALID_REDIRECTION_ADDRESS = 'Invalid redirection address', + TRANSFERRED_AMOUNT_GT_ZERO = 'Transferred amount needs to be greater than zero', + ZERO_COLLATERAL = 'The collateral balance is 0', + INCONSISTENT_PROTOCOL_BALANCE = 'The actual balance of the protocol is inconsistent', + TOO_SMALL_FLASH_LOAN = 'The requested amount is too small for a FlashLoan.', + // NOT_ENOUGH_LIQUIDITY_TO_BORROW = 'There is not enough liquidity available to borrow', + HF_IS_NOT_BELLOW_THRESHOLD = 'Health factor is not below the threshold', + INVALID_HF = 'Invalid health factor', + USER_DID_NOT_BORROW_SPECIFIED = 'User did not borrow the specified currency', + THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED = 'The collateral chosen cannot be liquidated', } export enum OLD_ProtocolErrors { diff --git a/test/stable-token.spec.ts b/test/stable-token.spec.ts index e6c25573..1d6adcdb 100644 --- a/test/stable-token.spec.ts +++ b/test/stable-token.spec.ts @@ -5,7 +5,7 @@ import {getContract} from '../helpers/contracts-helpers'; import {StableDebtToken} from '../types/StableDebtToken'; makeSuite('Stable debt token tests', (testEnv: TestEnv) => { - const {INVALID_POOL_CALLER_MSG_1} = ProtocolErrors; + const {CALLER_MUST_BE_LENDING_POOL} = ProtocolErrors; it('Tries to invoke mint not being the LendingPool', async () => { const {deployer, pool, dai} = testEnv; @@ -19,7 +19,7 @@ makeSuite('Stable debt token tests', (testEnv: TestEnv) => { ); await expect(stableDebtContract.mint(deployer.address, '1', '1')).to.be.revertedWith( - INVALID_POOL_CALLER_MSG_1 + CALLER_MUST_BE_LENDING_POOL ); }); @@ -35,7 +35,7 @@ makeSuite('Stable debt token tests', (testEnv: TestEnv) => { ); await expect(stableDebtContract.burn(deployer.address, '1')).to.be.revertedWith( - INVALID_POOL_CALLER_MSG_1 + CALLER_MUST_BE_LENDING_POOL ); }); }); diff --git a/test/variable-debt-token.spec.ts b/test/variable-debt-token.spec.ts index 28b35849..89bb1acc 100644 --- a/test/variable-debt-token.spec.ts +++ b/test/variable-debt-token.spec.ts @@ -5,7 +5,7 @@ import {getContract} from '../helpers/contracts-helpers'; import {VariableDebtToken} from '../types/VariableDebtToken'; makeSuite('Variable debt token tests', (testEnv: TestEnv) => { - const {INVALID_POOL_CALLER_MSG_1} = ProtocolErrors; + const {CALLER_MUST_BE_LENDING_POOL} = ProtocolErrors; it('Tries to invoke mint not being the LendingPool', async () => { const {deployer, pool, dai} = testEnv; @@ -19,7 +19,7 @@ makeSuite('Variable debt token tests', (testEnv: TestEnv) => { ); await expect(variableDebtContract.mint(deployer.address, '1')).to.be.revertedWith( - INVALID_POOL_CALLER_MSG_1 + CALLER_MUST_BE_LENDING_POOL ); }); @@ -35,7 +35,7 @@ makeSuite('Variable debt token tests', (testEnv: TestEnv) => { ); await expect(variableDebtContract.burn(deployer.address, '1')).to.be.revertedWith( - INVALID_POOL_CALLER_MSG_1 + CALLER_MUST_BE_LENDING_POOL ); }); }); From 76b4fc6b2dcd1b60d0491aa08fe731203c846f1b Mon Sep 17 00:00:00 2001 From: pol <> Date: Wed, 2 Sep 2020 17:54:34 +0200 Subject: [PATCH 07/11] All tests working. WIP look at old error messages to remove them all --- .../LendingPoolAddressesProviderRegistry.sol | 6 +- .../lendingpool/LendingPoolConfigurator.sol | 9 +-- contracts/libraries/helpers/Errors.sol | 21 ++++-- contracts/libraries/logic/ReserveLogic.sol | 6 +- contracts/tokenization/AToken.sol | 2 +- contracts/tokenization/base/DebtTokenBase.sol | 3 +- helpers/types.ts | 37 +++++++---- test/atoken-transfer.spec.ts | 28 ++++---- test/configurator.spec.ts | 66 +++++++++---------- test/flashloan.spec.ts | 14 ++-- test/upgradeability.spec.ts | 60 +++++++++-------- 11 files changed, 136 insertions(+), 116 deletions(-) diff --git a/contracts/configuration/LendingPoolAddressesProviderRegistry.sol b/contracts/configuration/LendingPoolAddressesProviderRegistry.sol index 274ff33f..ee0caf16 100644 --- a/contracts/configuration/LendingPoolAddressesProviderRegistry.sol +++ b/contracts/configuration/LendingPoolAddressesProviderRegistry.sol @@ -5,6 +5,7 @@ import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol'; import { ILendingPoolAddressesProviderRegistry } from '../interfaces/ILendingPoolAddressesProviderRegistry.sol'; +import {Errors} from '../libraries/helpers/Errors.sol'; /** * @title LendingPoolAddressesProviderRegistry contract @@ -16,9 +17,6 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP mapping(address => uint256) addressesProviders; address[] addressesProvidersList; - //require error messages - string private constant PROVIDER_NOT_REGISTERED = '1'; // 'Provider is not registered' - /** * @dev returns if an addressesProvider is registered or not * @param provider the addresses provider @@ -66,7 +64,7 @@ contract LendingPoolAddressesProviderRegistry is Ownable, ILendingPoolAddressesP * @param provider the pool address to be unregistered **/ function unregisterAddressesProvider(address provider) external override onlyOwner { - require(addressesProviders[provider] > 0, PROVIDER_NOT_REGISTERED); + require(addressesProviders[provider] > 0, Errors.PROVIDER_NOT_REGISTERED); addressesProviders[provider] = 0; emit AddressesProviderUnregistered(provider); } diff --git a/contracts/lendingpool/LendingPoolConfigurator.sol b/contracts/lendingpool/LendingPoolConfigurator.sol index 8ed719c7..e9c0a292 100644 --- a/contracts/lendingpool/LendingPoolConfigurator.sol +++ b/contracts/lendingpool/LendingPoolConfigurator.sol @@ -13,6 +13,7 @@ import {ReserveConfiguration} from '../libraries/configuration/ReserveConfigurat import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol'; import {IERC20Detailed} from '../interfaces/IERC20Detailed.sol'; +import {Errors} from '../libraries/helpers/Errors.sol'; /** * @title LendingPoolConfigurator contract @@ -25,10 +26,6 @@ contract LendingPoolConfigurator is VersionedInitializable { using SafeMath for uint256; using ReserveConfiguration for ReserveConfiguration.Map; - //require error messages - string private constant CALLER_NOT_LENDING_POOL_MANAGER = '1'; // 'The caller must be a lending pool manager' - string private constant RESERVE_LIQUIDITY_NOT_0 = '2'; // 'The liquidity of the reserve needs to be 0' - /** * @dev emitted when a reserve is initialized. * @param asset the address of the reserve @@ -182,7 +179,7 @@ contract LendingPoolConfigurator is VersionedInitializable { modifier onlyLendingPoolManager { require( addressesProvider.getLendingPoolManager() == msg.sender, - CALLER_NOT_LENDING_POOL_MANAGER + Errors.CALLER_NOT_LENDING_POOL_MANAGER ); _; } @@ -429,7 +426,7 @@ contract LendingPoolConfigurator is VersionedInitializable { ) = pool.getReserveData(asset); require( availableLiquidity == 0 && totalBorrowsStable == 0 && totalBorrowsVariable == 0, - RESERVE_LIQUIDITY_NOT_0 + Errors.RESERVE_LIQUIDITY_NOT_0 ); ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index 7db32078..388ea88f 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -39,10 +39,19 @@ library Errors { // require error messages - aToken string public constant CALLER_MUST_BE_LENDING_POOL = '27'; // 'The caller of this function must be a lending pool' - string public constant TRANSFER_CANNOT_BE_ALLOWED = '28'; // 'Transfer cannot be allowed.' - string public constant NOT_ALLOWED_TO_REDIRECT_INTEREST = '29'; // 'Caller is not allowed to redirect the interest of the user' - string public constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30'; // 'User cannot give allowance to himself' - string public constant TRANSFER_AMOUNT_NOT_GT_0 = '31'; // 'Transferred amount needs to be greater than zero' - string public constant INTEREST_ALREADY_REDIRECTED = '32'; // 'Interest is already redirected to the user' - string public constant NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '33'; // 'Interest stream can only be redirected if there is a valid balance' + string public constant NOT_ALLOWED_TO_REDIRECT_INTEREST = '28'; // 'Caller is not allowed to redirect the interest of the user' + string public constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '29'; // 'User cannot give allowance to himself' + string public constant TRANSFER_AMOUNT_NOT_GT_0 = '30'; // 'Transferred amount needs to be greater than zero' + string public constant INTEREST_ALREADY_REDIRECTED = '31'; // 'Interest is already redirected to the user' + string public constant NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '32'; // 'Interest stream can only be redirected if there is a valid balance' + + // require error messages - ReserveLogic + string public constant RESERVE_ALREADY_INITIALIZED = '33'; // 'Reserve has already been initialized' + + //require error messages - LendingPoolConfiguration + string public constant CALLER_NOT_LENDING_POOL_MANAGER = '34'; // 'The caller must be a lending pool manager' + string public constant RESERVE_LIQUIDITY_NOT_0 = '35'; // 'The liquidity of the reserve needs to be 0' + + //require error messages - LendingPoolAddressesProviderRegistry + string public constant PROVIDER_NOT_REGISTERED = '36'; // 'Provider is not registered' } diff --git a/contracts/libraries/logic/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol index 208d9024..c4871668 100644 --- a/contracts/libraries/logic/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -10,6 +10,7 @@ import {IStableDebtToken} from '../../tokenization/interfaces/IStableDebtToken.s import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; import {IReserveInterestRateStrategy} from '../../interfaces/IReserveInterestRateStrategy.sol'; import {WadRayMath} from '../math/WadRayMath.sol'; +import {Errors} from '../helpers/Errors.sol'; /** * @title ReserveLogic library @@ -21,9 +22,6 @@ library ReserveLogic { using WadRayMath for uint256; using SafeERC20 for IERC20; - //require error messages - string private constant RESERVE_ALREADY_INITIALIZED = '1'; // 'Reserve has already been initialized' - /** * @dev Emitted when the state of a reserve is updated * @param reserve the address of the reserve @@ -183,7 +181,7 @@ library ReserveLogic { address variableDebtTokenAddress, address interestRateStrategyAddress ) external { - require(reserve.aTokenAddress == address(0), RESERVE_ALREADY_INITIALIZED); + require(reserve.aTokenAddress == address(0), Errors.RESERVE_ALREADY_INITIALIZED); if (reserve.lastLiquidityIndex == 0) { //if the reserve has not been initialized yet reserve.lastLiquidityIndex = WadRayMath.ray(); diff --git a/contracts/tokenization/AToken.sol b/contracts/tokenization/AToken.sol index 528e4997..2c6e8541 100644 --- a/contracts/tokenization/AToken.sol +++ b/contracts/tokenization/AToken.sol @@ -40,7 +40,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { } modifier whenTransferAllowed(address from, uint256 amount) { - require(isTransferAllowed(from, amount), Errors.TRANSFER_CANNOT_BE_ALLOWED); + require(isTransferAllowed(from, amount), Errors.TRANSFER_NOT_ALLOWED); _; } diff --git a/contracts/tokenization/base/DebtTokenBase.sol b/contracts/tokenization/base/DebtTokenBase.sol index 87b83e0e..33d7222d 100644 --- a/contracts/tokenization/base/DebtTokenBase.sol +++ b/contracts/tokenization/base/DebtTokenBase.sol @@ -9,6 +9,7 @@ import { VersionedInitializable } from '../../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; import {IERC20Detailed} from '../../interfaces/IERC20Detailed.sol'; +import {Errors} from '../../libraries/helpers/Errors.sol'; /** * @title contract DebtTokenBase @@ -33,7 +34,7 @@ abstract contract DebtTokenBase is IERC20Detailed, VersionedInitializable { * @dev only lending pool can call functions marked by this modifier **/ modifier onlyLendingPool { - require(msg.sender == address(_pool), 'The caller of this function must be a lending pool'); + require(msg.sender == address(_pool), Errors.CALLER_MUST_BE_LENDING_POOL); _; } diff --git a/helpers/types.ts b/helpers/types.ts index 478b81cd..3d6aca55 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -76,30 +76,39 @@ export enum ProtocolErrors { // require error messages - aToken CALLER_MUST_BE_LENDING_POOL = '27', // 'The caller of this function must be a lending pool' - TRANSFER_CANNOT_BE_ALLOWED = '28', // 'Transfer cannot be allowed.' - NOT_ALLOWED_TO_REDIRECT_INTEREST = '29', // 'Caller is not allowed to redirect the interest of the user' - CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30', // 'User cannot give allowance to himself' - TRANSFER_AMOUNT_NOT_GT_0 = '31', // 'Transferred amount needs to be greater than zero' - INTEREST_ALREADY_REDIRECTED = '32', // 'Interest is already redirected to the user' - NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '33', // 'Interest stream can only be redirected if there is a valid balance' + NOT_ALLOWED_TO_REDIRECT_INTEREST = '28', // 'Caller is not allowed to redirect the interest of the user' + CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '29', // 'User cannot give allowance to himself' + TRANSFER_AMOUNT_NOT_GT_0 = '30', // 'Transferred amount needs to be greater than zero' + INTEREST_ALREADY_REDIRECTED = '31', // 'Interest is already redirected to the user' + NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '32', // 'Interest stream can only be redirected if there is a valid balance' + + // require error messages - ReserveLogic + RESERVE_ALREADY_INITIALIZED = '33', // 'Reserve has already been initialized' + + //require error messages - LendingPoolConfiguration + CALLER_NOT_LENDING_POOL_MANAGER = '34', // 'The caller must be a lending pool manager' + RESERVE_LIQUIDITY_NOT_0 = '35', // 'The liquidity of the reserve needs to be 0' + + //require error messages - LendingPoolAddressesProviderRegistry + PROVIDER_NOT_REGISTERED = '36', // 'Provider is not registered' // old INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract', INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract', - // INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', => CALLER_MUST_BE_LENDING_POOL - INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', + // INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', => 27 + // INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', => 34 INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', INVALID_TO_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', INVALID_OWNER_REVERT_MSG = 'Ownable: caller is not the owner', INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer', INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer', INVALID_REDIRECTION_ADDRESS = 'Invalid redirection address', - TRANSFERRED_AMOUNT_GT_ZERO = 'Transferred amount needs to be greater than zero', - ZERO_COLLATERAL = 'The collateral balance is 0', - INCONSISTENT_PROTOCOL_BALANCE = 'The actual balance of the protocol is inconsistent', - TOO_SMALL_FLASH_LOAN = 'The requested amount is too small for a FlashLoan.', - // NOT_ENOUGH_LIQUIDITY_TO_BORROW = 'There is not enough liquidity available to borrow', + // TRANSFERRED_AMOUNT_GT_ZERO = 'Transferred amount needs to be greater than zero', => 30 + // ZERO_COLLATERAL = 'The collateral balance is 0', + // INCONSISTENT_PROTOCOL_BALANCE = 'The actual balance of the protocol is inconsistent', => 26 + // TOO_SMALL_FLASH_LOAN = 'The requested amount is too small for a FlashLoan.', => 25 + // NOT_ENOUGH_LIQUIDITY_TO_BORROW = 'There is not enough liquidity available to borrow', => 24 HF_IS_NOT_BELLOW_THRESHOLD = 'Health factor is not below the threshold', INVALID_HF = 'Invalid health factor', USER_DID_NOT_BORROW_SPECIFIED = 'User did not borrow the specified currency', @@ -109,7 +118,7 @@ export enum ProtocolErrors { export enum OLD_ProtocolErrors { INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract', INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract', - // INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', => CALLER_MUST_BE_LENDING_POOL + INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', INVALID_TO_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', diff --git a/test/atoken-transfer.spec.ts b/test/atoken-transfer.spec.ts index 138eda61..1c161608 100644 --- a/test/atoken-transfer.spec.ts +++ b/test/atoken-transfer.spec.ts @@ -17,8 +17,10 @@ makeSuite('AToken: Transfer', (testEnv: TestEnv) => { INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER, INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER, INVALID_REDIRECTION_ADDRESS, - ZERO_COLLATERAL, - TRANSFERRED_AMOUNT_GT_ZERO, + // ZERO_COLLATERAL, + TRANSFER_AMOUNT_NOT_GT_0, + COLLATERAL_BALANCE_IS_0, + TRANSFER_NOT_ALLOWED, } = ProtocolErrors; it('User 0 deposits 1000 DAI, transfers to user 1', async () => { @@ -96,16 +98,14 @@ makeSuite('AToken: Transfer', (testEnv: TestEnv) => { await weth.connect(users[0].signer).mint(await convertToCurrencyDecimals(weth.address, '1')); await weth.connect(users[0].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL); - - await pool - .connect(users[0].signer) - .deposit(weth.address, ethers.utils.parseEther('1.0'), '0'); + + await pool.connect(users[0].signer).deposit(weth.address, ethers.utils.parseEther('1.0'), '0'); await expect( pool .connect(users[1].signer) .borrow(weth.address, ethers.utils.parseEther('0.1'), RateMode.Stable, AAVE_REFERRAL), - ZERO_COLLATERAL - ).to.be.revertedWith(ZERO_COLLATERAL); + COLLATERAL_BALANCE_IS_0 + ).to.be.revertedWith(COLLATERAL_BALANCE_IS_0); }); it('User 1 sets the DAI as collateral and borrows, tries to transfer everything back to user 0 (revert expected)', async () => { @@ -120,25 +120,25 @@ makeSuite('AToken: Transfer', (testEnv: TestEnv) => { await expect( aDai.connect(users[1].signer).transfer(users[0].address, aDAItoTransfer), - 'Transfer cannot be allowed.' - ).to.be.revertedWith('Transfer cannot be allowed.'); + TRANSFER_NOT_ALLOWED + ).to.be.revertedWith(TRANSFER_NOT_ALLOWED); }); it('User 0 tries to transfer 0 balance (revert expected)', async () => { const {users, pool, aDai, dai, weth} = testEnv; await expect( aDai.connect(users[0].signer).transfer(users[1].address, '0'), - TRANSFERRED_AMOUNT_GT_ZERO - ).to.be.revertedWith(TRANSFERRED_AMOUNT_GT_ZERO); + TRANSFER_AMOUNT_NOT_GT_0 + ).to.be.revertedWith(TRANSFER_AMOUNT_NOT_GT_0); }); it('User 1 repays the borrow, transfers aDAI back to user 0', async () => { const {users, pool, aDai, dai, weth} = testEnv; - + await weth.connect(users[1].signer).mint(await convertToCurrencyDecimals(weth.address, '2')); await weth.connect(users[1].signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL); - + await pool .connect(users[1].signer) .repay(weth.address, MAX_UINT_AMOUNT, RateMode.Stable, users[1].address); diff --git a/test/configurator.spec.ts b/test/configurator.spec.ts index 182f2f2b..a6513e54 100644 --- a/test/configurator.spec.ts +++ b/test/configurator.spec.ts @@ -6,7 +6,7 @@ import {ProtocolErrors} from '../helpers/types'; const {expect} = require('chai'); makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { - const {INVALID_POOL_MANAGER_CALLER_MSG} = ProtocolErrors; + const {CALLER_NOT_LENDING_POOL_MANAGER, RESERVE_LIQUIDITY_NOT_0} = ProtocolErrors; it('Deactivates the ETH reserve', async () => { const {configurator, pool, weth} = testEnv; @@ -27,16 +27,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).deactivateReserve(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Check the onlyLendingPoolManager on activateReserve ', async () => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).activateReserve(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Freezes the ETH reserve', async () => { @@ -58,16 +58,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).freezeReserve(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Check the onlyLendingPoolManager on unfreezeReserve ', async () => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).unfreezeReserve(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Deactivates the ETH reserve for borrowing', async () => { @@ -90,16 +90,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).disableBorrowingOnReserve(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Check the onlyLendingPoolManager on enableBorrowingOnReserve ', async () => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).enableBorrowingOnReserve(weth.address, true), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Deactivates the ETH reserve as collateral', async () => { @@ -121,8 +121,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).disableReserveAsCollateral(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Check the onlyLendingPoolManager on enableReserveAsCollateral ', async () => { @@ -131,8 +131,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { configurator .connect(users[2].signer) .enableReserveAsCollateral(weth.address, '75', '80', '105'), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Disable stable borrow rate on the ETH reserve', async () => { @@ -153,16 +153,16 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).disableReserveStableRate(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Check the onlyLendingPoolManager on enableReserveStableRate', async () => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).enableReserveStableRate(weth.address), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Changes LTV of the reserve', async () => { @@ -176,8 +176,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).setLtv(weth.address, '75'), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Changes liquidation threshold of the reserve', async () => { @@ -194,8 +194,8 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).setLiquidationThreshold(weth.address, '80'), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Changes liquidation bonus of the reserve', async () => { @@ -212,24 +212,24 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Check the onlyLendingPoolManager on setReserveDecimals', async () => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).setReserveDecimals(weth.address, '80'), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Check the onlyLendingPoolManager on setLiquidationBonus', async () => { const {configurator, users, weth} = testEnv; await expect( configurator.connect(users[2].signer).setLiquidationBonus(weth.address, '80'), - INVALID_POOL_MANAGER_CALLER_MSG - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + CALLER_NOT_LENDING_POOL_MANAGER + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Reverts when trying to disable the DAI reserve with liquidity on it', async () => { @@ -246,7 +246,7 @@ makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => { await expect( configurator.deactivateReserve(dai.address), - 'The liquidity of the reserve needs to be 0' - ).to.be.revertedWith('The liquidity of the reserve needs to be 0'); + RESERVE_LIQUIDITY_NOT_0 + ).to.be.revertedWith(RESERVE_LIQUIDITY_NOT_0); }); }); diff --git a/test/flashloan.spec.ts b/test/flashloan.spec.ts index fcda8c28..dfd7be16 100644 --- a/test/flashloan.spec.ts +++ b/test/flashloan.spec.ts @@ -11,8 +11,8 @@ const {expect} = require('chai'); makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { let _mockFlashLoanReceiver = {} as MockFlashLoanReceiver; const { - INCONSISTENT_PROTOCOL_BALANCE, - TOO_SMALL_FLASH_LOAN, + INCONSISTENT_PROTOCOL_ACTUAL_BALANCE, + REQUESTED_AMOUNT_TO_SMALL, NOT_ENOUGH_LIQUIDITY_TO_BORROW, } = ProtocolErrors; @@ -62,7 +62,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { const reserveDataBefore = await pool.getReserveData(weth.address); - console.log("Total liquidity is ", reserveDataBefore.availableLiquidity.toString()); + console.log('Total liquidity is ', reserveDataBefore.availableLiquidity.toString()); const txResult = await pool.flashLoan( _mockFlashLoanReceiver.address, @@ -99,7 +99,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { ethers.utils.parseEther('0.8'), '0x10' ) - ).to.be.revertedWith(INCONSISTENT_PROTOCOL_BALANCE); + ).to.be.revertedWith(INCONSISTENT_PROTOCOL_ACTUAL_BALANCE); }); it('tries to take a very small flashloan, which would result in 0 fees (revert expected)', async () => { @@ -112,7 +112,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { '1', //1 wei loan '0x10' ) - ).to.be.revertedWith(TOO_SMALL_FLASH_LOAN); + ).to.be.revertedWith(REQUESTED_AMOUNT_TO_SMALL); }); it('tries to take a flashloan that is bigger than the available liquidity (revert expected)', async () => { @@ -194,7 +194,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { ethers.utils.parseEther('500'), '0x10' ), - INCONSISTENT_PROTOCOL_BALANCE - ).to.be.revertedWith(INCONSISTENT_PROTOCOL_BALANCE); + INCONSISTENT_PROTOCOL_ACTUAL_BALANCE + ).to.be.revertedWith(INCONSISTENT_PROTOCOL_ACTUAL_BALANCE); }); }); diff --git a/test/upgradeability.spec.ts b/test/upgradeability.spec.ts index 41fc60ea..b2f50179 100644 --- a/test/upgradeability.spec.ts +++ b/test/upgradeability.spec.ts @@ -1,18 +1,22 @@ import {expect} from 'chai'; import {makeSuite, TestEnv} from './helpers/make-suite'; import {ProtocolErrors, eContractid} from '../helpers/types'; -import {deployGenericAToken, getAToken, deployContract, getContract} from '../helpers/contracts-helpers'; +import { + deployGenericAToken, + getAToken, + deployContract, + getContract, +} from '../helpers/contracts-helpers'; import {MockAToken} from '../types/MockAToken'; -import { MockStableDebtToken } from '../types/MockStableDebtToken'; -import { MockVariableDebtToken } from '../types/MockVariableDebtToken'; +import {MockStableDebtToken} from '../types/MockStableDebtToken'; +import {MockVariableDebtToken} from '../types/MockVariableDebtToken'; makeSuite('Upgradeability', (testEnv: TestEnv) => { - const {INVALID_POOL_MANAGER_CALLER_MSG} = ProtocolErrors; + const {CALLER_NOT_LENDING_POOL_MANAGER} = ProtocolErrors; let newATokenAddress: string; let newStableTokenAddress: string; let newVariableTokenAddress: string; - before('deploying instances', async () => { const {dai, pool} = testEnv; const aTokenInstance = await deployContract(eContractid.MockAToken, [ @@ -22,24 +26,19 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => { 'aDAI', ]); - const stableDebtTokenInstance = await deployContract(eContractid.MockStableDebtToken, [ - pool.address, - dai.address, - 'Aave stable debt bearing DAI updated', - 'stableDebtDAI', - ]); + const stableDebtTokenInstance = await deployContract( + eContractid.MockStableDebtToken, + [pool.address, dai.address, 'Aave stable debt bearing DAI updated', 'stableDebtDAI'] + ); - const variableDebtTokenInstance = await deployContract(eContractid.MockVariableDebtToken, [ - pool.address, - dai.address, - 'Aave variable debt bearing DAI updated', - 'variableDebtDAI', - ]); + const variableDebtTokenInstance = await deployContract( + eContractid.MockVariableDebtToken, + [pool.address, dai.address, 'Aave variable debt bearing DAI updated', 'variableDebtDAI'] + ); newATokenAddress = aTokenInstance.address; newVariableTokenAddress = variableDebtTokenInstance.address; newStableTokenAddress = stableDebtTokenInstance.address; - }); it('Tries to update the DAI Atoken implementation with a different address than the lendingPoolManager', async () => { @@ -47,7 +46,7 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => { await expect( configurator.connect(users[1].signer).updateAToken(dai.address, newATokenAddress) - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Upgrades the DAI Atoken implementation ', async () => { @@ -66,8 +65,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => { const {dai, configurator, users} = testEnv; await expect( - configurator.connect(users[1].signer).updateStableDebtToken(dai.address, newStableTokenAddress) - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + configurator + .connect(users[1].signer) + .updateStableDebtToken(dai.address, newStableTokenAddress) + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Upgrades the DAI stable debt token implementation ', async () => { @@ -79,7 +80,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => { const {stableDebtTokenAddress} = await pool.getReserveTokensAddresses(dai.address); - const debtToken = await getContract(eContractid.MockStableDebtToken, stableDebtTokenAddress); + const debtToken = await getContract( + eContractid.MockStableDebtToken, + stableDebtTokenAddress + ); const tokenName = await debtToken.name(); @@ -90,8 +94,10 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => { const {dai, configurator, users} = testEnv; await expect( - configurator.connect(users[1].signer).updateVariableDebtToken(dai.address, newVariableTokenAddress) - ).to.be.revertedWith(INVALID_POOL_MANAGER_CALLER_MSG); + configurator + .connect(users[1].signer) + .updateVariableDebtToken(dai.address, newVariableTokenAddress) + ).to.be.revertedWith(CALLER_NOT_LENDING_POOL_MANAGER); }); it('Upgrades the DAI variable debt token implementation ', async () => { @@ -103,11 +109,13 @@ makeSuite('Upgradeability', (testEnv: TestEnv) => { const {variableDebtTokenAddress} = await pool.getReserveTokensAddresses(dai.address); - const debtToken = await getContract(eContractid.MockStableDebtToken, variableDebtTokenAddress); + const debtToken = await getContract( + eContractid.MockStableDebtToken, + variableDebtTokenAddress + ); const tokenName = await debtToken.name(); expect(tokenName).to.be.eq('Aave variable debt bearing DAI updated', 'Invalid token name'); }); - }); From 5b5f8ae74a1a4283f2faa448dd23b90737c36d01 Mon Sep 17 00:00:00 2001 From: pol <> Date: Wed, 2 Sep 2020 18:18:17 +0200 Subject: [PATCH 08/11] cleaned comments --- helpers/types.ts | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/helpers/types.ts b/helpers/types.ts index 3d6aca55..713b9510 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -94,43 +94,12 @@ export enum ProtocolErrors { // old - INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract', - INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract', - // INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', => 27 - // INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', => 34 INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', INVALID_TO_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', INVALID_OWNER_REVERT_MSG = 'Ownable: caller is not the owner', INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer', INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer', INVALID_REDIRECTION_ADDRESS = 'Invalid redirection address', - // TRANSFERRED_AMOUNT_GT_ZERO = 'Transferred amount needs to be greater than zero', => 30 - // ZERO_COLLATERAL = 'The collateral balance is 0', - // INCONSISTENT_PROTOCOL_BALANCE = 'The actual balance of the protocol is inconsistent', => 26 - // TOO_SMALL_FLASH_LOAN = 'The requested amount is too small for a FlashLoan.', => 25 - // NOT_ENOUGH_LIQUIDITY_TO_BORROW = 'There is not enough liquidity available to borrow', => 24 - HF_IS_NOT_BELLOW_THRESHOLD = 'Health factor is not below the threshold', - INVALID_HF = 'Invalid health factor', - USER_DID_NOT_BORROW_SPECIFIED = 'User did not borrow the specified currency', - THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED = 'The collateral chosen cannot be liquidated', -} - -export enum OLD_ProtocolErrors { - INVALID_CONFIGURATOR_CALLER_MSG = 'The caller must be a lending pool configurator contract', - INVALID_POOL_CALLER_MSG = 'The caller must be a lending pool contract', - INVALID_POOL_CALLER_MSG_1 = 'The caller of this function must be a lending pool', - INVALID_POOL_MANAGER_CALLER_MSG = 'The caller must be a lending pool manager', - INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', - INVALID_TO_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', - INVALID_OWNER_REVERT_MSG = 'Ownable: caller is not the owner', - INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer', - INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer', - INVALID_REDIRECTION_ADDRESS = 'Invalid redirection address', - TRANSFERRED_AMOUNT_GT_ZERO = 'Transferred amount needs to be greater than zero', - ZERO_COLLATERAL = 'The collateral balance is 0', - INCONSISTENT_PROTOCOL_BALANCE = 'The actual balance of the protocol is inconsistent', - TOO_SMALL_FLASH_LOAN = 'The requested amount is too small for a FlashLoan.', - NOT_ENOUGH_LIQUIDITY_TO_BORROW = 'There is not enough liquidity available to borrow', HF_IS_NOT_BELLOW_THRESHOLD = 'Health factor is not below the threshold', INVALID_HF = 'Invalid health factor', USER_DID_NOT_BORROW_SPECIFIED = 'User did not borrow the specified currency', From 288d8f288973c25da696f83f0c5296fb5541e953 Mon Sep 17 00:00:00 2001 From: pol <> Date: Wed, 2 Sep 2020 18:53:39 +0200 Subject: [PATCH 09/11] Added LendingPoolLiquidationManager error messages to error lib, and updated tests. --- .../lendingpool/LendingPoolLiquidationManager.sol | 11 ++++++----- contracts/libraries/helpers/Errors.sol | 7 +++++++ helpers/types.ts | 10 +++++++--- test/liquidation-atoken.spec.ts | 12 ++++++------ test/liquidation-underlying.spec.ts | 11 +++-------- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/contracts/lendingpool/LendingPoolLiquidationManager.sol b/contracts/lendingpool/LendingPoolLiquidationManager.sol index a36ed3f3..27a29277 100644 --- a/contracts/lendingpool/LendingPoolLiquidationManager.sol +++ b/contracts/lendingpool/LendingPoolLiquidationManager.sol @@ -21,6 +21,7 @@ import {Helpers} from '../libraries/helpers/Helpers.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {PercentageMath} from '../libraries/math/PercentageMath.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; +import {Errors} from '../libraries/helpers/Errors.sol'; /** * @title LendingPoolLiquidationManager contract @@ -132,7 +133,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl if (vars.healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) { return ( uint256(LiquidationErrors.HEALTH_FACTOR_ABOVE_THRESHOLD), - 'Health factor is not below the threshold' + Errors.HEALTH_FACTOR_NOT_BELLOW_THRESHOLD ); } @@ -148,7 +149,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl if (!vars.isCollateralEnabled) { return ( uint256(LiquidationErrors.COLLATERAL_CANNOT_BE_LIQUIDATED), - 'The collateral chosen cannot be liquidated' + Errors.COLLATERAL_CANNOT_BE_LIQUIDATED ); } @@ -161,7 +162,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl if (vars.userStableDebt == 0 && vars.userVariableDebt == 0) { return ( uint256(LiquidationErrors.CURRRENCY_NOT_BORROWED), - 'User did not borrow the specified currency' + Errors.SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER ); } @@ -202,7 +203,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl if (currentAvailableCollateral < vars.maxCollateralToLiquidate) { return ( uint256(LiquidationErrors.NOT_ENOUGH_LIQUIDITY), - "There isn't enough liquidity available to liquidate" + Errors.NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE ); } } @@ -268,7 +269,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl receiveAToken ); - return (uint256(LiquidationErrors.NO_ERROR), 'No errors'); + return (uint256(LiquidationErrors.NO_ERROR), Errors.NO_ERRORS); } struct AvailableCollateralToLiquidateLocalVars { diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index 388ea88f..1b3f6980 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -54,4 +54,11 @@ library Errors { //require error messages - LendingPoolAddressesProviderRegistry string public constant PROVIDER_NOT_REGISTERED = '36'; // 'Provider is not registered' + + //return error messages - LendingPoolLiquidationManager + string public constant HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '37'; // 'Health factor is not below the threshold' + string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '38'; // 'The collateral chosen cannot be liquidated' + string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '39'; // 'User did not borrow the specified currency' + string public constant NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '40'; // "There isn't enough liquidity available to liquidate" + string public constant NO_ERRORS = '41'; // 'No errors' } diff --git a/helpers/types.ts b/helpers/types.ts index 713b9510..e98e5a3c 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -92,6 +92,13 @@ export enum ProtocolErrors { //require error messages - LendingPoolAddressesProviderRegistry PROVIDER_NOT_REGISTERED = '36', // 'Provider is not registered' + //return error messages - LendingPoolLiquidationManager + HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '37', // 'Health factor is not below the threshold' + COLLATERAL_CANNOT_BE_LIQUIDATED = '38', // 'The collateral chosen cannot be liquidated' + SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '39', // 'User did not borrow the specified currency' + NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '40', // "There isn't enough liquidity available to liquidate" + NO_ERRORS = '41', // 'No errors' + // old INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer', @@ -100,10 +107,7 @@ export enum ProtocolErrors { INVALID_REDIRECTED_BALANCE_BEFORE_TRANSFER = 'Invalid redirected balance before transfer', INVALID_REDIRECTED_BALANCE_AFTER_TRANSFER = 'Invalid redirected balance after transfer', INVALID_REDIRECTION_ADDRESS = 'Invalid redirection address', - HF_IS_NOT_BELLOW_THRESHOLD = 'Health factor is not below the threshold', INVALID_HF = 'Invalid health factor', - USER_DID_NOT_BORROW_SPECIFIED = 'User did not borrow the specified currency', - THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED = 'The collateral chosen cannot be liquidated', } export type tEthereumAddress = string; diff --git a/test/liquidation-atoken.spec.ts b/test/liquidation-atoken.spec.ts index 6e4af1e1..155b7c25 100644 --- a/test/liquidation-atoken.spec.ts +++ b/test/liquidation-atoken.spec.ts @@ -13,10 +13,10 @@ const {expect} = chai; makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => { const { - HF_IS_NOT_BELLOW_THRESHOLD, + HEALTH_FACTOR_NOT_BELLOW_THRESHOLD, INVALID_HF, - USER_DID_NOT_BORROW_SPECIFIED, - THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED, + SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER, + COLLATERAL_CANNOT_BE_LIQUIDATED, } = ProtocolErrors; it('LIQUIDATION - Deposits WETH, borrows DAI/Check liquidation fails because health factor is above 1', async () => { @@ -71,7 +71,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => //someone tries to liquidate user 2 await expect( pool.liquidationCall(weth.address, dai.address, borrower.address, 1, true) - ).to.be.revertedWith(HF_IS_NOT_BELLOW_THRESHOLD); + ).to.be.revertedWith(HEALTH_FACTOR_NOT_BELLOW_THRESHOLD); }); it('LIQUIDATION - Drop the health factor below 1', async () => { @@ -96,7 +96,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => //user 2 tries to borrow await expect( pool.liquidationCall(weth.address, weth.address, borrower.address, oneEther.toString(), true) - ).revertedWith(USER_DID_NOT_BORROW_SPECIFIED); + ).revertedWith(SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER); }); it('LIQUIDATION - Tries to liquidate a different collateral than the borrower collateral', async () => { @@ -105,7 +105,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => await expect( pool.liquidationCall(dai.address, dai.address, borrower.address, oneEther.toString(), true) - ).revertedWith(THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED); + ).revertedWith(COLLATERAL_CANNOT_BE_LIQUIDATED); }); it('LIQUIDATION - Liquidates the borrow', async () => { diff --git a/test/liquidation-underlying.spec.ts b/test/liquidation-underlying.spec.ts index 676c9c26..064e3856 100644 --- a/test/liquidation-underlying.spec.ts +++ b/test/liquidation-underlying.spec.ts @@ -13,12 +13,7 @@ const chai = require('chai'); const {expect} = chai; makeSuite('LendingPool liquidation - liquidator receiving the underlying asset', (testEnv) => { - const { - HF_IS_NOT_BELLOW_THRESHOLD, - INVALID_HF, - USER_DID_NOT_BORROW_SPECIFIED, - THE_COLLATERAL_CHOSEN_CANNOT_BE_LIQUIDATED, - } = ProtocolErrors; + const {INVALID_HF} = ProtocolErrors; it('LIQUIDATION - Deposits WETH, borrows DAI', async () => { const {dai, weth, users, pool, oracle} = testEnv; @@ -67,7 +62,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset', expect(userGlobalDataAfter.currentLiquidationThreshold.toString()).to.be.bignumber.equal( '8000', - 'Invalid liquidation threshold' + INVALID_HF ); }); @@ -86,7 +81,7 @@ makeSuite('LendingPool liquidation - liquidator receiving the underlying asset', expect(userGlobalData.healthFactor.toString()).to.be.bignumber.lt( oneEther.toFixed(0), - 'Invalid health factor' + INVALID_HF ); }); From 714c2ff3fdcf510a4d266d630198d4eccf6e3351 Mon Sep 17 00:00:00 2001 From: pol <> Date: Thu, 3 Sep 2020 10:33:15 +0200 Subject: [PATCH 10/11] Refactored as per the PR comments --- contracts/lendingpool/LendingPool.sol | 9 +++-- contracts/libraries/helpers/Errors.sol | 39 ++++++++++--------- contracts/libraries/logic/ValidationLogic.sol | 6 +-- contracts/tokenization/AToken.sol | 4 +- helpers/types.ts | 39 ++++++++++--------- test/flashloan.spec.ts | 4 +- 6 files changed, 53 insertions(+), 48 deletions(-) diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 01eaad25..e38ee13a 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -56,7 +56,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { * @dev only lending pools configurator can use functions affected by this modifier **/ modifier onlyLendingPoolConfigurator { - require(_addressesProvider.getLendingPoolConfigurator() == msg.sender, '30'); + require( + _addressesProvider.getLendingPoolConfigurator() == msg.sender, + Errors.CALLER_NOT_LENDING_POOL_CONFIGURATOR + ); _; } @@ -360,7 +363,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { require( userStableRate < reserve.currentLiquidityRate || userStableRate > rebalanceDownRateThreshold, - Errors.INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET + Errors.INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET ); //burn old debt tokens, mint new ones @@ -471,7 +474,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); require(availableLiquidityBefore >= amount, Errors.NOT_ENOUGH_LIQUIDITY_TO_BORROW); - require(amountFee > 0, Errors.REQUESTED_AMOUNT_TO_SMALL); + require(amountFee > 0, Errors.REQUESTED_AMOUNT_TOO_SMALL); //get the FlashLoanReceiver instance IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index 1b3f6980..db239d1f 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -15,7 +15,7 @@ library Errors { string public constant NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5'; // 'User cannot withdraw more than the available balance' string public constant TRANSFER_NOT_ALLOWED = '6'; // 'Transfer cannot be allowed.' string public constant BORROWING_NOT_ENABLED = '7'; // 'Borrowing is not enabled' - string public constant INVALID_INTERESTRATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected' + string public constant INVALID_INTEREST_RATE_MODE_SELECTED = '8'; // 'Invalid interest rate mode selected' string public constant COLLATERAL_BALANCE_IS_0 = '9'; // 'The collateral balance is 0' string public constant HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10'; // 'Health factor is lesser than the liquidation threshold' string public constant COLLATERAL_CANNOT_COVER_NEW_BORROW = '11'; // 'There is not enough collateral to cover a new borrow' @@ -23,7 +23,7 @@ library Errors { string public constant CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13'; // collateral is (mostly) the same currency that is being borrowed string public constant AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14'; // 'The requested amount is greater than the max loan size in stable rate mode string public constant NO_DEBT_OF_SELECTED_TYPE = '15'; // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' - string public constant NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed' + string public constant NO_EXPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16'; // 'To repay on behalf of an user an explicit amount to repay is needed' string public constant NO_STABLE_RATE_LOAN_IN_RESERVE = '17'; // 'User does not have a stable rate loan in progress on this reserve' string public constant NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18'; // 'User does not have a variable rate loan in progress on this reserve' string public constant UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19'; // 'The underlying balance needs to be greater than 0' @@ -31,34 +31,35 @@ library Errors { // require error messages - LendingPool string public constant NOT_ENOUGH_STABLE_BORROW_BALANCE = '21'; // 'User does not have any stable rate loan for this reserve' - string public constant INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '22'; // 'Interest rate rebalance conditions were not met' + string public constant INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET = '22'; // 'Interest rate rebalance conditions were not met' string public constant LIQUIDATION_CALL_FAILED = '23'; // 'Liquidation call failed' string public constant NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24'; // 'There is not enough liquidity available to borrow' - string public constant REQUESTED_AMOUNT_TO_SMALL = '25'; // 'The requested amount is too small for a FlashLoan.' + string public constant REQUESTED_AMOUNT_TOO_SMALL = '25'; // 'The requested amount is too small for a FlashLoan.' string public constant INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26'; // 'The actual balance of the protocol is inconsistent' + string public constant CALLER_NOT_LENDING_POOL_CONFIGURATOR = '27'; // 'The actual balance of the protocol is inconsistent' // require error messages - aToken - string public constant CALLER_MUST_BE_LENDING_POOL = '27'; // 'The caller of this function must be a lending pool' - string public constant NOT_ALLOWED_TO_REDIRECT_INTEREST = '28'; // 'Caller is not allowed to redirect the interest of the user' - string public constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '29'; // 'User cannot give allowance to himself' - string public constant TRANSFER_AMOUNT_NOT_GT_0 = '30'; // 'Transferred amount needs to be greater than zero' - string public constant INTEREST_ALREADY_REDIRECTED = '31'; // 'Interest is already redirected to the user' - string public constant NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '32'; // 'Interest stream can only be redirected if there is a valid balance' + string public constant CALLER_MUST_BE_LENDING_POOL = '28'; // 'The caller of this function must be a lending pool' + string public constant INTEREST_REDIRECTION_NOT_ALLOWED = '29'; // 'Caller is not allowed to redirect the interest of the user' + string public constant CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30'; // 'User cannot give allowance to himself' + string public constant TRANSFER_AMOUNT_NOT_GT_0 = '31'; // 'Transferred amount needs to be greater than zero' + string public constant INTEREST_ALREADY_REDIRECTED = '32'; // 'Interest is already redirected to the user' + string public constant NO_VALID_BALANCE_FOR_REDIRECTION = '33'; // 'Interest stream can only be redirected if there is a valid balance' // require error messages - ReserveLogic - string public constant RESERVE_ALREADY_INITIALIZED = '33'; // 'Reserve has already been initialized' + string public constant RESERVE_ALREADY_INITIALIZED = '34'; // 'Reserve has already been initialized' //require error messages - LendingPoolConfiguration - string public constant CALLER_NOT_LENDING_POOL_MANAGER = '34'; // 'The caller must be a lending pool manager' - string public constant RESERVE_LIQUIDITY_NOT_0 = '35'; // 'The liquidity of the reserve needs to be 0' + string public constant CALLER_NOT_LENDING_POOL_MANAGER = '35'; // 'The caller must be a lending pool manager' + string public constant RESERVE_LIQUIDITY_NOT_0 = '36'; // 'The liquidity of the reserve needs to be 0' //require error messages - LendingPoolAddressesProviderRegistry - string public constant PROVIDER_NOT_REGISTERED = '36'; // 'Provider is not registered' + string public constant PROVIDER_NOT_REGISTERED = '37'; // 'Provider is not registered' //return error messages - LendingPoolLiquidationManager - string public constant HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '37'; // 'Health factor is not below the threshold' - string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '38'; // 'The collateral chosen cannot be liquidated' - string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '39'; // 'User did not borrow the specified currency' - string public constant NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '40'; // "There isn't enough liquidity available to liquidate" - string public constant NO_ERRORS = '41'; // 'No errors' + string public constant HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '38'; // 'Health factor is not below the threshold' + string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '39'; // 'The collateral chosen cannot be liquidated' + string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40'; // 'User did not borrow the specified currency' + string public constant NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41'; // "There isn't enough liquidity available to liquidate" + string public constant NO_ERRORS = '42'; // 'No errors' } diff --git a/contracts/libraries/logic/ValidationLogic.sol b/contracts/libraries/logic/ValidationLogic.sol index 09a916e5..4541f489 100644 --- a/contracts/libraries/logic/ValidationLogic.sol +++ b/contracts/libraries/logic/ValidationLogic.sol @@ -145,7 +145,7 @@ library ValidationLogic { require( uint256(ReserveLogic.InterestRateMode.VARIABLE) == interestRateMode || uint256(ReserveLogic.InterestRateMode.STABLE) == interestRateMode, - Errors.INVALID_INTERESTRATE_MODE_SELECTED + Errors.INVALID_INTEREST_RATE_MODE_SELECTED ); //check that the amount is available in the reserve @@ -245,7 +245,7 @@ library ValidationLogic { require( amountSent != uint256(-1) || msg.sender == onBehalfOf, - Errors.NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF + Errors.NO_EXPLICIT_AMOUNT_TO_REPAY_ON_BEHALF ); } @@ -290,7 +290,7 @@ library ValidationLogic { Errors.CALLATERAL_SAME_AS_BORROWING_CURRENCY ); } else { - revert(Errors.INVALID_INTERESTRATE_MODE_SELECTED); + revert(Errors.INVALID_INTEREST_RATE_MODE_SELECTED); } } diff --git a/contracts/tokenization/AToken.sol b/contracts/tokenization/AToken.sol index 2c6e8541..3ec3eac5 100644 --- a/contracts/tokenization/AToken.sol +++ b/contracts/tokenization/AToken.sol @@ -101,7 +101,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { function redirectInterestStreamOf(address from, address to) external override { require( msg.sender == _interestRedirectionAllowances[from], - Errors.NOT_ALLOWED_TO_REDIRECT_INTEREST + Errors.INTEREST_REDIRECTION_NOT_ALLOWED ); _redirectInterestStream(from, to); } @@ -494,7 +494,7 @@ contract AToken is VersionedInitializable, ERC20, IAToken { uint256 fromIndex ) = _cumulateBalance(from); - require(fromBalance > 0, Errors.NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM); + require(fromBalance > 0, Errors.NO_VALID_BALANCE_FOR_REDIRECTION); //if the user is already redirecting the interest to someone, before changing //the redirection address we substract the redirected balance of the previous diff --git a/helpers/types.ts b/helpers/types.ts index e98e5a3c..fc3ca1d7 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -52,7 +52,7 @@ export enum ProtocolErrors { NOT_ENOUGH_AVAILABLE_USER_BALANCE = '5', // 'User cannot withdraw more than the available balance' TRANSFER_NOT_ALLOWED = '6', // 'Transfer cannot be allowed.' BORROWING_NOT_ENABLED = '7', // 'Borrowing is not enabled' - INVALID_INTERESTRATE_MODE_SELECTED = '8', // 'Invalid interest rate mode selected' + INVALID_INTEREST_RATE_MODE_SELECTED = '8', // 'Invalid interest rate mode selected' COLLATERAL_BALANCE_IS_0 = '9', // 'The collateral balance is 0' HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD = '10', // 'Health factor is lesser than the liquidation threshold' COLLATERAL_CANNOT_COVER_NEW_BORROW = '11', // 'There is not enough collateral to cover a new borrow' @@ -60,7 +60,7 @@ export enum ProtocolErrors { CALLATERAL_SAME_AS_BORROWING_CURRENCY = '13', // collateral is (mostly) the same currency that is being borrowed AMOUNT_BIGGER_THAN_MAX_LOAN_SIZE_STABLE = '14', // 'The requested amount is greater than the max loan size in stable rate mode NO_DEBT_OF_SELECTED_TYPE = '15', // 'for repayment of stable debt, the user needs to have stable debt, otherwise, he needs to have variable debt' - NO_EPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16', // 'To repay on behalf of an user an explicit amount to repay is needed' + NO_EXPLICIT_AMOUNT_TO_REPAY_ON_BEHALF = '16', // 'To repay on behalf of an user an explicit amount to repay is needed' NO_STABLE_RATE_LOAN_IN_RESERVE = '17', // 'User does not have a stable rate loan in progress on this reserve' NO_VARIABLE_RATE_LOAN_IN_RESERVE = '18', // 'User does not have a variable rate loan in progress on this reserve' UNDERLYING_BALANCE_NOT_GREATER_THAN_0 = '19', // 'The underlying balance needs to be greater than 0' @@ -68,36 +68,37 @@ export enum ProtocolErrors { // require error messages - LendingPool NOT_ENOUGH_STABLE_BORROW_BALANCE = '21', // 'User does not have any stable rate loan for this reserve' - INTERESTRATE_REBALANCE_CONDITIONS_NOT_MET = '22', // 'Interest rate rebalance conditions were not met' + INTEREST_RATE_REBALANCE_CONDITIONS_NOT_MET = '22', // 'Interest rate rebalance conditions were not met' LIQUIDATION_CALL_FAILED = '23', // 'Liquidation call failed' NOT_ENOUGH_LIQUIDITY_TO_BORROW = '24', // 'There is not enough liquidity available to borrow' - REQUESTED_AMOUNT_TO_SMALL = '25', // 'The requested amount is too small for a FlashLoan.' + REQUESTED_AMOUNT_TOO_SMALL = '25', // 'The requested amount is too small for a FlashLoan.' INCONSISTENT_PROTOCOL_ACTUAL_BALANCE = '26', // 'The actual balance of the protocol is inconsistent' + CALLER_NOT_LENDING_POOL_CONFIGURATOR = '27', // 'The actual balance of the protocol is inconsistent' // require error messages - aToken - CALLER_MUST_BE_LENDING_POOL = '27', // 'The caller of this function must be a lending pool' - NOT_ALLOWED_TO_REDIRECT_INTEREST = '28', // 'Caller is not allowed to redirect the interest of the user' - CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '29', // 'User cannot give allowance to himself' - TRANSFER_AMOUNT_NOT_GT_0 = '30', // 'Transferred amount needs to be greater than zero' - INTEREST_ALREADY_REDIRECTED = '31', // 'Interest is already redirected to the user' - NO_VALID_BALANCE_FOR_REDIRECT_INT_STREAM = '32', // 'Interest stream can only be redirected if there is a valid balance' + CALLER_MUST_BE_LENDING_POOL = '28', // 'The caller of this function must be a lending pool' + INTEREST_REDIRECTION_NOT_ALLOWED = '29', // 'Caller is not allowed to redirect the interest of the user' + CANNOT_GIVE_ALLOWANCE_TO_HIMSELF = '30', // 'User cannot give allowance to himself' + TRANSFER_AMOUNT_NOT_GT_0 = '31', // 'Transferred amount needs to be greater than zero' + INTEREST_ALREADY_REDIRECTED = '32', // 'Interest is already redirected to the user' + NO_VALID_BALANCE_FOR_REDIRECTION = '33', // 'Interest stream can only be redirected if there is a valid balance' // require error messages - ReserveLogic - RESERVE_ALREADY_INITIALIZED = '33', // 'Reserve has already been initialized' + RESERVE_ALREADY_INITIALIZED = '34', // 'Reserve has already been initialized' //require error messages - LendingPoolConfiguration - CALLER_NOT_LENDING_POOL_MANAGER = '34', // 'The caller must be a lending pool manager' - RESERVE_LIQUIDITY_NOT_0 = '35', // 'The liquidity of the reserve needs to be 0' + CALLER_NOT_LENDING_POOL_MANAGER = '35', // 'The caller must be a lending pool manager' + RESERVE_LIQUIDITY_NOT_0 = '36', // 'The liquidity of the reserve needs to be 0' //require error messages - LendingPoolAddressesProviderRegistry - PROVIDER_NOT_REGISTERED = '36', // 'Provider is not registered' + PROVIDER_NOT_REGISTERED = '37', // 'Provider is not registered' //return error messages - LendingPoolLiquidationManager - HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '37', // 'Health factor is not below the threshold' - COLLATERAL_CANNOT_BE_LIQUIDATED = '38', // 'The collateral chosen cannot be liquidated' - SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '39', // 'User did not borrow the specified currency' - NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '40', // "There isn't enough liquidity available to liquidate" - NO_ERRORS = '41', // 'No errors' + HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '38', // 'Health factor is not below the threshold' + COLLATERAL_CANNOT_BE_LIQUIDATED = '39', // 'The collateral chosen cannot be liquidated' + SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40', // 'User did not borrow the specified currency' + NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41', // "There isn't enough liquidity available to liquidate" + NO_ERRORS = '42', // 'No errors' // old diff --git a/test/flashloan.spec.ts b/test/flashloan.spec.ts index dfd7be16..223ea890 100644 --- a/test/flashloan.spec.ts +++ b/test/flashloan.spec.ts @@ -12,7 +12,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { let _mockFlashLoanReceiver = {} as MockFlashLoanReceiver; const { INCONSISTENT_PROTOCOL_ACTUAL_BALANCE, - REQUESTED_AMOUNT_TO_SMALL, + REQUESTED_AMOUNT_TOO_SMALL, NOT_ENOUGH_LIQUIDITY_TO_BORROW, } = ProtocolErrors; @@ -112,7 +112,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { '1', //1 wei loan '0x10' ) - ).to.be.revertedWith(REQUESTED_AMOUNT_TO_SMALL); + ).to.be.revertedWith(REQUESTED_AMOUNT_TOO_SMALL); }); it('tries to take a flashloan that is bigger than the available liquidity (revert expected)', async () => { From 07007fa933170fa622dc37a076031f6a816a15a1 Mon Sep 17 00:00:00 2001 From: pol <> Date: Thu, 3 Sep 2020 11:17:49 +0200 Subject: [PATCH 11/11] Fixed sintax errors --- contracts/lendingpool/LendingPoolLiquidationManager.sol | 2 +- contracts/libraries/helpers/Errors.sol | 2 +- helpers/types.ts | 2 +- test/liquidation-atoken.spec.ts | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/lendingpool/LendingPoolLiquidationManager.sol b/contracts/lendingpool/LendingPoolLiquidationManager.sol index 27a29277..34cb6f38 100644 --- a/contracts/lendingpool/LendingPoolLiquidationManager.sol +++ b/contracts/lendingpool/LendingPoolLiquidationManager.sol @@ -133,7 +133,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl if (vars.healthFactor >= GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD) { return ( uint256(LiquidationErrors.HEALTH_FACTOR_ABOVE_THRESHOLD), - Errors.HEALTH_FACTOR_NOT_BELLOW_THRESHOLD + Errors.HEALTH_FACTOR_NOT_BELOW_THRESHOLD ); } diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index db239d1f..079d1b9f 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -57,7 +57,7 @@ library Errors { string public constant PROVIDER_NOT_REGISTERED = '37'; // 'Provider is not registered' //return error messages - LendingPoolLiquidationManager - string public constant HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '38'; // 'Health factor is not below the threshold' + string public constant HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '38'; // 'Health factor is not below the threshold' string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '39'; // 'The collateral chosen cannot be liquidated' string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40'; // 'User did not borrow the specified currency' string public constant NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41'; // "There isn't enough liquidity available to liquidate" diff --git a/helpers/types.ts b/helpers/types.ts index fc3ca1d7..f17d5e9f 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -94,7 +94,7 @@ export enum ProtocolErrors { PROVIDER_NOT_REGISTERED = '37', // 'Provider is not registered' //return error messages - LendingPoolLiquidationManager - HEALTH_FACTOR_NOT_BELLOW_THRESHOLD = '38', // 'Health factor is not below the threshold' + HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '38', // 'Health factor is not below the threshold' COLLATERAL_CANNOT_BE_LIQUIDATED = '39', // 'The collateral chosen cannot be liquidated' SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40', // 'User did not borrow the specified currency' NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41', // "There isn't enough liquidity available to liquidate" diff --git a/test/liquidation-atoken.spec.ts b/test/liquidation-atoken.spec.ts index 155b7c25..921114f0 100644 --- a/test/liquidation-atoken.spec.ts +++ b/test/liquidation-atoken.spec.ts @@ -13,7 +13,7 @@ const {expect} = chai; makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => { const { - HEALTH_FACTOR_NOT_BELLOW_THRESHOLD, + HEALTH_FACTOR_NOT_BELOW_THRESHOLD, INVALID_HF, SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER, COLLATERAL_CANNOT_BE_LIQUIDATED, @@ -71,7 +71,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => //someone tries to liquidate user 2 await expect( pool.liquidationCall(weth.address, dai.address, borrower.address, 1, true) - ).to.be.revertedWith(HEALTH_FACTOR_NOT_BELLOW_THRESHOLD); + ).to.be.revertedWith(HEALTH_FACTOR_NOT_BELOW_THRESHOLD); }); it('LIQUIDATION - Drop the health factor below 1', async () => {