diff --git a/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol b/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol index c8b9d9ec..224dd5d4 100644 --- a/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol +++ b/contracts/lendingpool/DefaultReserveInterestRateStrategy.sol @@ -1,12 +1,11 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; -import '../interfaces/IReserveInterestRateStrategy.sol'; -import '../libraries/WadRayMath.sol'; -import '../configuration/LendingPoolAddressesProvider.sol'; -import '../interfaces/ILendingRateOracle.sol'; - -import '@openzeppelin/contracts/math/SafeMath.sol'; +import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; +import {IReserveInterestRateStrategy} from '../interfaces/IReserveInterestRateStrategy.sol'; +import {WadRayMath} from '../libraries/math/WadRayMath.sol'; +import {LendingPoolAddressesProvider} from '../configuration/LendingPoolAddressesProvider.sol'; +import {ILendingRateOracle} from '../interfaces/ILendingRateOracle.sol'; /** * @title DefaultReserveInterestRateStrategy contract diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 17f958ca..4e372c7f 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -2,29 +2,28 @@ pragma solidity ^0.6.8; pragma experimental ABIEncoderV2; -import '@openzeppelin/contracts/math/SafeMath.sol'; -import '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; -import '@openzeppelin/contracts/utils/Address.sol'; -import '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; - -import '../configuration/LendingPoolAddressesProvider.sol'; -import '../tokenization/AToken.sol'; -import '../libraries/WadRayMath.sol'; -import '../libraries/ReserveLogic.sol'; -import '../libraries/Helpers.sol'; -import '../libraries/GenericLogic.sol'; -import '../libraries/ValidationLogic.sol'; -import '../libraries/ReserveConfiguration.sol'; -import '../libraries/UserConfiguration.sol'; -import '../tokenization/interfaces/IStableDebtToken.sol'; -import '../tokenization/interfaces/IVariableDebtToken.sol'; - -import '../flashloan/interfaces/IFlashLoanReceiver.sol'; -import './LendingPoolLiquidationManager.sol'; -import '../interfaces/IPriceOracleGetter.sol'; +import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; +import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; +import {Address} from '@openzeppelin/contracts/utils/Address.sol'; +import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; +import { + VersionedInitializable +} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; +import {LendingPoolAddressesProvider} from '../configuration/LendingPoolAddressesProvider.sol'; +import {AToken} from '../tokenization/AToken.sol'; +import {Helpers} from '../libraries/helpers/Helpers.sol'; +import {WadRayMath} from '../libraries/math/WadRayMath.sol'; +import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol'; +import {GenericLogic} from '../libraries/logic/GenericLogic.sol'; +import {ValidationLogic} from '../libraries/logic/ValidationLogic.sol'; +import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol'; +import {UserConfiguration} from '../libraries/configuration/UserConfiguration.sol'; +import {IStableDebtToken} from '../tokenization/interfaces/IStableDebtToken.sol'; +import {IVariableDebtToken} from '../tokenization/interfaces/IVariableDebtToken.sol'; +import {IFlashLoanReceiver} from '../flashloan/interfaces/IFlashLoanReceiver.sol'; +import {LendingPoolLiquidationManager} from './LendingPoolLiquidationManager.sol'; +import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; -import '@nomiclabs/buidler/console.sol'; /** * @title LendingPool contract @@ -394,15 +393,6 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable { ); } - /** - * @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 _onBehalfOf the address for which msg.sender is repaying. - **/ - struct RepayLocalVars { uint256 stableDebt; uint256 variableDebt; @@ -411,6 +401,14 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable { uint256 totalDebt; } + /** + * @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 _onBehalfOf the address for which msg.sender is repaying. + **/ function repay( address _reserve, uint256 _amount, @@ -848,8 +846,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable { } receive() external payable { - //only contracts can send ETH to the core - require(msg.sender.isContract(), '22'); + revert(); } /** diff --git a/contracts/lendingpool/LendingPoolConfigurator.sol b/contracts/lendingpool/LendingPoolConfigurator.sol index 2a7c0f7e..1846551b 100644 --- a/contracts/lendingpool/LendingPoolConfigurator.sol +++ b/contracts/lendingpool/LendingPoolConfigurator.sol @@ -2,15 +2,17 @@ pragma solidity ^0.6.8; pragma experimental ABIEncoderV2; -import '@openzeppelin/contracts/math/SafeMath.sol'; - -import '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; -import '../libraries/ReserveConfiguration.sol'; -import '../configuration/LendingPoolAddressesProvider.sol'; -import '../libraries/openzeppelin-upgradeability/InitializableAdminUpgradeabilityProxy.sol'; +import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; +import { + VersionedInitializable +} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; +import { + InitializableAdminUpgradeabilityProxy +} from '../libraries/openzeppelin-upgradeability/InitializableAdminUpgradeabilityProxy.sol'; +import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol'; +import {LendingPoolAddressesProvider} from '../configuration/LendingPoolAddressesProvider.sol'; import {LendingPool} from './LendingPool.sol'; import {IERC20Detailed} from '../interfaces/IERC20Detailed.sol'; -import '@nomiclabs/buidler/console.sol'; /** * @title LendingPoolConfigurator contract diff --git a/contracts/lendingpool/LendingPoolLiquidationManager.sol b/contracts/lendingpool/LendingPoolLiquidationManager.sol index cfc821d7..50a84213 100644 --- a/contracts/lendingpool/LendingPoolLiquidationManager.sol +++ b/contracts/lendingpool/LendingPoolLiquidationManager.sol @@ -1,26 +1,26 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; -import '@openzeppelin/contracts/math/SafeMath.sol'; -import '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; -import '@openzeppelin/contracts/utils/Address.sol'; -import '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; - -import '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; - -import '../configuration/LendingPoolAddressesProvider.sol'; -import '../tokenization/AToken.sol'; -import '../tokenization/interfaces/IStableDebtToken.sol'; -import '../tokenization/interfaces/IVariableDebtToken.sol'; -import '../libraries/WadRayMath.sol'; -import '../interfaces/IPriceOracleGetter.sol'; -import '../libraries/GenericLogic.sol'; -import '../libraries/Helpers.sol'; -import '../libraries/ReserveLogic.sol'; -import '../libraries/ReserveConfiguration.sol'; -import '../libraries/UserConfiguration.sol'; -import {PercentageMath} from '../libraries/PercentageMath.sol'; +import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; +import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; +import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; +import {Address} from '@openzeppelin/contracts/utils/Address.sol'; +import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; +import { + VersionedInitializable +} from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; +import {LendingPoolAddressesProvider} from '../configuration/LendingPoolAddressesProvider.sol'; +import {AToken} from '../tokenization/AToken.sol'; +import {IStableDebtToken} from '../tokenization/interfaces/IStableDebtToken.sol'; +import {IVariableDebtToken} from '../tokenization/interfaces/IVariableDebtToken.sol'; +import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; +import {GenericLogic} from '../libraries/logic/GenericLogic.sol'; +import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol'; +import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol'; +import {UserConfiguration} from '../libraries/configuration/UserConfiguration.sol'; +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'; /** @@ -86,9 +86,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl uint256 actualAmountToLiquidate; uint256 liquidationRatio; uint256 maxAmountCollateralToLiquidate; - uint256 originationFee; - uint256 feeLiquidated; - uint256 liquidatedCollateralForFee; ReserveLogic.InterestRateMode borrowRateMode; uint256 userStableRate; uint256 maxCollateralToLiquidate; diff --git a/contracts/libraries/ReserveConfiguration.sol b/contracts/libraries/configuration/ReserveConfiguration.sol similarity index 97% rename from contracts/libraries/ReserveConfiguration.sol rename to contracts/libraries/configuration/ReserveConfiguration.sol index b1ebb8e5..fe2cf490 100644 --- a/contracts/libraries/ReserveConfiguration.sol +++ b/contracts/libraries/configuration/ReserveConfiguration.sol @@ -3,11 +3,9 @@ pragma solidity ^0.6.8; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; - -import {ReserveLogic} from './ReserveLogic.sol'; -import {WadRayMath} from './WadRayMath.sol'; - -import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; +import {ReserveLogic} from '../logic/ReserveLogic.sol'; +import {WadRayMath} from '../math/WadRayMath.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; /** * @title ReserveConfiguration library diff --git a/contracts/libraries/UserConfiguration.sol b/contracts/libraries/configuration/UserConfiguration.sol similarity index 96% rename from contracts/libraries/UserConfiguration.sol rename to contracts/libraries/configuration/UserConfiguration.sol index b440ac12..9c41f332 100644 --- a/contracts/libraries/UserConfiguration.sol +++ b/contracts/libraries/configuration/UserConfiguration.sol @@ -3,9 +3,8 @@ pragma solidity ^0.6.8; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import {WadRayMath} from './WadRayMath.sol'; -import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; - +import {WadRayMath} from '../math/WadRayMath.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; /** * @title UserConfiguration library * @author Aave diff --git a/contracts/libraries/Helpers.sol b/contracts/libraries/helpers/Helpers.sol similarity index 79% rename from contracts/libraries/Helpers.sol rename to contracts/libraries/helpers/Helpers.sol index de71cfa1..8f0b2db2 100644 --- a/contracts/libraries/Helpers.sol +++ b/contracts/libraries/helpers/Helpers.sol @@ -1,10 +1,8 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; -import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import '../tokenization/base/DebtTokenBase.sol'; -import './ReserveLogic.sol'; - +import {DebtTokenBase} from '../../tokenization/base/DebtTokenBase.sol'; +import {ReserveLogic} from '../logic/ReserveLogic.sol'; /** * @title Helpers library @@ -12,7 +10,6 @@ import './ReserveLogic.sol'; * @notice Implements calculation helpers. */ library Helpers { - /** * @dev fetches the user current stable and variable debt balances * @param _user the user @@ -25,8 +22,8 @@ library Helpers { returns (uint256, uint256) { return ( - IERC20(_reserve.stableDebtTokenAddress).balanceOf(_user), - IERC20(_reserve.variableDebtTokenAddress).balanceOf(_user) + DebtTokenBase(_reserve.stableDebtTokenAddress).balanceOf(_user), + DebtTokenBase(_reserve.variableDebtTokenAddress).balanceOf(_user) ); } diff --git a/contracts/libraries/GenericLogic.sol b/contracts/libraries/logic/GenericLogic.sol similarity index 96% rename from contracts/libraries/GenericLogic.sol rename to contracts/libraries/logic/GenericLogic.sol index 00392788..c8186ce1 100644 --- a/contracts/libraries/GenericLogic.sol +++ b/contracts/libraries/logic/GenericLogic.sol @@ -4,14 +4,12 @@ pragma experimental ABIEncoderV2; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; - import {ReserveLogic} from './ReserveLogic.sol'; -import {ReserveConfiguration} from './ReserveConfiguration.sol'; -import {UserConfiguration} from './UserConfiguration.sol'; -import {WadRayMath} from './WadRayMath.sol'; -import {PercentageMath} from './PercentageMath.sol'; -import '../interfaces/IPriceOracleGetter.sol'; -import '@nomiclabs/buidler/console.sol'; +import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; +import {UserConfiguration} from '../configuration/UserConfiguration.sol'; +import {WadRayMath} from '../math/WadRayMath.sol'; +import {PercentageMath} from '../math/PercentageMath.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; /** * @title GenericLogic library diff --git a/contracts/libraries/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol similarity index 72% rename from contracts/libraries/ReserveLogic.sol rename to contracts/libraries/logic/ReserveLogic.sol index bdad33fb..48c36bc5 100644 --- a/contracts/libraries/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -3,18 +3,13 @@ pragma solidity ^0.6.8; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import {MathUtils} from './MathUtils.sol'; -import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; +import {MathUtils} from '../math/MathUtils.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; -import {IStableDebtToken} from '../tokenization/interfaces/IStableDebtToken.sol'; -import {ReserveConfiguration} from './ReserveConfiguration.sol'; - -import '../configuration/LendingPoolAddressesProvider.sol'; -import '../interfaces/ILendingRateOracle.sol'; -import '../interfaces/IReserveInterestRateStrategy.sol'; -import '../tokenization/AToken.sol'; -import './WadRayMath.sol'; -import '@nomiclabs/buidler/console.sol'; +import {IStableDebtToken} from '../../tokenization/interfaces/IStableDebtToken.sol'; +import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; +import {IReserveInterestRateStrategy} from '../../interfaces/IReserveInterestRateStrategy.sol'; +import {WadRayMath} from '../math/WadRayMath.sol'; /** * @title ReserveLogic library @@ -25,16 +20,35 @@ library ReserveLogic { using SafeMath for uint256; using WadRayMath for uint256; using SafeERC20 for IERC20; - using Address for address; + + /** + * @dev Emitted when the state of a reserve is updated + * @dev NOTE: This event replaces the Deprecated ReserveUpdated() event, which didn't emit the average stable borrow rate + * @param reserve the address of the reserve + * @param liquidityRate the new liquidity rate + * @param stableBorrowRate the new stable borrow rate + * @param averageStableBorrowRate the new average stable borrow rate + * @param variableBorrowRate the new variable borrow rate + * @param liquidityIndex the new liquidity index + * @param variableBorrowIndex the new variable borrow index + **/ + event ReserveDataUpdated( + address indexed reserve, + uint256 liquidityRate, + uint256 stableBorrowRate, + uint256 averageStableBorrowRate, + uint256 variableBorrowRate, + uint256 liquidityIndex, + uint256 variableBorrowIndex + ); + using ReserveLogic for ReserveLogic.ReserveData; using ReserveConfiguration for ReserveConfiguration.Map; enum InterestRateMode {NONE, STABLE, VARIABLE} + // refer to the whitepaper, section 1.1 basic concepts for a formal description of these properties. struct ReserveData { - /** - * @dev refer to the whitepaper, section 1.1 basic concepts for a formal description of these properties. - **/ //the liquidity index. Expressed in ray uint256 lastLiquidityCumulativeIndex; //the current supply rate. Expressed in ray @@ -45,27 +59,21 @@ library ReserveLogic { uint256 currentStableBorrowRate; //variable borrow index. Expressed in ray uint256 lastVariableBorrowCumulativeIndex; + //stores the reserve configuration ReserveConfiguration.Map configuration; - /** - * @dev address of the aToken representing the asset - **/ address payable aTokenAddress; address stableDebtTokenAddress; address variableDebtTokenAddress; - /** - * @dev address of the interest rate strategy contract - **/ address interestRateStrategyAddress; uint40 lastUpdateTimestamp; - // isStableBorrowRateEnabled = true means users can borrow at a stable rate - bool isStableBorrowRateEnabled; + //the index of the reserve in the list of the active reserves uint8 index; } /** * @dev returns the ongoing normalized income for the reserve. * a value of 1e27 means there is no income. As time passes, the income is accrued. - * A value of 2*1e27 means that the income of the reserve is double the initial amount. + * A value of 2*1e27 means for each unit of assset two units of income have been accrued. * @param _reserve the reserve object * @return the normalized income. expressed in ray **/ @@ -114,10 +122,11 @@ library ReserveLogic { * @param _self the reserve object **/ function updateCumulativeIndexesAndTimestamp(ReserveData storage _self) internal { - uint256 totalBorrows = getTotalBorrows(_self); - - if (totalBorrows > 0) { - //only cumulating if there is any income being produced + //only cumulating if there is any income being produced + if ( + IERC20(_self.variableDebtTokenAddress).totalSupply() > 0 || + IERC20(_self.stableDebtTokenAddress).totalSupply() > 0 + ) { uint256 cumulatedLiquidityInterest = MathUtils.calculateLinearInterest( _self.currentLiquidityRate, _self.lastUpdateTimestamp @@ -190,34 +199,6 @@ library ReserveLogic { _self.interestRateStrategyAddress = _interestRateStrategyAddress; } - function getTotalBorrows(ReserveData storage _self) internal view returns (uint256) { - return - IERC20(_self.stableDebtTokenAddress).totalSupply().add( - IERC20(_self.variableDebtTokenAddress).totalSupply() - ); - } - - /** - * @dev Emitted when the state of a reserve is updated - * @dev NOTE: This event replaces the Deprecated ReserveUpdated() event, which didn't emit the average stable borrow rate - * @param reserve the address of the reserve - * @param liquidityRate the new liquidity rate - * @param stableBorrowRate the new stable borrow rate - * @param averageStableBorrowRate the new average stable borrow rate - * @param variableBorrowRate the new variable borrow rate - * @param liquidityIndex the new liquidity index - * @param variableBorrowIndex the new variable borrow index - **/ - event ReserveDataUpdated( - address indexed reserve, - uint256 liquidityRate, - uint256 stableBorrowRate, - uint256 averageStableBorrowRate, - uint256 variableBorrowRate, - uint256 liquidityIndex, - uint256 variableBorrowIndex - ); - /** * @dev updates the state of the core as a result of a flashloan action * @param _reserve the address of the reserve in which the flashloan is happening @@ -232,7 +213,9 @@ library ReserveLogic { //compounding the cumulated interest _reserve.updateCumulativeIndexesAndTimestamp(); - uint256 totalLiquidityBefore = _availableLiquidityBefore.add(_reserve.getTotalBorrows()); + uint256 totalLiquidityBefore = _availableLiquidityBefore + .add(IERC20(_reserve.variableDebtTokenAddress).totalSupply()) + .add(IERC20(_reserve.stableDebtTokenAddress).totalSupply()); //compounding the received fee into the reserve _reserve.cumulateToLiquidityIndex(totalLiquidityBefore, _income); @@ -241,19 +224,6 @@ library ReserveLogic { updateInterestRates(_reserve, _reserveAddress, _income, 0); } - /** - * @dev gets the total liquidity in the reserve. The total liquidity is the balance of the core contract + total borrows - * @param _reserve the reserve address - * @return the total liquidity - **/ - function getTotalLiquidity(ReserveData storage _reserve, address _reserveAddress) - public - view - returns (uint256) - { - return IERC20(_reserveAddress).balanceOf(address(this)).add(_reserve.getTotalBorrows()); - } - /** * @dev Updates the reserve current stable borrow rate Rf, the current variable borrow rate Rv and the current liquidity rate Rl. * Also updates the lastUpdateTimestamp value. Please refer to the whitepaper for further information. @@ -298,56 +268,4 @@ library ReserveLogic { _reserve.lastVariableBorrowCumulativeIndex ); } - - /** - * @dev gets the reserve current variable borrow rate. Is the base variable borrow rate if the reserve is empty - * @param _reserve the reserve address - * @return the reserve current variable borrow rate - **/ - function getReserveCurrentVariableBorrowRate(ReserveData storage _reserve) - external - view - returns (uint256) - { - if (_reserve.currentVariableBorrowRate == 0) { - return - IReserveInterestRateStrategy(_reserve.interestRateStrategyAddress) - .getBaseVariableBorrowRate(); - } - return _reserve.currentVariableBorrowRate; - } - - /** - * @dev gets the reserve current stable borrow rate. Is the market rate if the reserve is empty - * @param _reserve the reserve address - * @return the reserve current stable borrow rate - **/ - function getReserveCurrentStableBorrowRate(ReserveData storage _reserve, uint256 _baseRate) - public - view - returns (uint256) - { - return _reserve.currentStableBorrowRate == 0 ? _baseRate : _reserve.currentStableBorrowRate; - } - - /** - * @dev returns the utilization rate U of a specific reserve - * @param _reserve the reserve for which the information is needed - * @return the utilization rate in ray - **/ - function getUtilizationRate(ReserveData storage _reserve, address _reserveAddress) - public - view - returns (uint256) - { - uint256 totalBorrows = _reserve.getTotalBorrows(); - - if (totalBorrows == 0) { - return 0; - } - - uint256 availableLiquidity = IERC20(_reserveAddress).balanceOf(address(this)); - - return totalBorrows.rayDiv(availableLiquidity.add(totalBorrows)); - } } diff --git a/contracts/libraries/ValidationLogic.sol b/contracts/libraries/logic/ValidationLogic.sol similarity index 96% rename from contracts/libraries/ValidationLogic.sol rename to contracts/libraries/logic/ValidationLogic.sol index 50d2ae16..e6e58d72 100644 --- a/contracts/libraries/ValidationLogic.sol +++ b/contracts/libraries/logic/ValidationLogic.sol @@ -4,16 +4,14 @@ pragma experimental ABIEncoderV2; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; - import {ReserveLogic} from './ReserveLogic.sol'; import {GenericLogic} from './GenericLogic.sol'; -import {WadRayMath} from './WadRayMath.sol'; -import {PercentageMath} from './PercentageMath.sol'; +import {WadRayMath} from '../math/WadRayMath.sol'; +import {PercentageMath} from '../math/PercentageMath.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; -import {ReserveConfiguration} from './ReserveConfiguration.sol'; -import {UserConfiguration} from './UserConfiguration.sol'; -import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol'; -import '@nomiclabs/buidler/console.sol'; +import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; +import {UserConfiguration} from '../configuration/UserConfiguration.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; /** * @title ReserveLogic library @@ -35,12 +33,14 @@ library ValidationLogic { * @param _amount the amount to be deposited */ function validateDeposit(ReserveLogic.ReserveData storage _reserve, uint256 _amount) - external + 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'); } /** diff --git a/contracts/libraries/MathUtils.sol b/contracts/libraries/math/MathUtils.sol similarity index 83% rename from contracts/libraries/MathUtils.sol rename to contracts/libraries/math/MathUtils.sol index 1e26366d..e8d1dcbe 100644 --- a/contracts/libraries/MathUtils.sol +++ b/contracts/libraries/math/MathUtils.sol @@ -1,8 +1,8 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; -import '@openzeppelin/contracts/math/SafeMath.sol'; -import './WadRayMath.sol'; +import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; +import {WadRayMath} from './WadRayMath.sol'; library MathUtils { using SafeMath for uint256; @@ -34,7 +34,7 @@ library MathUtils { * @dev function to calculate the interest using a compounded interest rate formula. * To avoid expensive exponentiation, the calculation is performed using a binomial approximation: * - * (1+x)^n = 1+n*x+[n/2*(n-1)]*x^2+[n/6*(n-1)*(n-2)*x^3... + * (1+x)^n = 1+n*x+[n/2*(n-1)]*x^2+[n/6*(n-1)*(n-2)*x^3... * * The approximation slightly underpays liquidity providers, with the advantage of great gas cost reductions. * The whitepaper contains reference to the approximation and a table showing the margin of error per different time periods. @@ -48,30 +48,25 @@ library MathUtils { view returns (uint256) { - //solium-disable-next-line uint256 exp = block.timestamp.sub(uint256(_lastUpdateTimestamp)); - if(exp == 0){ + if (exp == 0) { return WadRayMath.ray(); } - uint256 expMinusOne = exp.sub(1); + uint256 expMinusOne = exp.sub(1); - uint256 expMinusTwo = exp > 2 ? exp.sub(2) : 0; + uint256 expMinusTwo = exp > 2 ? exp.sub(2) : 0; uint256 ratePerSecond = _rate.div(31536000); - - uint basePowerTwo = ratePerSecond.rayMul(ratePerSecond); - uint basePowerThree = basePowerTwo.rayMul(ratePerSecond); + uint256 basePowerTwo = ratePerSecond.rayMul(ratePerSecond); + uint256 basePowerThree = basePowerTwo.rayMul(ratePerSecond); - uint256 secondTerm = exp.mul(expMinusOne).mul(basePowerTwo).div(2); uint256 thirdTerm = exp.mul(expMinusOne).mul(expMinusTwo).mul(basePowerThree).div(6); - return WadRayMath.ray().add(ratePerSecond.mul(exp)).add(secondTerm).add(thirdTerm); - } } diff --git a/contracts/libraries/PercentageMath.sol b/contracts/libraries/math/PercentageMath.sol similarity index 100% rename from contracts/libraries/PercentageMath.sol rename to contracts/libraries/math/PercentageMath.sol diff --git a/contracts/libraries/WadRayMath.sol b/contracts/libraries/math/WadRayMath.sol similarity index 97% rename from contracts/libraries/WadRayMath.sol rename to contracts/libraries/math/WadRayMath.sol index 0dc29657..becf3d75 100644 --- a/contracts/libraries/WadRayMath.sol +++ b/contracts/libraries/math/WadRayMath.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: agpl-3.0 pragma solidity ^0.6.8; -import '@openzeppelin/contracts/math/SafeMath.sol'; +import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; /** * @title WadRayMath library @@ -112,5 +112,4 @@ library WadRayMath { function wadToRay(uint256 a) internal pure returns (uint256) { return a.mul(WAD_RAY_RATIO); } - } diff --git a/contracts/tokenization/AToken.sol b/contracts/tokenization/AToken.sol index c30a07e9..0745e452 100644 --- a/contracts/tokenization/AToken.sol +++ b/contracts/tokenization/AToken.sol @@ -3,14 +3,12 @@ pragma solidity ^0.6.8; import {ERC20} from './ERC20.sol'; import {LendingPool} from '../lendingpool/LendingPool.sol'; -import {WadRayMath} from '../libraries/WadRayMath.sol'; +import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; import { VersionedInitializable } from '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; -import '@nomiclabs/buidler/console.sol'; - /** * @title Aave ERC20 AToken * diff --git a/contracts/tokenization/StableDebtToken.sol b/contracts/tokenization/StableDebtToken.sol index 6fc51b6f..796d4215 100644 --- a/contracts/tokenization/StableDebtToken.sol +++ b/contracts/tokenization/StableDebtToken.sol @@ -5,8 +5,8 @@ import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; import {Address} from '@openzeppelin/contracts/utils/Address.sol'; import {DebtTokenBase} from './base/DebtTokenBase.sol'; -import {MathUtils} from '../libraries/MathUtils.sol'; -import {WadRayMath} from '../libraries/WadRayMath.sol'; +import {MathUtils} from '../libraries/math/MathUtils.sol'; +import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {IStableDebtToken} from './interfaces/IStableDebtToken.sol'; /** diff --git a/contracts/tokenization/VariableDebtToken.sol b/contracts/tokenization/VariableDebtToken.sol index 1e25ebfe..d75630b5 100644 --- a/contracts/tokenization/VariableDebtToken.sol +++ b/contracts/tokenization/VariableDebtToken.sol @@ -1,12 +1,11 @@ pragma solidity ^0.6.0; -import '@openzeppelin/contracts/GSN/Context.sol'; -import '@openzeppelin/contracts/token/ERC20/IERC20.sol'; -import '@openzeppelin/contracts/math/SafeMath.sol'; -import '@openzeppelin/contracts/utils/Address.sol'; +import {Context} from '@openzeppelin/contracts/GSN/Context.sol'; +import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; +import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol'; +import {Address} from '@openzeppelin/contracts/utils/Address.sol'; import {DebtTokenBase} from './base/DebtTokenBase.sol'; -import {WadRayMath} from '../libraries/WadRayMath.sol'; -import '@nomiclabs/buidler/console.sol'; +import {WadRayMath} from '../libraries/math/WadRayMath.sol'; import {IVariableDebtToken} from './interfaces/IVariableDebtToken.sol'; /** diff --git a/package-lock.json b/package-lock.json index 0d69c5ed..0b5eff61 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7524,6 +7524,7 @@ "anymatch": "^2.0.0", "async-each": "^1.0.1", "braces": "^2.3.2", + "fsevents": "^1.2.7", "glob-parent": "^3.1.0", "inherits": "^2.0.3", "is-binary-path": "^1.0.0", @@ -9990,6 +9991,17 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, + "fsevents": { + "version": "1.2.13", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", + "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", + "dev": true, + "optional": true, + "requires": { + "bindings": "^1.5.0", + "nan": "^2.12.1" + } + }, "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -13213,6 +13225,7 @@ "dev": true, "optional": true, "requires": { + "scrypt": "^6.0.2", "scryptsy": "^1.2.1" } }, @@ -18633,6 +18646,16 @@ } } }, + "scrypt": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/scrypt/-/scrypt-6.0.3.tgz", + "integrity": "sha1-BOAUpWgrU/pQwtXM4WfXGcBthw0=", + "dev": true, + "optional": true, + "requires": { + "nan": "^2.0.8" + } + }, "scrypt-js": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/scrypt-js/-/scrypt-js-3.0.1.tgz",