mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Fixed GenericLogic
This commit is contained in:
parent
e948101f87
commit
86541b3fe5
|
@ -43,23 +43,27 @@ library GenericLogic {
|
||||||
/**
|
/**
|
||||||
* @dev check if a specific balance decrease is allowed
|
* @dev check if a specific balance decrease is allowed
|
||||||
* (i.e. doesn't bring the user borrow position health factor under HEALTH_FACTOR_LIQUIDATION_THRESHOLD)
|
* (i.e. doesn't bring the user borrow position health factor under HEALTH_FACTOR_LIQUIDATION_THRESHOLD)
|
||||||
* @param _reserve the address of the reserve
|
* @param asset the address of the reserve
|
||||||
* @param _user the address of the user
|
* @param user the address of the user
|
||||||
* @param _amount the amount to decrease
|
* @param amount the amount to decrease
|
||||||
|
* @param reservesData the data of all the reserves
|
||||||
|
* @param userConfig the user configuration
|
||||||
|
* @param reserves the list of all the active reserves
|
||||||
|
* @param oracle the address of the oracle contract
|
||||||
* @return true if the decrease of the balance is allowed
|
* @return true if the decrease of the balance is allowed
|
||||||
**/
|
**/
|
||||||
function balanceDecreaseAllowed(
|
function balanceDecreaseAllowed(
|
||||||
address _reserve,
|
address asset,
|
||||||
address _user,
|
address user,
|
||||||
uint256 _amount,
|
uint256 amount,
|
||||||
mapping(address => ReserveLogic.ReserveData) storage _reservesData,
|
mapping(address => ReserveLogic.ReserveData) storage reservesData,
|
||||||
UserConfiguration.Map calldata _userConfig,
|
UserConfiguration.Map calldata userConfig,
|
||||||
address[] calldata _reserves,
|
address[] calldata reserves,
|
||||||
address _oracle
|
address oracle
|
||||||
) external view returns (bool) {
|
) external view returns (bool) {
|
||||||
if (
|
if (
|
||||||
!_userConfig.isBorrowingAny() ||
|
!userConfig.isBorrowingAny() ||
|
||||||
!_userConfig.isUsingAsCollateral(_reservesData[_reserve].index)
|
!userConfig.isUsingAsCollateral(reservesData[asset].index)
|
||||||
) {
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -67,7 +71,7 @@ library GenericLogic {
|
||||||
// Usage of a memory struct of vars to avoid "Stack too deep" errors due to local variables
|
// Usage of a memory struct of vars to avoid "Stack too deep" errors due to local variables
|
||||||
balanceDecreaseAllowedLocalVars memory vars;
|
balanceDecreaseAllowedLocalVars memory vars;
|
||||||
|
|
||||||
(vars.ltv, , , vars.decimals) = _reservesData[_reserve].configuration.getParams();
|
(vars.ltv, , , vars.decimals) = reservesData[asset].configuration.getParams();
|
||||||
|
|
||||||
if (vars.ltv == 0) {
|
if (vars.ltv == 0) {
|
||||||
return true; //if reserve is not used as collateral, no reasons to block the transfer
|
return true; //if reserve is not used as collateral, no reasons to block the transfer
|
||||||
|
@ -79,13 +83,13 @@ library GenericLogic {
|
||||||
,
|
,
|
||||||
vars.currentLiquidationThreshold,
|
vars.currentLiquidationThreshold,
|
||||||
|
|
||||||
) = calculateUserAccountData(_user, _reservesData, _userConfig, _reserves, _oracle);
|
) = calculateUserAccountData(user, reservesData, userConfig, reserves, oracle);
|
||||||
|
|
||||||
if (vars.borrowBalanceETH == 0) {
|
if (vars.borrowBalanceETH == 0) {
|
||||||
return true; //no borrows - no reasons to block the transfer
|
return true; //no borrows - no reasons to block the transfer
|
||||||
}
|
}
|
||||||
|
|
||||||
vars.amountToDecreaseETH = IPriceOracleGetter(_oracle).getAssetPrice(_reserve).mul(_amount).div(
|
vars.amountToDecreaseETH = IPriceOracleGetter(oracle).getAssetPrice(asset).mul(amount).div(
|
||||||
10**vars.decimals
|
10**vars.decimals
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -137,20 +141,20 @@ library GenericLogic {
|
||||||
* @dev calculates the user data across the reserves.
|
* @dev calculates the user data across the reserves.
|
||||||
* this includes the total liquidity/collateral/borrow balances in ETH,
|
* this includes the total liquidity/collateral/borrow balances in ETH,
|
||||||
* the average Loan To Value, the average Liquidation Ratio, and the Health factor.
|
* the average Loan To Value, the average Liquidation Ratio, and the Health factor.
|
||||||
* @param _user the address of the user
|
* @param user the address of the user
|
||||||
* @param _reservesData data of all the reserves
|
* @param reservesData data of all the reserves
|
||||||
* @param _userConfig the configuration of the user
|
* @param userConfig the configuration of the user
|
||||||
* @param _reserves the list of the available reserves
|
* @param reserves the list of the available reserves
|
||||||
* @param _oracle the price oracle address
|
* @param oracle the price oracle address
|
||||||
* @return the total collateral and total borrow balance of the user in ETH, the avg ltv and liquidation threshold and the HF
|
* @return the total collateral and total borrow balance of the user in ETH, the avg ltv and liquidation threshold and the HF
|
||||||
* also the average Ltv, liquidation threshold, and the health factor
|
* also the average Ltv, liquidation threshold, and the health factor
|
||||||
**/
|
**/
|
||||||
function calculateUserAccountData(
|
function calculateUserAccountData(
|
||||||
address _user,
|
address user,
|
||||||
mapping(address => ReserveLogic.ReserveData) storage _reservesData,
|
mapping(address => ReserveLogic.ReserveData) storage reservesData,
|
||||||
UserConfiguration.Map memory _userConfig,
|
UserConfiguration.Map memory userConfig,
|
||||||
address[] memory _reserves,
|
address[] memory reserves,
|
||||||
address _oracle
|
address oracle
|
||||||
)
|
)
|
||||||
internal
|
internal
|
||||||
view
|
view
|
||||||
|
@ -164,26 +168,26 @@ library GenericLogic {
|
||||||
{
|
{
|
||||||
CalculateUserAccountDataVars memory vars;
|
CalculateUserAccountDataVars memory vars;
|
||||||
|
|
||||||
if (_userConfig.isEmpty()) {
|
if (userConfig.isEmpty()) {
|
||||||
return (0, 0, 0, 0, uint256(-1));
|
return (0, 0, 0, 0, uint256(-1));
|
||||||
}
|
}
|
||||||
for (vars.i = 0; vars.i < _reserves.length; vars.i++) {
|
for (vars.i = 0; vars.i < reserves.length; vars.i++) {
|
||||||
if (!_userConfig.isUsingAsCollateralOrBorrowing(vars.i)) {
|
if (!userConfig.isUsingAsCollateralOrBorrowing(vars.i)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
vars.currentReserveAddress = _reserves[vars.i];
|
vars.currentReserveAddress = reserves[vars.i];
|
||||||
ReserveLogic.ReserveData storage currentReserve = _reservesData[vars.currentReserveAddress];
|
ReserveLogic.ReserveData storage currentReserve = reservesData[vars.currentReserveAddress];
|
||||||
|
|
||||||
(vars.ltv, vars.liquidationThreshold, , vars.decimals) = currentReserve
|
(vars.ltv, vars.liquidationThreshold, , vars.decimals) = currentReserve
|
||||||
.configuration
|
.configuration
|
||||||
.getParams();
|
.getParams();
|
||||||
|
|
||||||
vars.tokenUnit = 10**vars.decimals;
|
vars.tokenUnit = 10**vars.decimals;
|
||||||
vars.reserveUnitPrice = IPriceOracleGetter(_oracle).getAssetPrice(vars.currentReserveAddress);
|
vars.reserveUnitPrice = IPriceOracleGetter(oracle).getAssetPrice(vars.currentReserveAddress);
|
||||||
|
|
||||||
if (vars.ltv != 0 && _userConfig.isUsingAsCollateral(vars.i)) {
|
if (vars.ltv != 0 && userConfig.isUsingAsCollateral(vars.i)) {
|
||||||
vars.compoundedLiquidityBalance = IERC20(currentReserve.aTokenAddress).balanceOf(_user);
|
vars.compoundedLiquidityBalance = IERC20(currentReserve.aTokenAddress).balanceOf(user);
|
||||||
|
|
||||||
uint256 liquidityBalanceETH = vars
|
uint256 liquidityBalanceETH = vars
|
||||||
.reserveUnitPrice
|
.reserveUnitPrice
|
||||||
|
@ -198,12 +202,12 @@ library GenericLogic {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_userConfig.isBorrowing(vars.i)) {
|
if (userConfig.isBorrowing(vars.i)) {
|
||||||
vars.compoundedBorrowBalance = IERC20(currentReserve.stableDebtTokenAddress).balanceOf(
|
vars.compoundedBorrowBalance = IERC20(currentReserve.stableDebtTokenAddress).balanceOf(
|
||||||
_user
|
user
|
||||||
);
|
);
|
||||||
vars.compoundedBorrowBalance = vars.compoundedBorrowBalance.add(
|
vars.compoundedBorrowBalance = vars.compoundedBorrowBalance.add(
|
||||||
IERC20(currentReserve.variableDebtTokenAddress).balanceOf(_user)
|
IERC20(currentReserve.variableDebtTokenAddress).balanceOf(user)
|
||||||
);
|
);
|
||||||
|
|
||||||
vars.totalBorrowBalanceETH = vars.totalBorrowBalanceETH.add(
|
vars.totalBorrowBalanceETH = vars.totalBorrowBalanceETH.add(
|
||||||
|
@ -244,7 +248,7 @@ library GenericLogic {
|
||||||
uint256 collateralBalanceETH,
|
uint256 collateralBalanceETH,
|
||||||
uint256 borrowBalanceETH,
|
uint256 borrowBalanceETH,
|
||||||
uint256 liquidationThreshold
|
uint256 liquidationThreshold
|
||||||
) internal view returns (uint256) {
|
) internal pure returns (uint256) {
|
||||||
if (borrowBalanceETH == 0) return uint256(-1);
|
if (borrowBalanceETH == 0) return uint256(-1);
|
||||||
|
|
||||||
return (collateralBalanceETH.percentMul(liquidationThreshold)).wadDiv(borrowBalanceETH);
|
return (collateralBalanceETH.percentMul(liquidationThreshold)).wadDiv(borrowBalanceETH);
|
||||||
|
@ -263,7 +267,7 @@ library GenericLogic {
|
||||||
uint256 collateralBalanceETH,
|
uint256 collateralBalanceETH,
|
||||||
uint256 borrowBalanceETH,
|
uint256 borrowBalanceETH,
|
||||||
uint256 ltv
|
uint256 ltv
|
||||||
) external view returns (uint256) {
|
) internal pure returns (uint256) {
|
||||||
uint256 availableBorrowsETH = collateralBalanceETH.percentMul(ltv); //ltv is in percentage
|
uint256 availableBorrowsETH = collateralBalanceETH.percentMul(ltv); //ltv is in percentage
|
||||||
|
|
||||||
if (availableBorrowsETH < borrowBalanceETH) {
|
if (availableBorrowsETH < borrowBalanceETH) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user