From 76fcb20b4429db1c28142f774c627e3839115dd0 Mon Sep 17 00:00:00 2001 From: The3D Date: Thu, 20 Aug 2020 09:51:21 +0200 Subject: [PATCH] Code cleanup --- contracts/fees/FeeProvider.sol | 2 +- contracts/fees/TokenDistributor.sol | 6 +- .../DefaultReserveInterestRateStrategy.sol | 11 +- contracts/lendingpool/LendingPool.sol | 65 +++++----- .../lendingpool/LendingPoolConfigurator.sol | 16 +-- .../LendingPoolLiquidationManager.sol | 41 +++---- contracts/libraries/UserConfiguration.sol | 109 ----------------- .../ReserveConfiguration.sol | 10 +- .../configuration/UserConfiguration.sol | 113 ++++++++++++++++++ contracts/libraries/{ => helpers}/Helpers.sol | 11 +- .../libraries/{ => logic}/GenericLogic.sol | 20 ++-- .../libraries/{ => logic}/ReserveLogic.sol | 12 +- .../libraries/{ => logic}/ValidationLogic.sol | 14 +-- contracts/libraries/{ => math}/MathUtils.sol | 21 ++-- .../libraries/{ => math}/PercentageMath.sol | 0 contracts/libraries/{ => math}/WadRayMath.sol | 3 +- contracts/tokenization/AToken.sol | 4 +- contracts/tokenization/StableDebtToken.sol | 4 +- contracts/tokenization/VariableDebtToken.sol | 11 +- 19 files changed, 229 insertions(+), 244 deletions(-) delete mode 100644 contracts/libraries/UserConfiguration.sol rename contracts/libraries/{ => configuration}/ReserveConfiguration.sol (96%) create mode 100644 contracts/libraries/configuration/UserConfiguration.sol rename contracts/libraries/{ => helpers}/Helpers.sol (79%) rename contracts/libraries/{ => logic}/GenericLogic.sol (94%) rename contracts/libraries/{ => logic}/ReserveLogic.sol (95%) rename contracts/libraries/{ => logic}/ValidationLogic.sol (96%) rename contracts/libraries/{ => math}/MathUtils.sol (83%) rename contracts/libraries/{ => math}/PercentageMath.sol (100%) rename contracts/libraries/{ => math}/WadRayMath.sol (97%) diff --git a/contracts/fees/FeeProvider.sol b/contracts/fees/FeeProvider.sol index bf5342e9..65bf53b8 100644 --- a/contracts/fees/FeeProvider.sol +++ b/contracts/fees/FeeProvider.sol @@ -3,7 +3,7 @@ pragma solidity ^0.6.8; import '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; import '../interfaces/IFeeProvider.sol'; -import '../libraries/WadRayMath.sol'; +import '../libraries/math/WadRayMath.sol'; /** * @title FeeProvider contract diff --git a/contracts/fees/TokenDistributor.sol b/contracts/fees/TokenDistributor.sol index 4b322ac1..8adaf3b7 100644 --- a/contracts/fees/TokenDistributor.sol +++ b/contracts/fees/TokenDistributor.sol @@ -9,7 +9,7 @@ import '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; import '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol'; import '../interfaces/IExchangeAdapter.sol'; import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol'; -import {PercentageMath} from '../libraries/PercentageMath.sol'; +import {PercentageMath} from '../libraries/math/PercentageMath.sol'; /// @title TokenDistributor /// @author Aave @@ -128,9 +128,7 @@ contract TokenDistributor is ReentrancyGuard, VersionedInitializable { public { for (uint256 i = 0; i < _tokens.length; i++) { - uint256 _amountToDistribute = _tokens[i].balanceOf(address(this)).percentMul( - _percentages[i] - ); + uint256 _amountToDistribute = _tokens[i].balanceOf(address(this)).percentMul(_percentages[i]); if (_amountToDistribute <= 0) { continue; 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 5dc5a93e..a5a3bd38 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -2,30 +2,29 @@ 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 '../interfaces/IFeeProvider.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 {IFeeProvider} from '../interfaces/IFeeProvider.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 @@ -400,15 +399,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; @@ -417,6 +407,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, @@ -882,8 +880,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 ff0da778..45047dd8 100644 --- a/contracts/lendingpool/LendingPoolLiquidationManager.sol +++ b/contracts/lendingpool/LendingPoolLiquidationManager.sol @@ -1,27 +1,28 @@ // 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'; +import {IFeeProvider} from '../interfaces/IFeeProvider.sol'; /** * @title LendingPoolLiquidationManager contract diff --git a/contracts/libraries/UserConfiguration.sol b/contracts/libraries/UserConfiguration.sol deleted file mode 100644 index 9f7c1531..00000000 --- a/contracts/libraries/UserConfiguration.sol +++ /dev/null @@ -1,109 +0,0 @@ -// SPDX-License-Identifier: agpl-3.0 -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 {IFeeProvider} from '../interfaces/IFeeProvider.sol'; - -/** - * @title UserConfiguration library - * @author Aave - * @notice Implements the bitmap logic to handle the user configuration - */ -library UserConfiguration { - uint256 internal constant BORROWING_MASK = 0x5555555555555555555555555555555555555555555555555555555555555555; - - struct Map { - uint256 data; - } - - /** - * @dev sets if the user is borrowing the reserve identified by _reserveIndex - * @param _self the configuration object - * @param _reserveIndex the index of the reserve in the bitmap - * @param _borrowing true if the user is borrowing the reserve, false otherwise - **/ - function setBorrowing( - UserConfiguration.Map storage _self, - uint256 _reserveIndex, - bool _borrowing - ) internal { - _self.data = (_self.data & ~(1 << _reserveIndex*2)) | uint256(_borrowing ? 1 : 0) << (_reserveIndex * 2); - } - - /** - * @dev sets if the user is using as collateral the reserve identified by _reserveIndex - * @param _self the configuration object - * @param _reserveIndex the index of the reserve in the bitmap - * @param _usingAsCollateral true if the user is usin the reserve as collateral, false otherwise - **/ - function setUsingAsCollateral( - UserConfiguration.Map storage _self, - uint256 _reserveIndex, - bool _usingAsCollateral - ) internal { - _self.data = (_self.data & ~(1 << _reserveIndex*2+1)) | uint256(_usingAsCollateral ? 1 : 0) << (_reserveIndex * 2 + 1); - } - - /** - * @dev used to validate if a user has been using the reserve for borrowing or as collateral - * @param _self the configuration object - * @param _reserveIndex the index of the reserve in the bitmap - * @return true if the user has been using a reserve for borrowing or as collateral, false otherwise - **/ - function isUsingAsCollateralOrBorrowing(UserConfiguration.Map memory _self, uint256 _reserveIndex) - internal - view - returns (bool) - { - return (_self.data >> (_reserveIndex * 2)) & 3 != 0; - } - - /** - * @dev used to validate if a user has been using the reserve for borrowing - * @param _self the configuration object - * @param _reserveIndex the index of the reserve in the bitmap - * @return true if the user has been using a reserve for borrowing, false otherwise - **/ - function isBorrowing(UserConfiguration.Map memory _self, uint256 _reserveIndex) - internal - view - returns (bool) - { - return (_self.data >> (_reserveIndex * 2)) & 1 != 0; - } - - /** - * @dev used to validate if a user has been using the reserve as collateral - * @param _self the configuration object - * @param _reserveIndex the index of the reserve in the bitmap - * @return true if the user has been using a reserve as collateral, false otherwise - **/ - function isUsingAsCollateral(UserConfiguration.Map memory _self, uint256 _reserveIndex) - internal - view - returns (bool) - { - return (_self.data >> (_reserveIndex * 2 + 1)) & 1 != 0; - } - - /** - * @dev used to validate if a user has been borrowing from any reserve - * @param _self the configuration object - * @return true if the user has been borrowing any reserve, false otherwise - **/ - function isBorrowingAny(UserConfiguration.Map memory _self) internal view returns (bool) { - return _self.data & BORROWING_MASK != 0; - } - - /** - * @dev used to validate if a user has not been using any reserve - * @param _self the configuration object - * @return true if the user has been borrowing any reserve, false otherwise - **/ - function isEmpty(UserConfiguration.Map memory _self) internal view returns(bool) { - return _self.data == 0; - } -} diff --git a/contracts/libraries/ReserveConfiguration.sol b/contracts/libraries/configuration/ReserveConfiguration.sol similarity index 96% rename from contracts/libraries/ReserveConfiguration.sol rename to contracts/libraries/configuration/ReserveConfiguration.sol index 92811fa9..4a833556 100644 --- a/contracts/libraries/ReserveConfiguration.sol +++ b/contracts/libraries/configuration/ReserveConfiguration.sol @@ -3,12 +3,10 @@ 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 {IFeeProvider} from '../interfaces/IFeeProvider.sol'; +import {ReserveLogic} from '../logic/ReserveLogic.sol'; +import {WadRayMath} from '../math/WadRayMath.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; +import {IFeeProvider} from '../../interfaces/IFeeProvider.sol'; /** * @title ReserveConfiguration library diff --git a/contracts/libraries/configuration/UserConfiguration.sol b/contracts/libraries/configuration/UserConfiguration.sol new file mode 100644 index 00000000..0bac500a --- /dev/null +++ b/contracts/libraries/configuration/UserConfiguration.sol @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: agpl-3.0 +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 '../math/WadRayMath.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; +import {IFeeProvider} from '../../interfaces/IFeeProvider.sol'; + +/** + * @title UserConfiguration library + * @author Aave + * @notice Implements the bitmap logic to handle the user configuration + */ +library UserConfiguration { + uint256 internal constant BORROWING_MASK = 0x5555555555555555555555555555555555555555555555555555555555555555; + + struct Map { + uint256 data; + } + + /** + * @dev sets if the user is borrowing the reserve identified by _reserveIndex + * @param _self the configuration object + * @param _reserveIndex the index of the reserve in the bitmap + * @param _borrowing true if the user is borrowing the reserve, false otherwise + **/ + function setBorrowing( + UserConfiguration.Map storage _self, + uint256 _reserveIndex, + bool _borrowing + ) internal { + _self.data = + (_self.data & ~(1 << (_reserveIndex * 2))) | + (uint256(_borrowing ? 1 : 0) << (_reserveIndex * 2)); + } + + /** + * @dev sets if the user is using as collateral the reserve identified by _reserveIndex + * @param _self the configuration object + * @param _reserveIndex the index of the reserve in the bitmap + * @param _usingAsCollateral true if the user is usin the reserve as collateral, false otherwise + **/ + function setUsingAsCollateral( + UserConfiguration.Map storage _self, + uint256 _reserveIndex, + bool _usingAsCollateral + ) internal { + _self.data = + (_self.data & ~(1 << (_reserveIndex * 2 + 1))) | + (uint256(_usingAsCollateral ? 1 : 0) << (_reserveIndex * 2 + 1)); + } + + /** + * @dev used to validate if a user has been using the reserve for borrowing or as collateral + * @param _self the configuration object + * @param _reserveIndex the index of the reserve in the bitmap + * @return true if the user has been using a reserve for borrowing or as collateral, false otherwise + **/ + function isUsingAsCollateralOrBorrowing(UserConfiguration.Map memory _self, uint256 _reserveIndex) + internal + view + returns (bool) + { + return (_self.data >> (_reserveIndex * 2)) & 3 != 0; + } + + /** + * @dev used to validate if a user has been using the reserve for borrowing + * @param _self the configuration object + * @param _reserveIndex the index of the reserve in the bitmap + * @return true if the user has been using a reserve for borrowing, false otherwise + **/ + function isBorrowing(UserConfiguration.Map memory _self, uint256 _reserveIndex) + internal + view + returns (bool) + { + return (_self.data >> (_reserveIndex * 2)) & 1 != 0; + } + + /** + * @dev used to validate if a user has been using the reserve as collateral + * @param _self the configuration object + * @param _reserveIndex the index of the reserve in the bitmap + * @return true if the user has been using a reserve as collateral, false otherwise + **/ + function isUsingAsCollateral(UserConfiguration.Map memory _self, uint256 _reserveIndex) + internal + view + returns (bool) + { + return (_self.data >> (_reserveIndex * 2 + 1)) & 1 != 0; + } + + /** + * @dev used to validate if a user has been borrowing from any reserve + * @param _self the configuration object + * @return true if the user has been borrowing any reserve, false otherwise + **/ + function isBorrowingAny(UserConfiguration.Map memory _self) internal view returns (bool) { + return _self.data & BORROWING_MASK != 0; + } + + /** + * @dev used to validate if a user has not been using any reserve + * @param _self the configuration object + * @return true if the user has been borrowing any reserve, false otherwise + **/ + function isEmpty(UserConfiguration.Map memory _self) internal view returns (bool) { + return _self.data == 0; + } +} 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 94% rename from contracts/libraries/GenericLogic.sol rename to contracts/libraries/logic/GenericLogic.sol index a25d7805..5811fe76 100644 --- a/contracts/libraries/GenericLogic.sol +++ b/contracts/libraries/logic/GenericLogic.sol @@ -4,15 +4,13 @@ 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 {IFeeProvider} from '../interfaces/IFeeProvider.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'; +import {IFeeProvider} from '../../interfaces/IFeeProvider.sol'; /** * @title GenericLogic library @@ -60,8 +58,10 @@ library GenericLogic { address[] calldata _reserves, address _oracle ) external view returns (bool) { - - if (!_userConfig.isBorrowingAny() || !_userConfig.isUsingAsCollateral(_reservesData[_reserve].index)) { + if ( + !_userConfig.isBorrowingAny() || + !_userConfig.isUsingAsCollateral(_reservesData[_reserve].index) + ) { return true; } diff --git a/contracts/libraries/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol similarity index 95% rename from contracts/libraries/ReserveLogic.sol rename to contracts/libraries/logic/ReserveLogic.sol index 4d49b114..e637c0d1 100644 --- a/contracts/libraries/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -3,13 +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 {IReserveInterestRateStrategy} from '../interfaces/IReserveInterestRateStrategy.sol'; -import {WadRayMath} from './WadRayMath.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 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 16170de7..071eab5a 100644 --- a/contracts/libraries/ValidationLogic.sol +++ b/contracts/libraries/logic/ValidationLogic.sol @@ -4,17 +4,15 @@ 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 {IFeeProvider} from '../interfaces/IFeeProvider.sol'; -import '@nomiclabs/buidler/console.sol'; +import {ReserveConfiguration} from '../configuration/ReserveConfiguration.sol'; +import {UserConfiguration} from '../configuration/UserConfiguration.sol'; +import {IPriceOracleGetter} from '../../interfaces/IPriceOracleGetter.sol'; +import {IFeeProvider} from '../../interfaces/IFeeProvider.sol'; /** * @title ReserveLogic library 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'; /**