Merge branch 'fix/50-check-on-balancedecreaseallowed' into 'master'

Resolve "Fix: Check on balanceDecreaseAllowed() if the reserve liquidation threshold is 0, not the ltv"

Closes #50

See merge request aave-tech/protocol-v2!59
This commit is contained in:
Ernesto Boado 2020-09-28 14:37:03 +00:00
commit 479f6f6b72
3 changed files with 119 additions and 22 deletions

View File

@ -29,11 +29,10 @@ library GenericLogic {
struct balanceDecreaseAllowedLocalVars { struct balanceDecreaseAllowedLocalVars {
uint256 decimals; uint256 decimals;
uint256 ltv; uint256 liquidationThreshold;
uint256 collateralBalanceETH; uint256 collateralBalanceETH;
uint256 borrowBalanceETH; uint256 borrowBalanceETH;
uint256 currentLiquidationThreshold; uint256 avgLiquidationThreshold;
uint256 reserveLiquidationThreshold;
uint256 amountToDecreaseETH; uint256 amountToDecreaseETH;
uint256 collateralBalancefterDecrease; uint256 collateralBalancefterDecrease;
uint256 liquidationThresholdAfterDecrease; uint256 liquidationThresholdAfterDecrease;
@ -62,18 +61,15 @@ library GenericLogic {
address[] calldata reserves, address[] calldata reserves,
address oracle address oracle
) external view returns (bool) { ) external view returns (bool) {
if ( if (!userConfig.isBorrowingAny() || !userConfig.isUsingAsCollateral(reservesData[asset].id)) {
!userConfig.isBorrowingAny() ||
!userConfig.isUsingAsCollateral(reservesData[asset].id)
) {
return true; return true;
} }
balanceDecreaseAllowedLocalVars memory vars; balanceDecreaseAllowedLocalVars memory vars;
(vars.ltv, , , vars.decimals) = reservesData[asset].configuration.getParams(); (, vars.liquidationThreshold, , vars.decimals) = reservesData[asset].configuration.getParams();
if (vars.ltv == 0) { if (vars.liquidationThreshold == 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
} }
@ -81,7 +77,7 @@ library GenericLogic {
vars.collateralBalanceETH, vars.collateralBalanceETH,
vars.borrowBalanceETH, vars.borrowBalanceETH,
, ,
vars.currentLiquidationThreshold, vars.avgLiquidationThreshold,
) = calculateUserAccountData(user, reservesData, userConfig, reserves, oracle); ) = calculateUserAccountData(user, reservesData, userConfig, reserves, oracle);
@ -102,8 +98,8 @@ library GenericLogic {
vars.liquidationThresholdAfterDecrease = vars vars.liquidationThresholdAfterDecrease = vars
.collateralBalanceETH .collateralBalanceETH
.mul(vars.currentLiquidationThreshold) .mul(vars.avgLiquidationThreshold)
.sub(vars.amountToDecreaseETH.mul(vars.reserveLiquidationThreshold)) .sub(vars.amountToDecreaseETH.mul(vars.liquidationThreshold))
.div(vars.collateralBalancefterDecrease); .div(vars.collateralBalancefterDecrease);
uint256 healthFactorAfterDecrease = calculateHealthFactorFromBalances( uint256 healthFactorAfterDecrease = calculateHealthFactorFromBalances(
@ -186,7 +182,7 @@ library GenericLogic {
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.liquidationThreshold != 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
@ -254,7 +250,7 @@ library GenericLogic {
return (collateralBalanceETH.percentMul(liquidationThreshold)).wadDiv(borrowBalanceETH); return (collateralBalanceETH.percentMul(liquidationThreshold)).wadDiv(borrowBalanceETH);
} }
/** /**
* @dev calculates the equivalent amount in ETH that an user can borrow, depending on the available collateral and the * @dev calculates the equivalent amount in ETH that an user can borrow, depending on the available collateral and the
* average Loan To Value. * average Loan To Value.
* @param collateralBalanceETH the total collateral balance * @param collateralBalanceETH the total collateral balance

View File

@ -833,12 +833,13 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
await oracle.setAssetPrice(dai.address, daiPrice); await oracle.setAssetPrice(dai.address, daiPrice);
}); });
it('User 5 deposits WETH and DAI, then borrows USDC at Variable, then disables WETH as collateral', async () => { it('User 4 deposits WETH, LEND and DAI, then borrows USDC at Variable, then disables WETH as collateral', async () => {
const {pool, weth, dai, usdc, users} = testEnv; const {pool, weth, dai, usdc, users} = testEnv;
const user = users[4]; const user = users[4];
const amountWETHToDeposit = parseEther('10'); const amountWETHToDeposit = parseEther('10');
const amountDAIToDeposit = parseEther('60'); const amountDAIToDeposit = parseEther('100');
const amountToBorrow = parseUnits('65', 6);
const amountToBorrow = parseUnits('75', 6);
await weth.connect(user.signer).mint(amountWETHToDeposit); await weth.connect(user.signer).mint(amountWETHToDeposit);
await weth.connect(user.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL); await weth.connect(user.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
@ -851,8 +852,8 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
await pool.connect(user.signer).borrow(usdc.address, amountToBorrow, 2, 0, user.address); await pool.connect(user.signer).borrow(usdc.address, amountToBorrow, 2, 0, user.address);
}); });
it('Liquidator tries to liquidates User 5 USDC loan by swapping his WETH collateral, should revert due WETH collateral disabled', async () => { it('Liquidator tries to liquidate User 5 USDC loan by swapping his WETH collateral, should revert due WETH collateral disabled', async () => {
const {pool, weth, usdc, users, mockSwapAdapter, oracle} = testEnv; const {pool, weth, dai, usdc, users, mockSwapAdapter, oracle} = testEnv;
const user = users[4]; const user = users[4];
const liquidator = users[5]; const liquidator = users[5];
@ -874,6 +875,14 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
expect(wethUserDataBefore.usageAsCollateralEnabled).to.be.false; expect(wethUserDataBefore.usageAsCollateralEnabled).to.be.false;
//drop the price to set the HF below 1
const daiPrice = await oracle.getAssetPrice(dai.address);
await oracle.setAssetPrice(
dai.address,
new BigNumber(daiPrice.toString()).multipliedBy(0.9).toFixed(0)
);
// Liquidator should NOT be able to liquidate himself with WETH, even if is disabled // Liquidator should NOT be able to liquidate himself with WETH, even if is disabled
await mockSwapAdapter.setAmountToReturn(amountToRepay); await mockSwapAdapter.setAmountToReturn(amountToRepay);
await expect( await expect(
@ -888,6 +897,7 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
'0x' '0x'
) )
).to.be.revertedWith(COLLATERAL_CANNOT_BE_LIQUIDATED); ).to.be.revertedWith(COLLATERAL_CANNOT_BE_LIQUIDATED);
const repayWithCollateralTimestamp = await timeLatest(); const repayWithCollateralTimestamp = await timeLatest();
const {userData: wethUserDataAfter} = await getContractsData( const {userData: wethUserDataAfter} = await getContractsData(

View File

@ -43,7 +43,7 @@
] ]
}, },
{ {
"description": "User 1 Deposits 2 ETH, disables ETH as collateral, borrows 400 DAI (revert expected)", "description": "User 1 Deposits 2 WETH, disables WETH as collateral, borrows 400 DAI (revert expected)",
"actions": [ "actions": [
{ {
"name": "mint", "name": "mint",
@ -96,7 +96,7 @@
] ]
}, },
{ {
"description": "User 1 enables ETH as collateral, borrows 400 DAI", "description": "User 1 enables WETH as collateral, borrows 400 DAI",
"actions": [ "actions": [
{ {
"name": "setUseAsCollateral", "name": "setUseAsCollateral",
@ -120,7 +120,7 @@
] ]
}, },
{ {
"description": "User 1 disables ETH as collateral (revert expected)", "description": "User 1 disables WETH as collateral (revert expected)",
"actions": [ "actions": [
{ {
"name": "setUseAsCollateral", "name": "setUseAsCollateral",
@ -134,6 +134,97 @@
"revertMessage": "User deposit is already being used as collateral" "revertMessage": "User deposit is already being used as collateral"
} }
] ]
},
{
"description": "User 1 Deposits 1000 LEND, disables WETH as collateral. Should revert as 1000 LEND are not enough to cover the debt (revert expected)",
"actions": [
{
"name": "mint",
"args": {
"reserve": "LEND",
"amount": "1000",
"user": "1"
},
"expected": "success"
},
{
"name": "approve",
"args": {
"reserve": "LEND",
"user": "1"
},
"expected": "success"
},
{
"name": "deposit",
"args": {
"reserve": "LEND",
"amount": "1000",
"user": "1"
},
"expected": "success"
},
{
"name": "setUseAsCollateral",
"args": {
"reserve": "WETH",
"user": "1",
"useAsCollateral": "false"
},
"expected": "revert"
}
]
},
{
"description": "User 1 Deposits 64000 more LEND (enough to cover the DAI debt), disables WETH as collateral",
"actions": [
{
"name": "mint",
"args": {
"reserve": "LEND",
"amount": "64000",
"user": "1"
},
"expected": "success"
},
{
"name": "deposit",
"args": {
"reserve": "LEND",
"amount": "64000",
"user": "1"
},
"expected": "success"
},
{
"name": "setUseAsCollateral",
"args": {
"reserve": "WETH",
"user": "1",
"useAsCollateral": "false"
},
"expected": "success"
}
]
},
{
"description": "User 1 disables LEND as collateral (revert expected)",
"actions": [
{
"name": "setUseAsCollateral",
"args": {
"reserve": "LEND",
"user": "1",
"useAsCollateral": "false"
},
"expected": "User deposit is already being used as collateral"
}
]
} }
] ]
} }