Skip to content

Commit

Permalink
- Added set of usage as collateral to true on liquidator receiving aT…
Browse files Browse the repository at this point in the history
…oken.
  • Loading branch information
eboadom committed Nov 26, 2020
1 parent e9a94b2 commit 3f070d6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
39 changes: 23 additions & 16 deletions contracts/interfaces/ILendingPoolCollateralManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
pragma solidity 0.6.12;

/**
* @title ILendingPoolCollateralManager interface
* @title ILendingPoolCollateralManager
* @author Aave
* @notice Defines the actions involving management of collateral in the protocol.
**/
interface ILendingPoolCollateralManager {
/**
* @dev emitted when a borrower is liquidated
* @param collateral the address of the collateral being liquidated
* @param principal the address of the reserve
* @param user the address of the user being liquidated
* @param debtToCover the total amount liquidated
* @param liquidatedCollateralAmount the amount of collateral being liquidated
* @param liquidator the address of the liquidator
* @dev Emitted when a borrower is liquidated
* @param collateral The address of the collateral being liquidated
* @param principal The address of the reserve
* @param user The address of the user being liquidated
* @param debtToCover The total amount liquidated
* @param liquidatedCollateralAmount The amount of collateral being liquidated
* @param liquidator The address of the liquidator
* @param receiveAToken true if the liquidator wants to receive aTokens, false otherwise
**/
event LiquidationCall(
Expand All @@ -28,18 +28,25 @@ interface ILendingPoolCollateralManager {
);

/**
* @dev emitted when a user disables a reserve as collateral
* @param reserve the address of the reserve
* @param user the address of the user
* @dev Emitted when a reserve is disabled as collateral for an user
* @param reserve The address of the reserve
* @param user The address of the user
**/
event ReserveUsedAsCollateralDisabled(address indexed reserve, address indexed user);

/**
* @dev users can invoke this function to liquidate an undercollateralized position.
* @param collateral the address of the collateral to liquidated
* @param principal the address of the principal reserve
* @param user the address of the borrower
* @param debtToCover the amount of principal that the liquidator wants to repay
* @dev Emitted when a reserve is enabled as collateral for an user
* @param reserve The address of the reserve
* @param user The address of the user
**/
event ReserveUsedAsCollateralEnabled(address indexed reserve, address indexed user);

/**
* @dev Users can invoke this function to liquidate an undercollateralized position.
* @param collateral The address of the collateral to liquidated
* @param principal The address of the principal reserve
* @param user The address of the borrower
* @param debtToCover The amount of principal that the liquidator wants to repay
* @param receiveAToken true if the liquidators wants to receive the aTokens, false if
* he wants to receive the underlying asset directly
**/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contract LendingPoolCollateralManager is
uint256 maxCollateralToLiquidate;
uint256 debtAmountNeeded;
uint256 healthFactor;
uint256 liquidatorPreviousATokenBalance;
IAToken collateralAtoken;
bool isCollateralEnabled;
DataTypes.InterestRateMode borrowRateMode;
Expand Down Expand Up @@ -190,7 +191,14 @@ contract LendingPoolCollateralManager is
);

if (receiveAToken) {
vars.liquidatorPreviousATokenBalance = IERC20(vars.collateralAtoken).balanceOf(msg.sender);
vars.collateralAtoken.transferOnLiquidation(user, msg.sender, vars.maxCollateralToLiquidate);

if (vars.liquidatorPreviousATokenBalance == 0) {
DataTypes.UserConfigurationMap storage liquidatorConfig = _usersConfig[msg.sender];
liquidatorConfig.setUsingAsCollateral(collateralReserve.id, true);
emit ReserveUsedAsCollateralEnabled(collateralAsset, msg.sender);
}
} else {
collateralReserve.updateState();
collateralReserve.updateInterestRates(
Expand Down
33 changes: 19 additions & 14 deletions test/liquidation-atoken.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import BigNumber from 'bignumber.js';

import {DRE} from '../helpers/misc-utils';
import {APPROVAL_AMOUNT_LENDING_POOL, oneEther} from '../helpers/constants';
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
import {makeSuite} from './helpers/make-suite';
import {ProtocolErrors, RateMode} from '../helpers/types';
import {calcExpectedVariableDebtTokenBalance} from './helpers/utils/calculations';
import {getUserData, getReserveData} from './helpers/utils/helpers';
import { DRE } from '../helpers/misc-utils';
import { APPROVAL_AMOUNT_LENDING_POOL, oneEther } from '../helpers/constants';
import { convertToCurrencyDecimals } from '../helpers/contracts-helpers';
import { makeSuite } from './helpers/make-suite';
import { ProtocolErrors, RateMode } from '../helpers/types';
import { calcExpectedVariableDebtTokenBalance } from './helpers/utils/calculations';
import { getUserData, getReserveData } from './helpers/utils/helpers';

const chai = require('chai');
const {expect} = chai;
const { expect } = chai;

makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => {
const {
Expand All @@ -21,7 +21,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
} = ProtocolErrors;

it('LIQUIDATION - Deposits WETH, borrows DAI/Check liquidation fails because health factor is above 1', async () => {
const {dai, weth, users, pool, oracle} = testEnv;
const { dai, weth, users, pool, oracle } = testEnv;
const depositor = users[0];
const borrower = users[1];

Expand Down Expand Up @@ -80,7 +80,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
});

it('LIQUIDATION - Drop the health factor below 1', async () => {
const {dai, users, pool, oracle} = testEnv;
const { dai, users, pool, oracle } = testEnv;
const borrower = users[1];

const daiPrice = await oracle.getAssetPrice(dai.address);
Expand All @@ -99,7 +99,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
});

it('LIQUIDATION - Tries to liquidate a different currency than the loan principal', async () => {
const {pool, users, weth} = testEnv;
const { pool, users, weth } = testEnv;
const borrower = users[1];
//user 2 tries to borrow
await expect(
Expand All @@ -108,7 +108,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
});

it('LIQUIDATION - Tries to liquidate a different collateral than the borrower collateral', async () => {
const {pool, dai, users} = testEnv;
const { pool, dai, users } = testEnv;
const borrower = users[1];

await expect(
Expand All @@ -117,7 +117,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
});

it('LIQUIDATION - Liquidates the borrow', async () => {
const {pool, dai, weth, users, oracle, helpersContract} = testEnv;
const { pool, dai, weth, aWETH, aDai, users, oracle, helpersContract, deployer } = testEnv;
const borrower = users[1];

//mints dai to the caller
Expand Down Expand Up @@ -223,10 +223,15 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
new BigNumber(ethReserveDataBefore.availableLiquidity.toString()).toFixed(0),
'Invalid collateral available liquidity'
);

expect(
(await helpersContract.getUserReserveData(weth.address, deployer.address))
.usageAsCollateralEnabled
).to.be.true;
});

it('User 3 deposits 1000 USDC, user 4 1 WETH, user 4 borrows - drops HF, liquidates the borrow', async () => {
const {users, pool, usdc, oracle, weth, helpersContract} = testEnv;
const { users, pool, usdc, oracle, weth, helpersContract } = testEnv;
const depositor = users[3];
const borrower = users[4];

Expand Down

0 comments on commit 3f070d6

Please sign in to comment.