refactored UserLogic

This commit is contained in:
emilio 2020-08-06 09:52:15 +02:00
parent 8df5182c59
commit f9115b7030
13 changed files with 70 additions and 122 deletions

View File

@ -10,7 +10,7 @@ usePlugin('buidler-typechain');
usePlugin('solidity-coverage');
usePlugin('@nomiclabs/buidler-waffle');
usePlugin('@nomiclabs/buidler-etherscan');
usePlugin('buidler-gas-reporter');
//usePlugin('buidler-gas-reporter');
['misc'].forEach((folder) => {
const tasksPath = path.join(__dirname, 'tasks', folder);

View File

@ -12,7 +12,7 @@ import '../configuration/LendingPoolAddressesProvider.sol';
import '../tokenization/AToken.sol';
import '../libraries/WadRayMath.sol';
import '../libraries/ReserveLogic.sol';
import '../libraries/UserLogic.sol';
import '../libraries/Helpers.sol';
import '../libraries/GenericLogic.sol';
import '../libraries/ValidationLogic.sol';
import '../libraries/ReserveConfiguration.sol';
@ -38,7 +38,6 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
using WadRayMath for uint256;
using Address for address payable;
using ReserveLogic for ReserveLogic.ReserveData;
using UserLogic for UserLogic.UserReserveData;
using ReserveConfiguration for ReserveConfiguration.Map;
using UserConfiguration for UserConfiguration.Map;
@ -419,7 +418,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
RepayLocalVars memory vars;
ReserveLogic.ReserveData storage reserve = reserves[_reserve];
(vars.stableDebt, vars.variableDebt) = UserLogic.getUserCurrentDebt(_onBehalfOf, reserve);
(vars.stableDebt, vars.variableDebt) = Helpers.getUserCurrentDebt(_onBehalfOf, reserve);
vars.totalDebt = vars.stableDebt.add(vars.variableDebt);
@ -495,7 +494,7 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
function swapBorrowRateMode(address _reserve, uint256 _rateMode) external nonReentrant {
ReserveLogic.ReserveData storage reserve = reserves[_reserve];
(uint256 stableDebt, uint256 variableDebt) = UserLogic.getUserCurrentDebt(msg.sender, reserve);
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebt(msg.sender, reserve);
ReserveLogic.InterestRateMode rateMode = ReserveLogic.InterestRateMode(_rateMode);
@ -867,8 +866,8 @@ contract LendingPool is ReentrancyGuard, VersionedInitializable {
ReserveLogic.ReserveData storage reserve = reserves[_reserve];
currentATokenBalance = IERC20(reserve.aTokenAddress).balanceOf(_user);
(currentStableDebt, currentVariableDebt) = UserLogic.getUserCurrentDebt(_user, reserve);
(principalStableDebt, principalVariableDebt) = UserLogic.getUserPrincipalDebt(_user, reserve);
(currentStableDebt, currentVariableDebt) = Helpers.getUserCurrentDebt(_user, reserve);
(principalStableDebt, principalVariableDebt) = Helpers.getUserPrincipalDebt(_user, reserve);
liquidityRate = reserve.currentLiquidityRate;
stableBorrowRate = IStableDebtToken(reserve.stableDebtTokenAddress).getUserStableRate(_user);
stableRateLastUpdated = IStableDebtToken(reserve.stableDebtTokenAddress).getUserLastUpdated(

View File

@ -16,7 +16,7 @@ import '../tokenization/interfaces/IVariableDebtToken.sol';
import '../libraries/WadRayMath.sol';
import '../interfaces/IPriceOracleGetter.sol';
import '../libraries/GenericLogic.sol';
import '../libraries/UserLogic.sol';
import '../libraries/Helpers.sol';
import '../libraries/ReserveLogic.sol';
import '../libraries/UniversalERC20.sol';
import '../libraries/ReserveConfiguration.sol';
@ -35,7 +35,6 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
using PercentageMath for uint256;
using Address for address;
using ReserveLogic for ReserveLogic.ReserveData;
using UserLogic for UserLogic.UserReserveData;
using ReserveConfiguration for ReserveConfiguration.Map;
using UserConfiguration for UserConfiguration.Map;
@ -161,7 +160,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
}
//if the user hasn't borrowed the specific currency defined by _reserve, it cannot be liquidated
(vars.userStableDebt, vars.userVariableDebt) = UserLogic.getUserCurrentDebt(
(vars.userStableDebt, vars.userVariableDebt) = Helpers.getUserCurrentDebt(
_user,
principalReserve
);

View File

@ -8,7 +8,6 @@ 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 {UserLogic} from './UserLogic.sol';
import {WadRayMath} from './WadRayMath.sol';
import {PercentageMath} from './PercentageMath.sol';
import '../interfaces/IPriceOracleGetter.sol';
@ -22,7 +21,6 @@ import '@nomiclabs/buidler/console.sol';
*/
library GenericLogic {
using ReserveLogic for ReserveLogic.ReserveData;
using UserLogic for UserLogic.UserReserveData;
using SafeMath for uint256;
using WadRayMath for uint256;
using PercentageMath for uint256;

View File

@ -0,0 +1,49 @@
// 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';
/**
* @title Helpers library
* @author Aave
* @notice Implements calculation helpers.
*/
library Helpers {
/**
* @dev fetches the user current stable and variable debt balances
* @param _user the user
* @param _reserve the reserve object
* @return the stable and variable debt balance
**/
function getUserCurrentDebt(address _user, ReserveLogic.ReserveData storage _reserve)
internal
view
returns (uint256, uint256)
{
return (
IERC20(_reserve.stableDebtTokenAddress).balanceOf(_user),
IERC20(_reserve.variableDebtTokenAddress).balanceOf(_user)
);
}
/**
* @dev fetches the user principal stable and variable debt balances
* @param _user the user
* @param _reserve the reserve object
* @return the stable and variable debt balance
**/
function getUserPrincipalDebt(address _user, ReserveLogic.ReserveData storage _reserve)
internal
view
returns (uint256, uint256)
{
return (
DebtTokenBase(_reserve.stableDebtTokenAddress).principalBalanceOf(_user),
DebtTokenBase(_reserve.variableDebtTokenAddress).principalBalanceOf(_user)
);
}
}

View File

@ -5,7 +5,6 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {ReserveLogic} from './ReserveLogic.sol';
import {UserLogic} from './UserLogic.sol';
import {WadRayMath} from './WadRayMath.sol';
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';

View File

@ -3,8 +3,6 @@ pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {UserLogic} from './UserLogic.sol';
import {MathUtils} from './MathUtils.sol';
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
import {UniversalERC20} from './UniversalERC20.sol';
@ -28,7 +26,6 @@ library ReserveLogic {
using WadRayMath for uint256;
using UniversalERC20 for IERC20;
using Address for address;
using UserLogic for UserLogic.UserReserveData;
using ReserveLogic for ReserveLogic.ReserveData;
using ReserveConfiguration for ReserveConfiguration.Map;

View File

@ -3,10 +3,7 @@ pragma solidity ^0.6.8;
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {UserLogic} from './UserLogic.sol';
import {WadRayMath} from './WadRayMath.sol';
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
import {IFeeProvider} from '../interfaces/IFeeProvider.sol';

View File

@ -1,84 +0,0 @@
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '../tokenization/base/DebtTokenBase.sol';
import './ReserveLogic.sol';
import './ReserveConfiguration.sol';
import './UserConfiguration.sol';
/**
* @title UserLogic library
* @author Aave
* @notice Implements user specific logic.
*/
library UserLogic {
using SafeMath for uint256;
using ReserveConfiguration for ReserveConfiguration.Map;
struct UserReserveData {
//defines if a specific deposit should or not be used as a collateral in borrows
UserConfiguration.Map configuration;
bool useAsCollateral;
}
/**
* @dev checks if a user is allowed to borrow at a stable rate
* @param _reserve the reserve address
* @param _user the user
* @param _amount the amount the the user wants to borrow
* @return true if the user is allowed to borrow at a stable rate, false otherwise
**/
function isAllowedToBorrowAtStable(
UserReserveData storage _user,
ReserveLogic.ReserveData storage _reserve,
address _userAddress,
uint256 _amount
) external view returns (bool) {
if (!_reserve.isStableBorrowRateEnabled) return false;
return
!_user.useAsCollateral ||
_reserve.configuration.getLtv() == 0 ||
_amount > IERC20(_reserve.aTokenAddress).balanceOf(_userAddress);
}
/**
* @dev fetches the user current stable and variable debt balances
* @param _user the user
* @param _reserve the reserve object
* @return the stable and variable debt balance
**/
function getUserCurrentDebt(address _user, ReserveLogic.ReserveData storage _reserve)
internal
view
returns (uint256, uint256)
{
return (
IERC20(_reserve.stableDebtTokenAddress).balanceOf(_user),
IERC20(_reserve.variableDebtTokenAddress).balanceOf(_user)
);
}
/**
* @dev fetches the user principal stable and variable debt balances
* @param _user the user
* @param _reserve the reserve object
* @return the stable and variable debt balance
**/
function getUserPrincipalDebt(address _user, ReserveLogic.ReserveData storage _reserve)
internal
view
returns (uint256, uint256)
{
return (
DebtTokenBase(_reserve.stableDebtTokenAddress).principalBalanceOf(_user),
DebtTokenBase(_reserve.variableDebtTokenAddress).principalBalanceOf(_user)
);
}
}

View File

@ -6,7 +6,6 @@ import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {ReserveLogic} from './ReserveLogic.sol';
import {UserLogic} from './UserLogic.sol';
import {GenericLogic} from './GenericLogic.sol';
import {WadRayMath} from './WadRayMath.sol';
import {PercentageMath} from './PercentageMath.sol';
@ -24,7 +23,6 @@ import '@nomiclabs/buidler/console.sol';
*/
library ValidationLogic {
using ReserveLogic for ReserveLogic.ReserveData;
using UserLogic for UserLogic.UserReserveData;
using SafeMath for uint256;
using WadRayMath for uint256;
using PercentageMath for uint256;

View File

@ -70,7 +70,7 @@
},
"PriceOracle": {
"buidlerevm": {
"address": "0x85bdE212E66e2BAE510E44Ed59116c1eC712795b",
"address": "0xE4C10Db67595aF2Cb4166c8C274e0140f7E43059",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -80,7 +80,7 @@
},
"MockAggregator": {
"buidlerevm": {
"address": "0xAF6BA11790D1942625C0c2dA07da19AB63845cfF",
"address": "0xEC1C93A9f6a9e18E97784c76aC52053587FcDB89",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -90,7 +90,7 @@
},
"ChainlinkProxyPriceProvider": {
"buidlerevm": {
"address": "0xD83D2773a7873ae2b5f8Fb92097e20a8C64F691E",
"address": "0x7B6C3e5486D9e6959441ab554A889099eed76290",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -100,7 +100,7 @@
},
"LendingRateOracle": {
"buidlerevm": {
"address": "0xf91aC1098F3b154671Ce83290114aaE45ac0225f",
"address": "0xD83D2773a7873ae2b5f8Fb92097e20a8C64F691E",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -110,7 +110,7 @@
},
"DefaultReserveInterestRateStrategy": {
"buidlerevm": {
"address": "0x2dD8146Ad2138ac61F93E549f8F917927B47E28a",
"address": "0x63b9792E7A95e3aa037255E8cAa0Dfd76f7383e7",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -146,7 +146,7 @@
},
"TokenDistributor": {
"buidlerevm": {
"address": "0xC5f7aC6895DcB76877E71db756433fB0E0478FEB"
"address": "0x1bb3d8FA7bDa74Af0D64d348a2545E7570863fA8"
},
"localhost": {
"address": "0xf2923EBa2C4AF250D93e8201Bc20a0096B3A8f89"
@ -154,7 +154,7 @@
},
"InitializableAdminUpgradeabilityProxy": {
"buidlerevm": {
"address": "0xC5f7aC6895DcB76877E71db756433fB0E0478FEB",
"address": "0x1bb3d8FA7bDa74Af0D64d348a2545E7570863fA8",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -164,7 +164,7 @@
},
"MockFlashLoanReceiver": {
"buidlerevm": {
"address": "0x24E420B42971372F060a93129846761F354Bc50B"
"address": "0xC5f7aC6895DcB76877E71db756433fB0E0478FEB"
},
"localhost": {
"address": "0x9D72c382e918491A463157Ea3e7633FE0F26F83d"
@ -172,7 +172,7 @@
},
"WalletBalanceProvider": {
"buidlerevm": {
"address": "0x285671fF5C8172dE63cF5eA264B2e827aDBC6740",
"address": "0x51fa472EB89c046484B037B6125CF843C9d41b44",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -412,7 +412,7 @@
},
"AaveProtocolTestHelpers": {
"buidlerevm": {
"address": "0xb840b4fe440b5E26e1840cd2D6320FAda1C0ca5d"
"address": "0x4b2c297ba5be42610994974b9543D56B864CA011"
},
"localhost": {
"address": "0x49CC1e6749f45e3BaB945B96c0d6723a606BDcDa"
@ -420,7 +420,7 @@
},
"StableDebtToken": {
"buidlerevm": {
"address": "0xBdFE372Bb5a0db801A1a17796EC5cfF2F30A714C",
"address": "0xc783bfC59158E888dA3E9c7768aaDC7a58ee7809",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {
@ -430,7 +430,7 @@
},
"VariableDebtToken": {
"buidlerevm": {
"address": "0xD325d114a728C2114Bd33Ad47152f790f2a29c5c",
"address": "0xBdFE372Bb5a0db801A1a17796EC5cfF2F30A714C",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
},
"localhost": {

View File

@ -118,7 +118,6 @@ const deployLibrary = async (libraryId: eContractid) => {
};
export const linkLibrariesToArtifact = async (artifact: Artifact) => {
const userLogic = await deployLibrary(eContractid.UserLogic);
const reserveLogic = await deployLibrary(eContractid.ReserveLogic);
const genericLogicArtifact = await readArtifact(
@ -127,7 +126,6 @@ export const linkLibrariesToArtifact = async (artifact: Artifact) => {
);
const linkedGenericLogicByteCode = linkBytecode(genericLogicArtifact, {
[eContractid.UserLogic]: userLogic.address,
[eContractid.ReserveLogic]: reserveLogic.address,
});
@ -144,7 +142,6 @@ export const linkLibrariesToArtifact = async (artifact: Artifact) => {
);
const linkedValidationLogicByteCode = linkBytecode(validationLogicArtifact, {
[eContractid.UserLogic]: userLogic.address,
[eContractid.ReserveLogic]: reserveLogic.address,
[eContractid.GenericLogic]: genericLogic.address,
});
@ -157,7 +154,6 @@ export const linkLibrariesToArtifact = async (artifact: Artifact) => {
const validationLogic = await (await validationLogicFactory.deploy()).deployed();
const linkedBytecode = linkBytecode(artifact, {
[eContractid.UserLogic]: userLogic.address,
[eContractid.ReserveLogic]: reserveLogic.address,
[eContractid.GenericLogic]: genericLogic.address,
[eContractid.ValidationLogic]: validationLogic.address,

View File

@ -12,7 +12,7 @@ BigNumber.config({DECIMAL_PLACES: 0, ROUNDING_MODE: BigNumber.ROUND_DOWN});
const scenarioFolder = './test/helpers/scenarios/';
const selectedScenarios: string[] = ['deposit.json','redeem.json','borrow-repay-stable.json', 'borrow-repay-variable.json'];
const selectedScenarios: string[] = [];
fs.readdirSync(scenarioFolder).forEach((file) => {
if (selectedScenarios.length > 0 && !selectedScenarios.includes(file)) return;