diff --git a/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol b/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol index f3ea77cb..c8c2d946 100644 --- a/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol +++ b/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol @@ -35,34 +35,34 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy { LendingPoolAddressesProvider public immutable addressesProvider; //base variable borrow rate when Utilization rate = 0. Expressed in ray - uint256 internal immutable baseVariableBorrowRate; + uint256 internal immutable _baseVariableBorrowRate; //slope of the variable interest curve when utilization rate > 0 and <= OPTIMAL_UTILIZATION_RATE. Expressed in ray - uint256 internal immutable variableRateSlope1; + uint256 internal immutable _variableRateSlope1; //slope of the variable interest curve when utilization rate > OPTIMAL_UTILIZATION_RATE. Expressed in ray - uint256 internal immutable variableRateSlope2; + uint256 internal immutable _variableRateSlope2; //slope of the stable interest curve when utilization rate > 0 and <= OPTIMAL_UTILIZATION_RATE. Expressed in ray - uint256 internal immutable stableRateSlope1; + uint256 internal immutable _stableRateSlope1; //slope of the stable interest curve when utilization rate > OPTIMAL_UTILIZATION_RATE. Expressed in ray - uint256 internal immutable stableRateSlope2; + uint256 internal immutable _stableRateSlope2; constructor( - LendingPoolAddressesProvider _provider, - uint256 _baseVariableBorrowRate, - uint256 _variableRateSlope1, - uint256 _variableRateSlope2, - uint256 _stableRateSlope1, - uint256 _stableRateSlope2 + LendingPoolAddressesProvider provider, + uint256 baseVariableBorrowRate, + uint256 variableRateSlope1, + uint256 variableRateSlope2, + uint256 stableRateSlope1, + uint256 stableRateSlope2 ) public { - addressesProvider = _provider; - baseVariableBorrowRate = _baseVariableBorrowRate; - variableRateSlope1 = _variableRateSlope1; - variableRateSlope2 = _variableRateSlope2; - stableRateSlope1 = _stableRateSlope1; - stableRateSlope2 = _stableRateSlope2; + addressesProvider = provider; + _baseVariableBorrowRate = baseVariableBorrowRate; + _variableRateSlope1 = variableRateSlope1; + _variableRateSlope2 = variableRateSlope2; + _stableRateSlope1 = stableRateSlope1; + _stableRateSlope2 = stableRateSlope2; } /** @@ -70,115 +70,122 @@ contract DefaultReserveInterestRateStrategy is IReserveInterestRateStrategy { */ function getVariableRateSlope1() external view returns (uint256) { - return variableRateSlope1; + return _variableRateSlope1; } function getVariableRateSlope2() external view returns (uint256) { - return variableRateSlope2; + return _variableRateSlope2; } function getStableRateSlope1() external view returns (uint256) { - return stableRateSlope1; + return _stableRateSlope1; } function getStableRateSlope2() external view returns (uint256) { - return stableRateSlope2; + return _stableRateSlope2; } function getBaseVariableBorrowRate() external override view returns (uint256) { - return baseVariableBorrowRate; + return _baseVariableBorrowRate; } /** * @dev calculates the interest rates depending on the available liquidity and the total borrowed. - * @param _reserve the address of the reserve - * @param _availableLiquidity the liquidity available in the reserve - * @param _totalBorrowsStable the total borrowed from the reserve a stable rate - * @param _totalBorrowsVariable the total borrowed from the reserve at a variable rate - * @param _averageStableBorrowRate the weighted average of all the stable rate borrows + * @param reserve the address of the reserve + * @param availableLiquidity the liquidity available in the reserve + * @param totalBorrowsStable the total borrowed from the reserve a stable rate + * @param totalBorrowsVariable the total borrowed from the reserve at a variable rate + * @param averageStableBorrowRate the weighted average of all the stable rate borrows * @return currentLiquidityRate the liquidity rate * @return currentStableBorrowRate stable borrow rate * @return currentVariableBorrowRate variable borrow rate **/ function calculateInterestRates( - address _reserve, - uint256 _availableLiquidity, - uint256 _totalBorrowsStable, - uint256 _totalBorrowsVariable, - uint256 _averageStableBorrowRate + address reserve, + uint256 availableLiquidity, + uint256 totalBorrowsStable, + uint256 totalBorrowsVariable, + uint256 averageStableBorrowRate ) external override view returns ( - uint256 currentLiquidityRate, - uint256 currentStableBorrowRate, - uint256 currentVariableBorrowRate + uint256, + uint256, + uint256 ) { - uint256 totalBorrows = _totalBorrowsStable.add(_totalBorrowsVariable); + uint256 totalBorrows = totalBorrowsStable.add(totalBorrowsVariable); + uint256 currentVariableBorrowRate = 0; + uint256 currentStableBorrowRate = 0; + uint256 currentLiquidityRate = 0; - uint256 utilizationRate = (totalBorrows == 0 && _availableLiquidity == 0) + uint256 utilizationRate = (totalBorrows == 0 && availableLiquidity == 0) ? 0 - : totalBorrows.rayDiv(_availableLiquidity.add(totalBorrows)); + : totalBorrows.rayDiv(availableLiquidity.add(totalBorrows)); currentStableBorrowRate = ILendingRateOracle(addressesProvider.getLendingRateOracle()) - .getMarketBorrowRate(_reserve); + .getMarketBorrowRate(reserve); if (utilizationRate > OPTIMAL_UTILIZATION_RATE) { uint256 excessUtilizationRateRatio = utilizationRate.sub(OPTIMAL_UTILIZATION_RATE).rayDiv( EXCESS_UTILIZATION_RATE ); - currentStableBorrowRate = currentStableBorrowRate.add(stableRateSlope1).add( - stableRateSlope2.rayMul(excessUtilizationRateRatio) + currentStableBorrowRate = currentStableBorrowRate.add(_stableRateSlope1).add( + _stableRateSlope2.rayMul(excessUtilizationRateRatio) ); - currentVariableBorrowRate = baseVariableBorrowRate.add(variableRateSlope1).add( - variableRateSlope2.rayMul(excessUtilizationRateRatio) + currentVariableBorrowRate = _baseVariableBorrowRate.add(_variableRateSlope1).add( + _variableRateSlope2.rayMul(excessUtilizationRateRatio) ); } else { currentStableBorrowRate = currentStableBorrowRate.add( - stableRateSlope1.rayMul(utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE)) + _stableRateSlope1.rayMul(utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE)) ); - currentVariableBorrowRate = baseVariableBorrowRate.add( - utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE).rayMul(variableRateSlope1) + currentVariableBorrowRate = _baseVariableBorrowRate.add( + utilizationRate.rayDiv(OPTIMAL_UTILIZATION_RATE).rayMul(_variableRateSlope1) ); } currentLiquidityRate = getOverallBorrowRateInternal( - _totalBorrowsStable, - _totalBorrowsVariable, + totalBorrowsStable, + totalBorrowsVariable, currentVariableBorrowRate, - _averageStableBorrowRate + averageStableBorrowRate ) .rayMul(utilizationRate); + + return (currentLiquidityRate, + currentStableBorrowRate, + currentVariableBorrowRate); } /** * @dev calculates the overall borrow rate as the weighted average between the total variable borrows and total stable borrows. - * @param _totalBorrowsStable the total borrowed from the reserve a stable rate - * @param _totalBorrowsVariable the total borrowed from the reserve at a variable rate - * @param _currentVariableBorrowRate the current variable borrow rate - * @param _currentAverageStableBorrowRate the weighted average of all the stable rate borrows + * @param totalBorrowsStable the total borrowed from the reserve a stable rate + * @param totalBorrowsVariable the total borrowed from the reserve at a variable rate + * @param currentVariableBorrowRate the current variable borrow rate + * @param currentAverageStableBorrowRate the weighted average of all the stable rate borrows * @return the weighted averaged borrow rate **/ function getOverallBorrowRateInternal( - uint256 _totalBorrowsStable, - uint256 _totalBorrowsVariable, - uint256 _currentVariableBorrowRate, - uint256 _currentAverageStableBorrowRate + uint256 totalBorrowsStable, + uint256 totalBorrowsVariable, + uint256 currentVariableBorrowRate, + uint256 currentAverageStableBorrowRate ) internal pure returns (uint256) { - uint256 totalBorrows = _totalBorrowsStable.add(_totalBorrowsVariable); + uint256 totalBorrows = totalBorrowsStable.add(totalBorrowsVariable); if (totalBorrows == 0) return 0; - uint256 weightedVariableRate = _totalBorrowsVariable.wadToRay().rayMul( - _currentVariableBorrowRate + uint256 weightedVariableRate = totalBorrowsVariable.wadToRay().rayMul( + currentVariableBorrowRate ); - uint256 weightedStableRate = _totalBorrowsStable.wadToRay().rayMul( - _currentAverageStableBorrowRate + uint256 weightedStableRate = totalBorrowsStable.wadToRay().rayMul( + currentAverageStableBorrowRate ); uint256 overallBorrowRate = weightedVariableRate.add(weightedStableRate).rayDiv( diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 6e43f978..6d9ec9d5 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -8,7 +8,7 @@ import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import { VersionedInitializable } from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; -import {LendingPoolAddressesProvider} from '../configuration/LendingPoolAddressesProvider.sol'; +import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {IAToken} from '../tokenization/interfaces/IAToken.sol'; import {Helpers} from '../libraries/helpers/Helpers.sol'; import {WadRayMath} from '../libraries/math/WadRayMath.sol'; @@ -37,136 +37,112 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { using ReserveLogic for ReserveLogic.ReserveData; using ReserveConfiguration for ReserveConfiguration.Map; using UserConfiguration for UserConfiguration.Map; - - //main configuration parameters - uint256 private constant REBALANCE_DOWN_RATE_DELTA = (1e27) / 5; - uint256 private constant MAX_STABLE_RATE_BORROW_SIZE_PERCENT = 25; - uint256 private constant FLASHLOAN_FEE_TOTAL = 9; - - LendingPoolAddressesProvider public addressesProvider; using SafeERC20 for IERC20; - mapping(address => ReserveLogic.ReserveData) internal reserves; - mapping(address => UserConfiguration.Map) internal usersConfig; + //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; - address[] public reservesList; + ILendingPoolAddressesProvider internal addressesProvider; + + mapping(address => ReserveLogic.ReserveData) internal _reserves; + mapping(address => UserConfiguration.Map) internal _usersConfig; + + address[] internal reservesList; /** * @dev emitted on deposit - * @param _reserve the address of the reserve - * @param _user the address of the user - * @param _amount the amount to be deposited - * @param _referral the referral number of the action - * @param _timestamp the timestamp of the action + * @param reserve the address of the reserve + * @param user the address of the user + * @param amount the amount to be deposited + * @param referral the referral number of the action **/ event Deposit( - address indexed _reserve, - address indexed _user, - uint256 _amount, - uint16 indexed _referral, - uint256 _timestamp + address indexed reserve, + address indexed user, + uint256 amount, + uint16 indexed referral ); /** * @dev emitted during a withdraw action. - * @param _reserve the address of the reserve - * @param _user the address of the user - * @param _amount the amount to be withdrawn - * @param _timestamp the timestamp of the action + * @param reserve the address of the reserve + * @param user the address of the user + * @param amount the amount to be withdrawn **/ - event Withdraw( - address indexed _reserve, - address indexed _user, - uint256 _amount, - uint256 _timestamp - ); + event Withdraw(address indexed reserve, address indexed user, uint256 amount); /** * @dev emitted on borrow - * @param _reserve the address of the reserve - * @param _user the address of the user - * @param _amount the amount to be deposited - * @param _borrowRateMode the rate mode, can be either 1-stable or 2-variable - * @param _borrowRate the rate at which the user has borrowed - * @param _referral the referral number of the action - * @param _timestamp the timestamp of the action + * @param reserve the address of the reserve + * @param user the address of the user + * @param amount the amount to be deposited + * @param borrowRateMode the rate mode, can be either 1-stable or 2-variable + * @param borrowRate the rate at which the user has borrowed + * @param referral the referral number of the action **/ event Borrow( - address indexed _reserve, - address indexed _user, - uint256 _amount, - uint256 _borrowRateMode, - uint256 _borrowRate, - uint16 indexed _referral, - uint256 _timestamp + address indexed reserve, + address indexed user, + uint256 amount, + uint256 borrowRateMode, + uint256 borrowRate, + uint16 indexed referral ); - /** * @dev emitted on repay - * @param _reserve the address of the reserve - * @param _user the address of the user for which the repay has been executed - * @param _repayer the address of the user that has performed the repay action - * @param _amount the amount repaid - * @param _timestamp the timestamp of the action + * @param reserve the address of the reserve + * @param user the address of the user for which the repay has been executed + * @param repayer the address of the user that has performed the repay action + * @param amount the amount repaid **/ event Repay( - address indexed _reserve, - address indexed _user, - address indexed _repayer, - uint256 _amount, - uint256 _timestamp + address indexed reserve, + address indexed user, + address indexed repayer, + uint256 amount ); - /** * @dev emitted when a user performs a rate swap - * @param _reserve the address of the reserve - * @param _user the address of the user executing the swap - * @param _timestamp the timestamp of the action + * @param reserve the address of the reserve + * @param user the address of the user executing the swap **/ - event Swap(address indexed _reserve, address indexed _user, uint256 _timestamp); + event Swap(address indexed reserve, address indexed user, uint256 timestamp); /** * @dev emitted when a user enables a reserve as collateral - * @param _reserve the address of the reserve - * @param _user the address of the user + * @param reserve the address of the reserve + * @param user the address of the user **/ - event ReserveUsedAsCollateralEnabled(address indexed _reserve, address indexed _user); + event ReserveUsedAsCollateralEnabled(address indexed reserve, address indexed user); /** * @dev emitted when a user disables a reserve as collateral - * @param _reserve the address of the reserve - * @param _user the address of the user + * @param reserve the address of the reserve + * @param user the address of the user **/ - event ReserveUsedAsCollateralDisabled(address indexed _reserve, address indexed _user); + event ReserveUsedAsCollateralDisabled(address indexed reserve, address indexed user); /** * @dev emitted when the stable rate of a user gets rebalanced - * @param _reserve the address of the reserve - * @param _user the address of the user for which the rebalance has been executed - * @param _timestamp the timestamp of the action + * @param reserve the address of the reserve + * @param user the address of the user for which the rebalance has been executed **/ - event RebalanceStableBorrowRate( - address indexed _reserve, - address indexed _user, - uint256 _timestamp - ); - + event RebalanceStableBorrowRate(address indexed reserve, address indexed user); /** * @dev emitted when a flashloan is executed - * @param _target the address of the flashLoanReceiver - * @param _reserve the address of the reserve - * @param _amount the amount requested - * @param _totalFee the total fee on the amount - * @param _timestamp the timestamp of the action + * @param target the address of the flashLoanReceiver + * @param reserve the address of the reserve + * @param amount the amount requested + * @param totalFee the total fee on the amount **/ event FlashLoan( - address indexed _target, - address indexed _reserve, - uint256 _amount, - uint256 _totalFee, - uint256 _timestamp + address indexed target, + address indexed reserve, + uint256 amount, + uint256 totalFee ); - /** * @dev these events are not emitted directly by the LendingPool * but they are declared here as the LendingPoolLiquidationManager @@ -176,46 +152,40 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { /** * @dev emitted when a borrow fee is liquidated - * @param _collateral the address of the collateral being liquidated - * @param _reserve the address of the reserve - * @param _user the address of the user being liquidated - * @param _feeLiquidated the total fee liquidated - * @param _liquidatedCollateralForFee the amount of collateral received by the protocol in exchange for the fee - * @param _timestamp the timestamp of the action + * @param collateral the address of the collateral being liquidated + * @param reserve the address of the reserve + * @param user the address of the user being liquidated + * @param feeLiquidated the total fee liquidated + * @param liquidatedCollateralForFee the amount of collateral received by the protocol in exchange for the fee **/ event OriginationFeeLiquidated( - address indexed _collateral, - address indexed _reserve, - address indexed _user, - uint256 _feeLiquidated, - uint256 _liquidatedCollateralForFee, - uint256 _timestamp + address indexed collateral, + address indexed reserve, + address indexed user, + uint256 feeLiquidated, + uint256 liquidatedCollateralForFee ); - /** * @dev emitted when a borrower is liquidated - * @param _collateral the address of the collateral being liquidated - * @param _reserve the address of the reserve - * @param _user the address of the user being liquidated - * @param _purchaseAmount the total amount liquidated - * @param _liquidatedCollateralAmount the amount of collateral being liquidated - * @param _accruedBorrowInterest the amount of interest accrued by the borrower since the last action - * @param _liquidator the address of the liquidator - * @param _receiveAToken true if the liquidator wants to receive aTokens, false otherwise - * @param _timestamp the timestamp of the action + * @param collateral the address of the collateral being liquidated + * @param reserve the address of the reserve + * @param user the address of the user being liquidated + * @param purchaseAmount the total amount liquidated + * @param liquidatedCollateralAmount the amount of collateral being liquidated + * @param accruedBorrowInterest the amount of interest accrued by the borrower since the last action + * @param liquidator the address of the liquidator + * @param receiveAToken true if the liquidator wants to receive aTokens, false otherwise **/ event LiquidationCall( - address indexed _collateral, - address indexed _reserve, - address indexed _user, - uint256 _purchaseAmount, - uint256 _liquidatedCollateralAmount, - uint256 _accruedBorrowInterest, - address _liquidator, - bool _receiveAToken, - uint256 _timestamp + address indexed collateral, + address indexed reserve, + address indexed user, + uint256 purchaseAmount, + uint256 liquidatedCollateralAmount, + uint256 accruedBorrowInterest, + address liquidator, + bool receiveAToken ); - /** * @dev only lending pools configurator can use functions affected by this modifier **/ @@ -235,123 +205,123 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { /** * @dev this function is invoked by the proxy contract when the LendingPool contract is added to the * AddressesProvider. - * @param _addressesProvider the address of the LendingPoolAddressesProvider registry + * @param provider the address of the LendingPoolAddressesProvider registry **/ - function initialize(LendingPoolAddressesProvider _addressesProvider) public initializer { - addressesProvider = _addressesProvider; + 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 _reserve 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. + * @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 _reserve, - uint256 _amount, - uint16 _referralCode + address asset, + uint256 amount, + uint16 referralCode ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; - ValidationLogic.validateDeposit(reserve, _amount); + ValidationLogic.validateDeposit(reserve, amount); IAToken aToken = IAToken(reserve.aTokenAddress); bool isFirstDeposit = aToken.balanceOf(msg.sender) == 0; reserve.updateCumulativeIndexesAndTimestamp(); - reserve.updateInterestRates(_reserve, _amount, 0); + reserve.updateInterestRates(asset, amount, 0); if (isFirstDeposit) { - usersConfig[msg.sender].setUsingAsCollateral(reserve.index, true); + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, true); } //minting AToken to user 1:1 with the specific exchange rate - aToken.mint(msg.sender, _amount); + aToken.mint(msg.sender, amount); //transfer to the aToken contract - IERC20(_reserve).safeTransferFrom(msg.sender, address(aToken), _amount); + IERC20(asset).safeTransferFrom(msg.sender, address(aToken), amount); //solium-disable-next-line - emit Deposit(_reserve, msg.sender, _amount, _referralCode, block.timestamp); + emit Deposit(asset, msg.sender, amount, referralCode); } /** - * @dev withdraws the assets of _user. - * @param _reserve the address of the reserve - * @param _amount the underlying amount to be redeemed + * @dev withdraws the _reserves of _user. + * @param asset the address of the reserve + * @param amount the underlying amount to be redeemed **/ - function withdraw(address _reserve, uint256 _amount) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + function withdraw(address asset, uint256 amount) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; IAToken aToken = IAToken(reserve.aTokenAddress); uint256 userBalance = aToken.balanceOf(msg.sender); - uint256 amountToWithdraw = _amount; + uint256 amountToWithdraw = amount; //if amount is equal to uint(-1), the user wants to redeem everything - if (_amount == UINT_MAX_VALUE) { + if (amount == UINT_MAX_VALUE) { amountToWithdraw = userBalance; } ValidationLogic.validateWithdraw( - _reserve, + asset, address(aToken), amountToWithdraw, userBalance, - reserves, - usersConfig[msg.sender], + _reserves, + _usersConfig[msg.sender], reservesList, addressesProvider.getPriceOracle() ); reserve.updateCumulativeIndexesAndTimestamp(); - reserve.updateInterestRates(_reserve, 0, amountToWithdraw); + reserve.updateInterestRates(asset, 0, amountToWithdraw); if (amountToWithdraw == userBalance) { - usersConfig[msg.sender].setUsingAsCollateral(reserve.index, false); + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, false); } aToken.burn(msg.sender, msg.sender, amountToWithdraw); //solium-disable-next-line - emit Withdraw(_reserve, msg.sender, _amount, block.timestamp); + emit Withdraw(asset, msg.sender, amount); } /** * @dev Allows users to borrow a specific amount of the reserve currency, provided that the borrower * already deposited enough collateral. - * @param _reserve 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) + * @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 _reserve, - uint256 _amount, - uint256 _interestRateMode, - uint16 _referralCode + address asset, + uint256 amount, + uint256 interestRateMode, + uint16 referralCode ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; - UserConfiguration.Map storage userConfig = usersConfig[msg.sender]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; + UserConfiguration.Map storage userConfig = _usersConfig[msg.sender]; uint256 amountInETH = IPriceOracleGetter(addressesProvider.getPriceOracle()) - .getAssetPrice(_reserve) - .mul(_amount) + .getAssetPrice(asset) + .mul(amount) .div(10**reserve.configuration.getDecimals()); //price is in ether ValidationLogic.validateBorrow( reserve, - _reserve, - _amount, + asset, + amount, amountInETH, - _interestRateMode, + interestRateMode, MAX_STABLE_RATE_BORROW_SIZE_PERCENT, - reserves, - usersConfig[msg.sender], + _reserves, + _usersConfig[msg.sender], reservesList, addressesProvider.getPriceOracle() ); @@ -361,34 +331,30 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { reserve.updateCumulativeIndexesAndTimestamp(); - if (ReserveLogic.InterestRateMode(_interestRateMode) == ReserveLogic.InterestRateMode.STABLE) { - IStableDebtToken(reserve.stableDebtTokenAddress).mint(msg.sender, _amount, userStableRate); - uint40 stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress) - .getUserLastUpdated(msg.sender); + if (ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE) { + IStableDebtToken(reserve.stableDebtTokenAddress).mint(msg.sender, amount, userStableRate); } else { - IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, _amount); + IVariableDebtToken(reserve.variableDebtTokenAddress).mint(msg.sender, amount); } - reserve.updateInterestRates(_reserve, 0, _amount); + reserve.updateInterestRates(asset, 0, amount); if (!userConfig.isBorrowing(reserve.index)) { userConfig.setBorrowing(reserve.index, true); } //if we reached this point, we can transfer - IAToken(reserve.aTokenAddress).transferUnderlyingTo(msg.sender, _amount); + IAToken(reserve.aTokenAddress).transferUnderlyingTo(msg.sender, amount); emit Borrow( - _reserve, + asset, msg.sender, - _amount, - _interestRateMode, - ReserveLogic.InterestRateMode(_interestRateMode) == ReserveLogic.InterestRateMode.STABLE + amount, + interestRateMode, + ReserveLogic.InterestRateMode(interestRateMode) == ReserveLogic.InterestRateMode.STABLE ? userStableRate : reserve.currentVariableBorrowRate, - _referralCode, - //solium-disable-next-line - block.timestamp + referralCode ); } @@ -396,17 +362,17 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { * @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 _reserve 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 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 _reserve, - uint256 _amount, + address asset, + uint256 amount, uint256 _rateMode, address _onBehalfOf ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(_onBehalfOf, reserve); @@ -417,14 +383,14 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { ? stableDebt : variableDebt; - if (_amount != UINT_MAX_VALUE && _amount < paybackAmount) { - paybackAmount = _amount; + if (amount != UINT_MAX_VALUE && amount < paybackAmount) { + paybackAmount = amount; } ValidationLogic.validateRepay( reserve, - _reserve, - _amount, + asset, + amount, rateMode, _onBehalfOf, stableDebt, @@ -441,31 +407,29 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { IVariableDebtToken(reserve.variableDebtTokenAddress).burn(_onBehalfOf, paybackAmount); } - reserve.updateInterestRates(_reserve, paybackAmount, 0); + reserve.updateInterestRates(asset, paybackAmount, 0); if (stableDebt.add(variableDebt).sub(paybackAmount) == 0) { - usersConfig[_onBehalfOf].setBorrowing(reserve.index, false); + _usersConfig[_onBehalfOf].setBorrowing(reserve.index, false); } - IERC20(_reserve).safeTransferFrom(msg.sender, reserve.aTokenAddress, paybackAmount); + IERC20(asset).safeTransferFrom(msg.sender, reserve.aTokenAddress, paybackAmount); emit Repay( - _reserve, + asset, _onBehalfOf, msg.sender, - paybackAmount, - //solium-disable-next-line - block.timestamp + paybackAmount ); } /** * @dev borrowers can user this function to swap between stable and variable borrow rate modes. - * @param _reserve the address of the reserve on which the user borrowed + * @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 _reserve, uint256 _rateMode) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + function swapBorrowRateMode(address asset, uint256 _rateMode) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; (uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve); @@ -473,7 +437,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { ValidationLogic.validateSwapRateMode( reserve, - usersConfig[msg.sender], + _usersConfig[msg.sender], stableDebt, variableDebt, rateMode @@ -495,10 +459,10 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { ); } - reserve.updateInterestRates(_reserve, 0, 0); + reserve.updateInterestRates(asset, 0, 0); emit Swap( - _reserve, + asset, msg.sender, //solium-disable-next-line block.timestamp @@ -509,21 +473,17 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { * @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 _reserve the address of the reserve + * @param asset the address of the reserve * @param _user the address of the user to be rebalanced **/ - function rebalanceStableBorrowRate(address _reserve, address _user) - external - override - nonReentrant - { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + function rebalanceStableBorrowRate(address asset, address _user) external override nonReentrant { + ReserveLogic.ReserveData storage reserve = _reserves[asset]; IStableDebtToken stableDebtToken = IStableDebtToken(reserve.stableDebtTokenAddress); uint256 stableBorrowBalance = IERC20(address(stableDebtToken)).balanceOf(_user); - // user must be borrowing on _reserve at a stable rate + // 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( @@ -549,13 +509,11 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { stableDebtToken.burn(_user, stableBorrowBalance); stableDebtToken.mint(_user, stableBorrowBalance, reserve.currentStableBorrowRate); - reserve.updateInterestRates(_reserve, 0, 0); + reserve.updateInterestRates(asset, 0, 0); emit RebalanceStableBorrowRate( - _reserve, - _user, - //solium-disable-next-line - block.timestamp + asset, + _user ); return; @@ -563,38 +521,38 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { /** * @dev allows depositors to enable or disable a specific deposit as collateral. - * @param _reserve the address of the reserve + * @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 _reserve, bool _useAsCollateral) + function setUserUseReserveAsCollateral(address asset, bool _useAsCollateral) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; ValidationLogic.validateSetUseReserveAsCollateral( reserve, - _reserve, - reserves, - usersConfig[msg.sender], + asset, + _reserves, + _usersConfig[msg.sender], reservesList, addressesProvider.getPriceOracle() ); - usersConfig[msg.sender].setUsingAsCollateral(reserve.index, _useAsCollateral); + _usersConfig[msg.sender].setUsingAsCollateral(reserve.index, _useAsCollateral); if (_useAsCollateral) { - emit ReserveUsedAsCollateralEnabled(_reserve, msg.sender); + emit ReserveUsedAsCollateralEnabled(asset, msg.sender); } else { - emit ReserveUsedAsCollateralDisabled(_reserve, msg.sender); + emit ReserveUsedAsCollateralDisabled(asset, msg.sender); } } /** * @dev users can invoke this function to liquidate an undercollateralized position. - * @param _reserve the address of the collateral to liquidated - * @param _reserve the address of the principal reserve + * @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 @@ -602,7 +560,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { **/ function liquidationCall( address _collateral, - address _reserve, + address asset, address _user, uint256 _purchaseAmount, bool _receiveAToken @@ -614,7 +572,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { abi.encodeWithSignature( 'liquidationCall(address,address,address,uint256,bool)', _collateral, - _reserve, + asset, _user, _purchaseAmount, _receiveAToken @@ -634,60 +592,60 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { * @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 _receiver The address of the contract receiving the funds. The receiver should implement the IFlashLoanReceiver interface. - * @param _reserve the address of the principal reserve - * @param _amount the amount requested for this flashloan + * @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 _receiver, - address _reserve, - uint256 _amount, - bytes calldata _params + address receiverAddress, + address asset, + uint256 amount, + bytes calldata params ) external override nonReentrant { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; address aTokenAddress = reserve.aTokenAddress; //check that the reserve has enough available liquidity - uint256 availableLiquidityBefore = IERC20(_reserve).balanceOf(aTokenAddress); + uint256 availableLiquidityBefore = IERC20(asset).balanceOf(aTokenAddress); //calculate amount fee - uint256 amountFee = _amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); + uint256 amountFee = amount.mul(FLASHLOAN_FEE_TOTAL).div(10000); require( - availableLiquidityBefore >= _amount, + availableLiquidityBefore >= amount, 'There is not enough liquidity available to borrow' ); require(amountFee > 0, 'The requested amount is too small for a FlashLoan.'); //get the FlashLoanReceiver instance - IFlashLoanReceiver receiver = IFlashLoanReceiver(_receiver); + IFlashLoanReceiver receiver = IFlashLoanReceiver(receiverAddress); //transfer funds to the receiver - IAToken(aTokenAddress).transferUnderlyingTo(_receiver, _amount); + IAToken(aTokenAddress).transferUnderlyingTo(receiverAddress, amount); //execute action of the receiver - receiver.executeOperation(_reserve, aTokenAddress, _amount, amountFee, _params); + receiver.executeOperation(asset, aTokenAddress, amount, amountFee, params); //check that the actual balance of the core contract includes the returned amount - uint256 availableLiquidityAfter = IERC20(_reserve).balanceOf(aTokenAddress); + uint256 availableLiquidityAfter = IERC20(asset).balanceOf(aTokenAddress); require( availableLiquidityAfter == availableLiquidityBefore.add(amountFee), 'The actual balance of the protocol is inconsistent' ); - reserve.updateStateOnFlashLoan(_reserve, availableLiquidityBefore, amountFee); + reserve.updateStateOnFlashLoan(asset, availableLiquidityBefore, amountFee); //solium-disable-next-line - emit FlashLoan(_receiver, _reserve, _amount, amountFee, block.timestamp); + emit FlashLoan(receiverAddress, asset, amount, amountFee); } /** * @dev accessory functions to fetch data from the core contract **/ - function getReserveConfigurationData(address _reserve) + function getReserveConfigurationData(address asset) external override view @@ -704,7 +662,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { bool isFreezed ) { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; return ( reserve.configuration.getDecimals(), @@ -720,7 +678,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { ); } - function getReserveTokensAddresses(address _reserve) + function getReserveTokensAddresses(address asset) external override view @@ -730,7 +688,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { address variableDebtTokenAddress ) { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; return ( reserve.aTokenAddress, @@ -739,7 +697,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { ); } - function getReserveData(address _reserve) + function getReserveData(address asset) external override view @@ -756,9 +714,9 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { uint40 lastUpdateTimestamp ) { - ReserveLogic.ReserveData memory reserve = reserves[_reserve]; + ReserveLogic.ReserveData memory reserve = _reserves[asset]; return ( - IERC20(_reserve).balanceOf(reserve.aTokenAddress), + IERC20(asset).balanceOf(reserve.aTokenAddress), IERC20(reserve.stableDebtTokenAddress).totalSupply(), IERC20(reserve.variableDebtTokenAddress).totalSupply(), reserve.currentLiquidityRate, @@ -792,8 +750,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { healthFactor ) = GenericLogic.calculateUserAccountData( _user, - reserves, - usersConfig[_user], + _reserves, + _usersConfig[_user], reservesList, addressesProvider.getPriceOracle() ); @@ -805,7 +763,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { ); } - function getUserReserveData(address _reserve, address _user) + function getUserReserveData(address asset, address _user) external override view @@ -822,7 +780,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { bool usageAsCollateralEnabled ) { - ReserveLogic.ReserveData storage reserve = reserves[_reserve]; + ReserveLogic.ReserveData storage reserve = _reserves[asset]; currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(_user); (currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(_user, reserve); @@ -832,7 +790,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated( _user ); - usageAsCollateralEnabled = usersConfig[_user].isUsingAsCollateral(reserve.index); + usageAsCollateralEnabled = _usersConfig[_user].isUsingAsCollateral(reserve.index); variableBorrowIndex = IVariableDebtToken(reserve.variableDebtTokenAddress).getUserIndex(_user); } @@ -846,55 +804,55 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { /** * @dev initializes a reserve - * @param _reserve the address of the 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 _reserve, + address asset, address _aTokenAddress, address _stableDebtAddress, address _variableDebtAddress, address _interestRateStrategyAddress ) external override onlyLendingPoolConfigurator { - reserves[_reserve].init( + _reserves[asset].init( _aTokenAddress, _stableDebtAddress, _variableDebtAddress, _interestRateStrategyAddress ); - addReserveToListInternal(_reserve); + _addReserveToList(asset); } /** * @dev updates the address of the interest rate strategy contract - * @param _reserve the address of the reserve - * @param _rateStrategyAddress 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 _reserve, address _rateStrategyAddress) + function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress) external override onlyLendingPoolConfigurator { - reserves[_reserve].interestRateStrategyAddress = _rateStrategyAddress; + _reserves[asset].interestRateStrategyAddress = rateStrategyAddress; } - function setConfiguration(address _reserve, uint256 _configuration) + function setConfiguration(address asset, uint256 configuration) external override onlyLendingPoolConfigurator { - reserves[_reserve].configuration.data = _configuration; + _reserves[asset].configuration.data = configuration; } - function getConfiguration(address _reserve) + function getConfiguration(address asset) external override view returns (ReserveConfiguration.Map memory) { - return reserves[_reserve].configuration; + return _reserves[asset].configuration; } /** @@ -902,47 +860,78 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable, ILendingPool { **/ /** - * @dev adds a reserve to the array of the reserves address + * @dev adds a reserve to the array of the _reserves address **/ - function addReserveToListInternal(address _reserve) internal { + function _addReserveToList(address asset) internal { bool reserveAlreadyAdded = false; for (uint256 i = 0; i < reservesList.length; i++) - if (reservesList[i] == _reserve) { + if (reservesList[i] == asset) { reserveAlreadyAdded = true; } if (!reserveAlreadyAdded) { - reserves[_reserve].index = uint8(reservesList.length); - reservesList.push(_reserve); + _reserves[asset].index = uint8(reservesList.length); + reservesList.push(asset); } } - function getReserveNormalizedIncome(address _reserve) external override view returns (uint256) { - return reserves[_reserve].getNormalizedIncome(); + /** + * @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 getReserveNormalizedVariableDebt(address _reserve) + /** + * @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[_reserve].getNormalizedDebt(); + 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 _reserve, - address _user, - uint256 _amount + address asset, + address user, + uint256 amount ) external override view returns (bool) { return GenericLogic.balanceDecreaseAllowed( - _reserve, - _user, - _amount, - reserves, - usersConfig[_user], + 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; + } } diff --git a/contracts/lendingpool/LendingPoolConfigurator.sol b/contracts/lendingpool/LendingPoolConfigurator.sol index 3d1fd91f..9560b88e 100644 --- a/contracts/lendingpool/LendingPoolConfigurator.sol +++ b/contracts/lendingpool/LendingPoolConfigurator.sol @@ -10,7 +10,7 @@ import { InitializableAdminUpgradeabilityProxy } from '../libraries/openzeppelin-upgradeability/InitializableAdminUpgradeabilityProxy.sol'; import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol'; -import {LendingPoolAddressesProvider} from '../configuration/LendingPoolAddressesProvider.sol'; +import {ILendingPoolAddressesProvider} from '../interfaces/ILendingPoolAddressesProvider.sol'; import {ILendingPool} from '../interfaces/ILendingPool.sol'; import {IERC20Detailed} from '../interfaces/IERC20Detailed.sol'; @@ -27,155 +27,157 @@ contract LendingPoolConfigurator is VersionedInitializable { /** * @dev emitted when a reserve is initialized. - * @param _reserve the address of the reserve - * @param _aToken the address of the overlying aToken contract - * @param _interestRateStrategyAddress the address of the interest rate strategy for the reserve + * @param asset the address of the reserve + * @param aToken the address of the overlying aToken contract + * @param stableDebtToken the address of the associated stable rate debt token + * @param variableDebtToken the address of the associated variable rate debt token + * @param interestRateStrategyAddress the address of the interest rate strategy for the reserve **/ event ReserveInitialized( - address indexed _reserve, - address indexed _aToken, - address _stableDebtToken, - address _variableDebtToken, - address _interestRateStrategyAddress + address indexed asset, + address indexed aToken, + address stableDebtToken, + address variableDebtToken, + address interestRateStrategyAddress ); /** * @dev emitted when borrowing is enabled on a reserve - * @param _reserve the address of the reserve - * @param _stableRateEnabled true if stable rate borrowing is enabled, false otherwise + * @param asset the address of the reserve + * @param stableRateEnabled true if stable rate borrowing is enabled, false otherwise **/ - event BorrowingEnabledOnReserve(address _reserve, bool _stableRateEnabled); + event BorrowingEnabledOnReserve(address asset, bool stableRateEnabled); /** * @dev emitted when borrowing is disabled on a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event BorrowingDisabledOnReserve(address indexed _reserve); + event BorrowingDisabledOnReserve(address indexed asset); /** * @dev emitted when a reserve is enabled as collateral. - * @param _reserve the address of the reserve - * @param _ltv the loan to value of the asset when used as collateral - * @param _liquidationThreshold the threshold at which loans using this asset as collateral will be considered undercollateralized - * @param _liquidationBonus the bonus liquidators receive to liquidate this asset + * @param asset the address of the reserve + * @param ltv the loan to value of the asset when used as collateral + * @param liquidationThreshold the threshold at which loans using this asset as collateral will be considered undercollateralized + * @param liquidationBonus the bonus liquidators receive to liquidate this asset **/ event ReserveEnabledAsCollateral( - address indexed _reserve, - uint256 _ltv, - uint256 _liquidationThreshold, - uint256 _liquidationBonus + address indexed asset, + uint256 ltv, + uint256 liquidationThreshold, + uint256 liquidationBonus ); /** * @dev emitted when a reserve is disabled as collateral - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event ReserveDisabledAsCollateral(address indexed _reserve); + event ReserveDisabledAsCollateral(address indexed asset); /** * @dev emitted when stable rate borrowing is enabled on a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event StableRateEnabledOnReserve(address indexed _reserve); + event StableRateEnabledOnReserve(address indexed asset); /** * @dev emitted when stable rate borrowing is disabled on a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event StableRateDisabledOnReserve(address indexed _reserve); + event StableRateDisabledOnReserve(address indexed asset); /** * @dev emitted when a reserve is activated - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event ReserveActivated(address indexed _reserve); + event ReserveActivated(address indexed asset); /** * @dev emitted when a reserve is deactivated - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event ReserveDeactivated(address indexed _reserve); + event ReserveDeactivated(address indexed asset); /** * @dev emitted when a reserve is freezed - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event ReserveFreezed(address indexed _reserve); + event ReserveFreezed(address indexed asset); /** * @dev emitted when a reserve is unfreezed - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - event ReserveUnfreezed(address indexed _reserve); + event ReserveUnfreezed(address indexed asset); /** * @dev emitted when a reserve loan to value is updated - * @param _reserve the address of the reserve - * @param _ltv the new value for the loan to value + * @param asset the address of the reserve + * @param ltv the new value for the loan to value **/ - event ReserveBaseLtvChanged(address _reserve, uint256 _ltv); + event ReserveBaseLtvChanged(address asset, uint256 ltv); /** * @dev emitted when a reserve liquidation threshold is updated - * @param _reserve the address of the reserve - * @param _threshold the new value for the liquidation threshold + * @param asset the address of the reserve + * @param threshold the new value for the liquidation threshold **/ - event ReserveLiquidationThresholdChanged(address _reserve, uint256 _threshold); + event ReserveLiquidationThresholdChanged(address asset, uint256 threshold); /** * @dev emitted when a reserve liquidation bonus is updated - * @param _reserve the address of the reserve - * @param _bonus the new value for the liquidation bonus + * @param asset the address of the reserve + * @param bonus the new value for the liquidation bonus **/ - event ReserveLiquidationBonusChanged(address _reserve, uint256 _bonus); + event ReserveLiquidationBonusChanged(address asset, uint256 bonus); /** * @dev emitted when the reserve decimals are updated - * @param _reserve the address of the reserve - * @param _decimals the new decimals + * @param asset the address of the reserve + * @param decimals the new decimals **/ - event ReserveDecimalsChanged(address _reserve, uint256 _decimals); + event ReserveDecimalsChanged(address asset, uint256 decimals); /** * @dev emitted when a reserve interest strategy contract is updated - * @param _reserve the address of the reserve - * @param _strategy the new address of the interest strategy contract + * @param asset the address of the reserve + * @param strategy the new address of the interest strategy contract **/ - event ReserveInterestRateStrategyChanged(address _reserve, address _strategy); + event ReserveInterestRateStrategyChanged(address asset, address strategy); /** * @dev emitted when an aToken implementation is upgraded - * @param _reserve the address of the reserve - * @param _proxy the aToken proxy address - * @param _implementation the new aToken implementation + * @param asset the address of the reserve + * @param proxy the aToken proxy address + * @param implementation the new aToken implementation **/ - event ATokenUpgraded(address _reserve, address _proxy, address _implementation); + event ATokenUpgraded(address asset, address proxy, address implementation); /** * @dev emitted when the implementation of a stable debt token is upgraded - * @param _reserve the address of the reserve - * @param _proxy the stable debt token proxy address - * @param _implementation the new aToken implementation + * @param asset the address of the reserve + * @param proxy the stable debt token proxy address + * @param implementation the new aToken implementation **/ - event StableDebtTokenUpgraded(address _reserve, address _proxy, address _implementation); + event StableDebtTokenUpgraded(address asset, address proxy, address implementation); /** * @dev emitted when the implementation of a variable debt token is upgraded - * @param _reserve the address of the reserve + * @param asset the address of the reserve * @param _proxy the variable debt token proxy address * @param _implementation the new aToken implementation **/ - event VariableDebtTokenUpgraded(address _reserve, address _proxy, address _implementation); + event VariableDebtTokenUpgraded(address asset, address _proxy, address _implementation); - LendingPoolAddressesProvider public poolAddressesProvider; - ILendingPool public pool; + ILendingPoolAddressesProvider internal addressesProvider; + ILendingPool internal pool; /** * @dev only the lending pool manager can call functions affected by this modifier **/ modifier onlyLendingPoolManager { require( - poolAddressesProvider.getLendingPoolManager() == msg.sender, + addressesProvider.getLendingPoolManager() == msg.sender, 'The caller must be a lending pool manager' ); _; @@ -187,242 +189,235 @@ contract LendingPoolConfigurator is VersionedInitializable { return CONFIGURATOR_REVISION; } - function initialize(LendingPoolAddressesProvider _poolAddressesProvider) public initializer { - poolAddressesProvider = _poolAddressesProvider; - pool = ILendingPool(poolAddressesProvider.getLendingPool()); + function initialize(ILendingPoolAddressesProvider provider) public initializer { + addressesProvider = provider; + pool = ILendingPool(addressesProvider.getLendingPool()); } /** * @dev initializes a reserve - * @param _reserve the address of the reserve to be initialized - * @param _aTokenImpl the address of the aToken contract implementation - * @param _stableDebtTokenImpl the address of the stable debt token contract - * @param _variableDebtTokenImpl the address of the variable debt token contract - * @param _underlyingAssetDecimals the decimals of the reserve underlying asset - * @param _interestRateStrategyAddress the address of the interest rate strategy contract for this reserve + * @param asset the address of the reserve to be initialized + * @param aTokenImpl the address of the aToken contract implementation + * @param stableDebtTokenImpl the address of the stable debt token contract + * @param variableDebtTokenImpl the address of the variable debt token contract + * @param underlyingAssetDecimals the decimals of the reserve underlying asset + * @param interestRateStrategyAddress the address of the interest rate strategy contract for this reserve **/ function initReserve( - address _reserve, - address _aTokenImpl, - address _stableDebtTokenImpl, - address _variableDebtTokenImpl, - uint8 _underlyingAssetDecimals, - address _interestRateStrategyAddress + address asset, + address aTokenImpl, + address stableDebtTokenImpl, + address variableDebtTokenImpl, + uint8 underlyingAssetDecimals, + address interestRateStrategyAddress ) public onlyLendingPoolManager { address aTokenProxyAddress = _initTokenWithProxy( - _aTokenImpl, - _underlyingAssetDecimals, - IERC20Detailed(_aTokenImpl).name(), - IERC20Detailed(_aTokenImpl).symbol() + aTokenImpl, + underlyingAssetDecimals ); address stableDebtTokenProxyAddress = _initTokenWithProxy( - _stableDebtTokenImpl, - _underlyingAssetDecimals, - IERC20Detailed(_stableDebtTokenImpl).name(), - IERC20Detailed(_stableDebtTokenImpl).symbol() + stableDebtTokenImpl, + underlyingAssetDecimals ); address variableDebtTokenProxyAddress = _initTokenWithProxy( - _variableDebtTokenImpl, - _underlyingAssetDecimals, - IERC20Detailed(_variableDebtTokenImpl).name(), - IERC20Detailed(_variableDebtTokenImpl).symbol() + variableDebtTokenImpl, + underlyingAssetDecimals ); pool.initReserve( - _reserve, + asset, aTokenProxyAddress, stableDebtTokenProxyAddress, variableDebtTokenProxyAddress, - _interestRateStrategyAddress + interestRateStrategyAddress ); - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); - currentConfig.setDecimals(_underlyingAssetDecimals); + currentConfig.setDecimals(underlyingAssetDecimals); currentConfig.setActive(true); currentConfig.setFrozen(false); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); emit ReserveInitialized( - _reserve, + asset, aTokenProxyAddress, stableDebtTokenProxyAddress, variableDebtTokenProxyAddress, - _interestRateStrategyAddress + interestRateStrategyAddress ); } /** - * @dev updates the aToken implementation for the _reserve - * @param _reserve the address of the reserve to be updated - * @param _implementation the address of the new aToken implementation + * @dev updates the aToken implementation for the asset + * @param asset the address of the reserve to be updated + * @param implementation the address of the new aToken implementation **/ - function updateAToken(address _reserve, address _implementation) external onlyLendingPoolManager { - (address aTokenAddress, , ) = pool.getReserveTokensAddresses(_reserve); + function updateAToken(address asset, address implementation) external onlyLendingPoolManager { + (address aTokenAddress, , ) = pool.getReserveTokensAddresses(asset); - _upgradeTokenImplementation(_reserve, aTokenAddress, _implementation); + _upgradeTokenImplementation(asset, aTokenAddress, implementation); - emit ATokenUpgraded(_reserve, aTokenAddress, _implementation); + emit ATokenUpgraded(asset, aTokenAddress, implementation); } /** - * @dev updates the stable debt token implementation for the _reserve - * @param _reserve the address of the reserve to be updated - * @param _implementation the address of the new aToken implementation + * @dev updates the stable debt token implementation for the asset + * @param asset the address of the reserve to be updated + * @param implementation the address of the new aToken implementation **/ - function updateStableDebtToken(address _reserve, address _implementation) + function updateStableDebtToken(address asset, address implementation) external onlyLendingPoolManager { - (, address stableDebtToken, ) = pool.getReserveTokensAddresses(_reserve); + (, address stableDebtToken, ) = pool.getReserveTokensAddresses(asset); - _upgradeTokenImplementation(_reserve, stableDebtToken, _implementation); + _upgradeTokenImplementation(asset, stableDebtToken, implementation); - emit StableDebtTokenUpgraded(_reserve, stableDebtToken, _implementation); + emit StableDebtTokenUpgraded(asset, stableDebtToken, implementation); } - /** - * @dev updates the variable debt token implementation for the _reserve - * @param _reserve the address of the reserve to be updated - * @param _implementation the address of the new aToken implementation + * @dev updates the variable debt token implementation for the asset + * @param asset the address of the reserve to be updated + * @param implementation the address of the new aToken implementation **/ - function updateVariableDebtToken(address _reserve, address _implementation) + function updateVariableDebtToken(address asset, address implementation) external onlyLendingPoolManager { - (, , address variableDebtToken) = pool.getReserveTokensAddresses(_reserve); + (, , address variableDebtToken) = pool.getReserveTokensAddresses(asset); - _upgradeTokenImplementation(_reserve, variableDebtToken, _implementation); + _upgradeTokenImplementation(asset, variableDebtToken, implementation); - emit VariableDebtTokenUpgraded(_reserve, variableDebtToken, _implementation); + emit VariableDebtTokenUpgraded(asset, variableDebtToken, implementation); } /** * @dev enables borrowing on a reserve - * @param _reserve the address of the reserve - * @param _stableBorrowRateEnabled true if stable borrow rate needs to be enabled by default on this reserve + * @param asset the address of the reserve + * @param stableBorrowRateEnabled true if stable borrow rate needs to be enabled by default on this reserve **/ - function enableBorrowingOnReserve(address _reserve, bool _stableBorrowRateEnabled) + function enableBorrowingOnReserve(address asset, bool stableBorrowRateEnabled) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setBorrowingEnabled(true); - currentConfig.setStableRateBorrowingEnabled(_stableBorrowRateEnabled); + currentConfig.setStableRateBorrowingEnabled(stableBorrowRateEnabled); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit BorrowingEnabledOnReserve(_reserve, _stableBorrowRateEnabled); + emit BorrowingEnabledOnReserve(asset, stableBorrowRateEnabled); } /** * @dev disables borrowing on a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function disableBorrowingOnReserve(address _reserve) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function disableBorrowingOnReserve(address asset) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setBorrowingEnabled(false); - pool.setConfiguration(_reserve, currentConfig.data); - emit BorrowingDisabledOnReserve(_reserve); + pool.setConfiguration(asset, currentConfig.data); + emit BorrowingDisabledOnReserve(asset); } /** * @dev enables a reserve to be used as collateral - * @param _reserve the address of the reserve - * @param _baseLTVasCollateral the loan to value of the asset when used as collateral - * @param _liquidationThreshold the threshold at which loans using this asset as collateral will be considered undercollateralized - * @param _liquidationBonus the bonus liquidators receive to liquidate this asset + * @param asset the address of the reserve + * @param ltv the loan to value of the asset when used as collateral + * @param liquidationThreshold the threshold at which loans using this asset as collateral will be considered undercollateralized + * @param liquidationBonus the bonus liquidators receive to liquidate this asset **/ function enableReserveAsCollateral( - address _reserve, - uint256 _baseLTVasCollateral, - uint256 _liquidationThreshold, - uint256 _liquidationBonus + address asset, + uint256 ltv, + uint256 liquidationThreshold, + uint256 liquidationBonus ) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); - currentConfig.setLtv(_baseLTVasCollateral); - currentConfig.setLiquidationThreshold(_liquidationThreshold); - currentConfig.setLiquidationBonus(_liquidationBonus); + currentConfig.setLtv(ltv); + currentConfig.setLiquidationThreshold(liquidationThreshold); + currentConfig.setLiquidationBonus(liquidationBonus); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); emit ReserveEnabledAsCollateral( - _reserve, - _baseLTVasCollateral, - _liquidationThreshold, - _liquidationBonus + asset, + ltv, + liquidationThreshold, + liquidationBonus ); } /** * @dev disables a reserve as collateral - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function disableReserveAsCollateral(address _reserve) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function disableReserveAsCollateral(address asset) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setLtv(0); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveDisabledAsCollateral(_reserve); + emit ReserveDisabledAsCollateral(asset); } /** * @dev enable stable rate borrowing on a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function enableReserveStableRate(address _reserve) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function enableReserveStableRate(address asset) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setStableRateBorrowingEnabled(true); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit StableRateEnabledOnReserve(_reserve); + emit StableRateEnabledOnReserve(asset); } /** * @dev disable stable rate borrowing on a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function disableReserveStableRate(address _reserve) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function disableReserveStableRate(address asset) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setStableRateBorrowingEnabled(false); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit StableRateDisabledOnReserve(_reserve); + emit StableRateDisabledOnReserve(asset); } /** * @dev activates a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function activateReserve(address _reserve) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function activateReserve(address asset) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setActive(true); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveActivated(_reserve); + emit ReserveActivated(asset); } /** * @dev deactivates a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function deactivateReserve(address _reserve) external onlyLendingPoolManager { + function deactivateReserve(address asset) external onlyLendingPoolManager { ( uint256 availableLiquidity, uint256 totalBorrowsStable, @@ -434,170 +429,166 @@ contract LendingPoolConfigurator is VersionedInitializable { , , - ) = pool.getReserveData(_reserve); + ) = pool.getReserveData(asset); require( availableLiquidity == 0 && totalBorrowsStable == 0 && totalBorrowsVariable == 0, 'The liquidity of the reserve needs to be 0' ); - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setActive(false); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveDeactivated(_reserve); + emit ReserveDeactivated(asset); } /** * @dev freezes a reserve. A freezed reserve doesn't accept any new deposit, borrow or rate swap, but can accept repayments, liquidations, rate rebalances and redeems - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function freezeReserve(address _reserve) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function freezeReserve(address asset) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setFrozen(true); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveFreezed(_reserve); + emit ReserveFreezed(asset); } /** * @dev unfreezes a reserve - * @param _reserve the address of the reserve + * @param asset the address of the reserve **/ - function unfreezeReserve(address _reserve) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function unfreezeReserve(address asset) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); currentConfig.setFrozen(false); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveUnfreezed(_reserve); + emit ReserveUnfreezed(asset); } /** * @dev emitted when a reserve loan to value is updated - * @param _reserve the address of the reserve - * @param _ltv the new value for the loan to value + * @param asset the address of the reserve + * @param ltv the new value for the loan to value **/ - function setLtv(address _reserve, uint256 _ltv) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function setLtv(address asset, uint256 ltv) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); - currentConfig.setLtv(_ltv); + currentConfig.setLtv(ltv); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveBaseLtvChanged(_reserve, _ltv); + emit ReserveBaseLtvChanged(asset, ltv); } /** * @dev updates the liquidation threshold of a reserve. - * @param _reserve the address of the reserve - * @param _threshold the new value for the liquidation threshold + * @param asset the address of the reserve + * @param threshold the new value for the liquidation threshold **/ - function setLiquidationThreshold(address _reserve, uint256 _threshold) + function setLiquidationThreshold(address asset, uint256 threshold) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); - currentConfig.setLiquidationThreshold(_threshold); + currentConfig.setLiquidationThreshold(threshold); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveLiquidationThresholdChanged(_reserve, _threshold); + emit ReserveLiquidationThresholdChanged(asset, threshold); } /** * @dev updates the liquidation bonus of a reserve - * @param _reserve the address of the reserve - * @param _bonus the new value for the liquidation bonus + * @param asset the address of the reserve + * @param bonus the new value for the liquidation bonus **/ - function setLiquidationBonus(address _reserve, uint256 _bonus) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function setLiquidationBonus(address asset, uint256 bonus) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); - currentConfig.setLiquidationBonus(_bonus); + currentConfig.setLiquidationBonus(bonus); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveLiquidationBonusChanged(_reserve, _bonus); + emit ReserveLiquidationBonusChanged(asset, bonus); } /** * @dev updates the reserve decimals - * @param _reserve the address of the reserve - * @param _decimals the new number of decimals + * @param asset the address of the reserve + * @param decimals the new number of decimals **/ - function setReserveDecimals(address _reserve, uint256 _decimals) external onlyLendingPoolManager { - ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(_reserve); + function setReserveDecimals(address asset, uint256 decimals) external onlyLendingPoolManager { + ReserveConfiguration.Map memory currentConfig = pool.getConfiguration(asset); - currentConfig.setDecimals(_decimals); + currentConfig.setDecimals(decimals); - pool.setConfiguration(_reserve, currentConfig.data); + pool.setConfiguration(asset, currentConfig.data); - emit ReserveDecimalsChanged(_reserve, _decimals); + emit ReserveDecimalsChanged(asset, decimals); } /** * @dev sets the interest rate strategy of a reserve - * @param _reserve the address of the reserve - * @param _rateStrategyAddress the new address of the interest strategy contract + * @param asset the address of the reserve + * @param rateStrategyAddress the new address of the interest strategy contract **/ - function setReserveInterestRateStrategyAddress(address _reserve, address _rateStrategyAddress) + function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress) external onlyLendingPoolManager { - pool.setReserveInterestRateStrategyAddress(_reserve, _rateStrategyAddress); - emit ReserveInterestRateStrategyChanged(_reserve, _rateStrategyAddress); + pool.setReserveInterestRateStrategyAddress(asset, rateStrategyAddress); + emit ReserveInterestRateStrategyChanged(asset, rateStrategyAddress); } /** * @dev initializes a token with a proxy and a specific implementation - * @param _implementation the address of the implementation - * @param _decimals the decimals of the token - * @param _name the name of the token - * @param _symbol the symbol of the token + * @param implementation the address of the implementation + * @param decimals the decimals of the token **/ function _initTokenWithProxy( - address _implementation, - uint8 _decimals, - string memory _name, - string memory _symbol + address implementation, + uint8 decimals ) internal returns (address) { InitializableAdminUpgradeabilityProxy proxy = new InitializableAdminUpgradeabilityProxy(); bytes memory params = abi.encodeWithSignature( 'initialize(uint8,string,string)', - _decimals, - _name, - _symbol + decimals, + IERC20Detailed(implementation).name(), + IERC20Detailed(implementation).symbol() ); - proxy.initialize(_implementation, address(this), params); + proxy.initialize(implementation, address(this), params); return address(proxy); } function _upgradeTokenImplementation( - address _reserve, - address _proxy, - address _implementation + address asset, + address proxyAddress, + address implementation ) internal { InitializableAdminUpgradeabilityProxy proxy = InitializableAdminUpgradeabilityProxy( - payable(_proxy) + payable(proxyAddress) ); - (uint256 decimals, , , , , , , , , ) = pool.getReserveConfigurationData(_reserve); + (uint256 decimals, , , , , , , , , ) = pool.getReserveConfigurationData(asset); bytes memory params = abi.encodeWithSignature( 'initialize(uint8,string,string)', uint8(decimals), - IERC20Detailed(_implementation).name(), - IERC20Detailed(_implementation).symbol() + IERC20Detailed(implementation).name(), + IERC20Detailed(implementation).symbol() ); - proxy.upgradeToAndCall(_implementation, params); + proxy.upgradeToAndCall(implementation, params); } } diff --git a/contracts/lendingpool/LendingPoolLiquidationManager.sol b/contracts/lendingpool/LendingPoolLiquidationManager.sol index f42144d2..17d08a03 100644 --- a/contracts/lendingpool/LendingPoolLiquidationManager.sol +++ b/contracts/lendingpool/LendingPoolLiquidationManager.sol @@ -36,35 +36,33 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl using ReserveConfiguration for ReserveConfiguration.Map; using UserConfiguration for UserConfiguration.Map; - LendingPoolAddressesProvider public addressesProvider; + LendingPoolAddressesProvider internal addressesProvider; mapping(address => ReserveLogic.ReserveData) internal reserves; mapping(address => UserConfiguration.Map) internal usersConfig; - address[] public reservesList; + address[] internal reservesList; - uint256 constant LIQUIDATION_CLOSE_FACTOR_PERCENT = 5000; + uint256 internal constant LIQUIDATION_CLOSE_FACTOR_PERCENT = 5000; /** * @dev emitted when a borrower is liquidated - * @param _collateral the address of the collateral being liquidated - * @param _reserve the address of the reserve - * @param _user the address of the user being liquidated - * @param _purchaseAmount the total amount liquidated - * @param _liquidatedCollateralAmount the amount of collateral being liquidated - * @param _liquidator the address of the liquidator - * @param _receiveAToken true if the liquidator wants to receive aTokens, false otherwise - * @param _timestamp the timestamp of the action + * @param collateral the address of the collateral being liquidated + * @param principal the address of the reserve + * @param user the address of the user being liquidated + * @param purchaseAmount the total amount liquidated + * @param liquidatedCollateralAmount the amount of collateral being liquidated + * @param liquidator the address of the liquidator + * @param receiveAToken true if the liquidator wants to receive aTokens, false otherwise **/ event LiquidationCall( - address indexed _collateral, - address indexed _reserve, - address indexed _user, - uint256 _purchaseAmount, - uint256 _liquidatedCollateralAmount, - address _liquidator, - bool _receiveAToken, - uint256 _timestamp + address indexed collateral, + address indexed principal, + address indexed user, + uint256 purchaseAmount, + uint256 liquidatedCollateralAmount, + address liquidator, + bool receiveAToken ); enum LiquidationErrors { @@ -103,30 +101,30 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl /** * @dev users can invoke this function to liquidate an undercollateralized position. - * @param _reserve the address of the collateral to liquidated - * @param _reserve 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 + * @param collateral the address of the collateral to liquidated + * @param principal 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 _reserve, - address _user, - uint256 _purchaseAmount, - bool _receiveAToken + address collateral, + address principal, + address user, + uint256 purchaseAmount, + bool receiveAToken ) external returns (uint256, string memory) { - ReserveLogic.ReserveData storage principalReserve = reserves[_reserve]; - ReserveLogic.ReserveData storage collateralReserve = reserves[_collateral]; - UserConfiguration.Map storage userConfig = usersConfig[_user]; + ReserveLogic.ReserveData storage principalReserve = reserves[principal]; + ReserveLogic.ReserveData storage collateralReserve = reserves[collateral]; + UserConfiguration.Map storage userConfig = usersConfig[user]; LiquidationCallLocalVars memory vars; (, , , , vars.healthFactor) = GenericLogic.calculateUserAccountData( - _user, + user, reserves, - usersConfig[_user], + usersConfig[user], reservesList, addressesProvider.getPriceOracle() ); @@ -138,13 +136,13 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl ); } - vars.userCollateralBalance = IERC20(collateralReserve.aTokenAddress).balanceOf(_user); + vars.userCollateralBalance = IERC20(collateralReserve.aTokenAddress).balanceOf(user); vars.isCollateralEnabled = collateralReserve.configuration.getLiquidationThreshold() > 0 && userConfig.isUsingAsCollateral(collateralReserve.index); - //if _collateral isn't enabled as collateral by _user, it cannot be liquidated + //if collateral isn't enabled as collateral by user, it cannot be liquidated if (!vars.isCollateralEnabled) { return ( uint256(LiquidationErrors.COLLATERAL_CANNOT_BE_LIQUIDATED), @@ -152,9 +150,9 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl ); } - //if the user hasn't borrowed the specific currency defined by _reserve, it cannot be liquidated + //if the user hasn't borrowed the specific currency defined by asset, it cannot be liquidated (vars.userStableDebt, vars.userVariableDebt) = Helpers.getUserCurrentDebt( - _user, + user, principalReserve ); @@ -170,9 +168,9 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl LIQUIDATION_CLOSE_FACTOR_PERCENT ); - vars.actualAmountToLiquidate = _purchaseAmount > vars.maxPrincipalAmountToLiquidate + vars.actualAmountToLiquidate = purchaseAmount > vars.maxPrincipalAmountToLiquidate ? vars.maxPrincipalAmountToLiquidate - : _purchaseAmount; + : purchaseAmount; ( vars.maxCollateralToLiquidate, @@ -180,14 +178,14 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl ) = calculateAvailableCollateralToLiquidate( collateralReserve, principalReserve, - _collateral, - _reserve, + collateral, + principal, vars.actualAmountToLiquidate, vars.userCollateralBalance ); //if principalAmountNeeded < vars.ActualAmountToLiquidate, there isn't enough - //of _collateral to cover the actual amount that is being liquidated, hence we liquidate + //of collateral to cover the actual amount that is being liquidated, hence we liquidate //a smaller amount if (vars.principalAmountNeeded < vars.actualAmountToLiquidate) { @@ -197,8 +195,8 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl vars.collateralAtoken = IAToken(collateralReserve.aTokenAddress); //if liquidator reclaims the underlying asset, we make sure there is enough available collateral in the reserve - if (!_receiveAToken) { - uint256 currentAvailableCollateral = IERC20(_collateral).balanceOf( + if (!receiveAToken) { + uint256 currentAvailableCollateral = IERC20(collateral).balanceOf( address(vars.collateralAtoken) ); if (currentAvailableCollateral < vars.maxCollateralToLiquidate) { @@ -211,55 +209,53 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl //update the principal reserve principalReserve.updateCumulativeIndexesAndTimestamp(); - principalReserve.updateInterestRates(_reserve, vars.actualAmountToLiquidate, 0); + principalReserve.updateInterestRates(principal, vars.actualAmountToLiquidate, 0); if (vars.userVariableDebt >= vars.actualAmountToLiquidate) { IVariableDebtToken(principalReserve.variableDebtTokenAddress).burn( - _user, + user, vars.actualAmountToLiquidate ); } else { IVariableDebtToken(principalReserve.variableDebtTokenAddress).burn( - _user, + user, vars.userVariableDebt ); IStableDebtToken(principalReserve.stableDebtTokenAddress).burn( - _user, + user, vars.actualAmountToLiquidate.sub(vars.userVariableDebt) ); } //if liquidator reclaims the aToken, he receives the equivalent atoken amount - if (_receiveAToken) { - vars.collateralAtoken.transferOnLiquidation(_user, msg.sender, vars.maxCollateralToLiquidate); + if (receiveAToken) { + vars.collateralAtoken.transferOnLiquidation(user, msg.sender, vars.maxCollateralToLiquidate); } else { //otherwise receives the underlying asset //updating collateral reserve collateralReserve.updateCumulativeIndexesAndTimestamp(); - collateralReserve.updateInterestRates(_collateral, 0, vars.maxCollateralToLiquidate); + collateralReserve.updateInterestRates(collateral, 0, vars.maxCollateralToLiquidate); //burn the equivalent amount of atoken - vars.collateralAtoken.burn(_user, msg.sender, vars.maxCollateralToLiquidate); + vars.collateralAtoken.burn(user, msg.sender, vars.maxCollateralToLiquidate); } //transfers the principal currency to the aToken - IERC20(_reserve).safeTransferFrom( + IERC20(principal).safeTransferFrom( msg.sender, principalReserve.aTokenAddress, vars.actualAmountToLiquidate ); emit LiquidationCall( - _collateral, - _reserve, - _user, + collateral, + principal, + user, vars.actualAmountToLiquidate, vars.maxCollateralToLiquidate, msg.sender, - _receiveAToken, - //solium-disable-next-line - block.timestamp + receiveAToken ); return (uint256(LiquidationErrors.NO_ERROR), 'No errors'); @@ -279,20 +275,20 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl * @dev calculates how much of a specific collateral can be liquidated, given * a certain amount of principal currency. This function needs to be called after * all the checks to validate the liquidation have been performed, otherwise it might fail. - * @param _collateralAddress the collateral to be liquidated - * @param _principalAddress the principal currency to be liquidated - * @param _purchaseAmount the amount of principal being liquidated - * @param _userCollateralBalance the collatera balance for the specific _collateral asset of the user being liquidated + * @param collateralAddress the collateral to be liquidated + * @param principalAddress the principal currency to be liquidated + * @param purchaseAmount the amount of principal being liquidated + * @param userCollateralBalance the collatera balance for the specific collateral asset of the user being liquidated * @return collateralAmount the maximum amount that is possible to liquidated given all the liquidation constraints (user balance, close factor) * @return principalAmountNeeded the purchase amount **/ function calculateAvailableCollateralToLiquidate( ReserveLogic.ReserveData storage _collateralReserve, ReserveLogic.ReserveData storage _principalReserve, - address _collateralAddress, - address _principalAddress, - uint256 _purchaseAmount, - uint256 _userCollateralBalance + address collateralAddress, + address principalAddress, + uint256 purchaseAmount, + uint256 userCollateralBalance ) internal view returns (uint256, uint256) { uint256 collateralAmount = 0; uint256 principalAmountNeeded = 0; @@ -301,8 +297,8 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl // Usage of a memory struct of vars to avoid "Stack too deep" errors due to local variables AvailableCollateralToLiquidateLocalVars memory vars; - vars.collateralPrice = oracle.getAssetPrice(_collateralAddress); - vars.principalCurrencyPrice = oracle.getAssetPrice(_principalAddress); + vars.collateralPrice = oracle.getAssetPrice(collateralAddress); + vars.principalCurrencyPrice = oracle.getAssetPrice(principalAddress); (, , vars.liquidationBonus, vars.collateralDecimals) = _collateralReserve .configuration @@ -313,13 +309,13 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl //max amount of principal currency that is available for liquidation. vars.maxAmountCollateralToLiquidate = vars .principalCurrencyPrice - .mul(_purchaseAmount) + .mul(purchaseAmount) .mul(10**vars.collateralDecimals) .div(vars.collateralPrice.mul(10**vars.principalDecimals)) .percentMul(vars.liquidationBonus); - if (vars.maxAmountCollateralToLiquidate > _userCollateralBalance) { - collateralAmount = _userCollateralBalance; + if (vars.maxAmountCollateralToLiquidate > userCollateralBalance) { + collateralAmount = userCollateralBalance; principalAmountNeeded = vars .collateralPrice .mul(collateralAmount) @@ -328,7 +324,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl .percentDiv(vars.liquidationBonus); } else { collateralAmount = vars.maxAmountCollateralToLiquidate; - principalAmountNeeded = _purchaseAmount; + principalAmountNeeded = purchaseAmount; } return (collateralAmount, principalAmountNeeded); }