mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
Refactored code to use PercentageMath, fixed PercentageMath bug
This commit is contained in:
parent
4baf17d9e6
commit
11e06e2e92
|
@ -9,6 +9,7 @@ import '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
|
|||
import '../libraries/openzeppelin-upgradeability/VersionedInitializable.sol';
|
||||
import '../interfaces/IExchangeAdapter.sol';
|
||||
import '../libraries/UniversalERC20.sol';
|
||||
import {PercentageMath} from '../libraries/PercentageMath.sol';
|
||||
|
||||
/// @title TokenDistributor
|
||||
/// @author Aave
|
||||
|
@ -22,6 +23,7 @@ import '../libraries/UniversalERC20.sol';
|
|||
/// and burn it (sending to address(0) the tokenToBurn)
|
||||
contract TokenDistributor is ReentrancyGuard, VersionedInitializable {
|
||||
using SafeMath for uint256;
|
||||
using PercentageMath for uint256;
|
||||
using UniversalERC20 for IERC20;
|
||||
|
||||
struct Distribution {
|
||||
|
@ -126,10 +128,9 @@ contract TokenDistributor is ReentrancyGuard, VersionedInitializable {
|
|||
public
|
||||
{
|
||||
for (uint256 i = 0; i < _tokens.length; i++) {
|
||||
uint256 _amountToDistribute = _tokens[i]
|
||||
.universalBalanceOf(address(this))
|
||||
.mul(_percentages[i])
|
||||
.div(100);
|
||||
uint256 _amountToDistribute = _tokens[i].universalBalanceOf(address(this)).percentMul(
|
||||
_percentages[i]
|
||||
);
|
||||
|
||||
if (_amountToDistribute <= 0) {
|
||||
continue;
|
||||
|
|
|
@ -20,6 +20,7 @@ import '../libraries/UserLogic.sol';
|
|||
import '../libraries/ReserveLogic.sol';
|
||||
import '../libraries/UniversalERC20.sol';
|
||||
import '../libraries/ReserveConfiguration.sol';
|
||||
import {PercentageMath} from '../libraries/PercentageMath.sol';
|
||||
|
||||
/**
|
||||
* @title LendingPoolLiquidationManager contract
|
||||
|
@ -30,6 +31,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
|||
using UniversalERC20 for IERC20;
|
||||
using SafeMath for uint256;
|
||||
using WadRayMath for uint256;
|
||||
using PercentageMath for uint256;
|
||||
using Address for address;
|
||||
using ReserveLogic for ReserveLogic.ReserveData;
|
||||
using UserLogic for UserLogic.UserReserveData;
|
||||
|
@ -177,11 +179,9 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
|||
}
|
||||
|
||||
//all clear - calculate the max principal amount that can be liquidated
|
||||
vars.maxPrincipalAmountToLiquidate = vars
|
||||
.userStableDebt
|
||||
.add(vars.userVariableDebt)
|
||||
.mul(LIQUIDATION_CLOSE_FACTOR_PERCENT)
|
||||
.div(100);
|
||||
vars.maxPrincipalAmountToLiquidate = vars.userStableDebt.add(vars.userVariableDebt).percentMul(
|
||||
LIQUIDATION_CLOSE_FACTOR_PERCENT
|
||||
);
|
||||
|
||||
vars.actualAmountToLiquidate = _purchaseAmount > vars.maxPrincipalAmountToLiquidate
|
||||
? vars.maxPrincipalAmountToLiquidate
|
||||
|
@ -331,8 +331,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
|||
.mul(_purchaseAmount)
|
||||
.mul(10**vars.collateralDecimals)
|
||||
.div(vars.collateralPrice.mul(10**vars.principalDecimals))
|
||||
.mul(vars.liquidationBonus)
|
||||
.div(100);
|
||||
.percentMul(vars.liquidationBonus);
|
||||
|
||||
if (vars.maxAmountCollateralToLiquidate > _userCollateralBalance) {
|
||||
collateralAmount = _userCollateralBalance;
|
||||
|
@ -341,8 +340,7 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl
|
|||
.mul(collateralAmount)
|
||||
.mul(10**vars.principalDecimals)
|
||||
.div(vars.principalCurrencyPrice.mul(10**vars.collateralDecimals))
|
||||
.mul(100)
|
||||
.div(vars.liquidationBonus);
|
||||
.percentDiv(vars.liquidationBonus);
|
||||
} else {
|
||||
collateralAmount = vars.maxAmountCollateralToLiquidate;
|
||||
principalAmountNeeded = _purchaseAmount;
|
||||
|
|
|
@ -8,7 +8,7 @@ import {ReserveLogic} from './ReserveLogic.sol';
|
|||
import {ReserveConfiguration} from './ReserveConfiguration.sol';
|
||||
import {UserLogic} from './UserLogic.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';
|
||||
|
@ -23,6 +23,7 @@ library GenericLogic {
|
|||
using UserLogic for UserLogic.UserReserveData;
|
||||
using SafeMath for uint256;
|
||||
using WadRayMath for uint256;
|
||||
using PercentageMath for uint256;
|
||||
using ReserveConfiguration for ReserveConfiguration.Map;
|
||||
|
||||
uint256 public constant HEALTH_FACTOR_LIQUIDATION_THRESHOLD = 1e18;
|
||||
|
@ -237,7 +238,7 @@ library GenericLogic {
|
|||
) internal view returns (uint256) {
|
||||
if (borrowBalanceETH == 0) return uint256(-1);
|
||||
|
||||
return (collateralBalanceETH.mul(liquidationThreshold).div(100)).wadDiv(borrowBalanceETH);
|
||||
return (collateralBalanceETH.percentMul(liquidationThreshold)).wadDiv(borrowBalanceETH);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -254,7 +255,7 @@ library GenericLogic {
|
|||
uint256 borrowBalanceETH,
|
||||
uint256 ltv
|
||||
) external view returns (uint256) {
|
||||
uint256 availableBorrowsETH = collateralBalanceETH.mul(ltv).div(100); //ltv is in percentage
|
||||
uint256 availableBorrowsETH = collateralBalanceETH.percentMul(ltv); //ltv is in percentage
|
||||
|
||||
if (availableBorrowsETH < borrowBalanceETH) {
|
||||
return 0;
|
||||
|
|
|
@ -23,6 +23,6 @@ library PercentageMath {
|
|||
function percentDiv(uint256 value, uint256 percentage) internal pure returns (uint256) {
|
||||
uint256 halfPercentage = percentage / 2;
|
||||
|
||||
return halfPercentage.add(value.mul(PERCENTAGE_FACTOR)).div(halfPercentage);
|
||||
return halfPercentage.add(value.mul(PERCENTAGE_FACTOR)).div(percentage);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ 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';
|
||||
import {UniversalERC20} from './UniversalERC20.sol';
|
||||
import {ReserveConfiguration} from './ReserveConfiguration.sol';
|
||||
import {IPriceOracleGetter} from '../interfaces/IPriceOracleGetter.sol';
|
||||
|
@ -24,6 +25,7 @@ library ValidationLogic {
|
|||
using UserLogic for UserLogic.UserReserveData;
|
||||
using SafeMath for uint256;
|
||||
using WadRayMath for uint256;
|
||||
using PercentageMath for uint256;
|
||||
using UniversalERC20 for IERC20;
|
||||
using ReserveConfiguration for ReserveConfiguration.Map;
|
||||
|
||||
|
@ -161,7 +163,7 @@ library ValidationLogic {
|
|||
require(vars.healthFactor > GenericLogic.HEALTH_FACTOR_LIQUIDATION_THRESHOLD, '8');
|
||||
|
||||
//add the current already borrowed amount to the amount requested to calculate the total collateral needed.
|
||||
vars.amountOfCollateralNeededETH = vars.userBorrowBalanceETH.add(_amountInETH).mul(100).div(
|
||||
vars.amountOfCollateralNeededETH = vars.userBorrowBalanceETH.add(_amountInETH).percentDiv(
|
||||
vars.currentLtv
|
||||
); //LTV is calculated in percentage
|
||||
|
||||
|
@ -193,7 +195,7 @@ library ValidationLogic {
|
|||
|
||||
//calculate the max available loan size in stable rate mode as a percentage of the
|
||||
//available liquidity
|
||||
uint256 maxLoanSizeStable = vars.availableLiquidity.mul(_maxStableLoanPercent).div(100);
|
||||
uint256 maxLoanSizeStable = vars.availableLiquidity.percentMul(_maxStableLoanPercent);
|
||||
|
||||
require(_amount <= maxLoanSizeStable, '13');
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user