Skip to content

Commit

Permalink
✨ (#1303) Liquidate full amount when market is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Sep 21, 2022
1 parent 2e526f1 commit 74fbc84
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
27 changes: 13 additions & 14 deletions contracts/aave-v2/ExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
uint256 collateralTokenUnit; // The collateral token unit considering its decimals.
uint256 borrowedReserveDecimals; // The number of decimals of the borrowed asset in the reserve.
uint256 borrowedTokenUnit; // The unit of borrowed token considering its decimals.
uint256 closeFactor; // The close factor used during the liquidation.
}

// Struct to avoid stack too deep.
Expand Down Expand Up @@ -221,11 +222,13 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
_updateIndexes(_poolTokenBorrowed);
_updateIndexes(_poolTokenCollateral);

if (!borrowedMarket.isDeprecated && !_liquidationAllowed(_borrower))
revert UnauthorisedLiquidate();
LiquidateVars memory vars;
if (!borrowedMarket.isDeprecated) {
vars.closeFactor = DEFAULT_LIQUIDATION_CLOSE_FACTOR;
if (!_liquidationAllowed(_borrower)) revert UnauthorisedLiquidate();
} else vars.closeFactor = MAX_BASIS_POINTS; // Allow liquidation of the whole debt.

address tokenBorrowedAddress = market[_poolTokenBorrowed].underlyingToken;

uint256 amountToLiquidate = Math.min(
_amount,
_getUserBorrowBalanceInOf(_poolTokenBorrowed, _borrower).percentMul(
Expand All @@ -237,16 +240,13 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {

IPriceOracleGetter oracle = IPriceOracleGetter(addressesProvider.getPriceOracle());

LiquidateVars memory vars;
{
ILendingPool poolMem = pool;
(, , vars.liquidationBonus, vars.collateralReserveDecimals, ) = poolMem
.getConfiguration(tokenCollateralAddress)
.getParamsMemory();
(, , , vars.borrowedReserveDecimals, ) = poolMem
.getConfiguration(tokenBorrowedAddress)
.getParamsMemory();
}
ILendingPool poolMem = pool;
(, , vars.liquidationBonus, vars.collateralReserveDecimals, ) = poolMem
.getConfiguration(tokenCollateralAddress)
.getParamsMemory();
(, , , vars.borrowedReserveDecimals, ) = poolMem
.getConfiguration(tokenBorrowedAddress)
.getParamsMemory();

unchecked {
vars.collateralTokenUnit = 10**vars.collateralReserveDecimals;
Expand Down Expand Up @@ -316,7 +316,6 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
emit P2PBorrowDeltaUpdated(_poolToken, deltasMem.p2pBorrowDelta);

ERC20 underlyingToken = ERC20(market[_poolToken].underlyingToken);

_borrowFromPool(underlyingToken, _amount);
_supplyToPool(underlyingToken, _amount);

Expand Down
6 changes: 3 additions & 3 deletions contracts/aave-v3/ExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
uint256 borrowedTokenUnit; // The unit of borrowed token considering its decimals.
uint256 borrowedTokenPrice; // The price of the borrowed token.
uint256 amountToLiquidate; // The amount of debt token to repay.
uint256 closeFactor;
bool liquidationAllowed;
uint256 closeFactor; // The close factor used during the liquidation.
bool liquidationAllowed; // Whether the liquidation is allowed or not.
}

// Struct to avoid stack too deep.
Expand Down Expand Up @@ -232,7 +232,7 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
if (!borrowedMarket.isDeprecated) {
(vars.closeFactor, vars.liquidationAllowed) = _liquidationAllowed(_borrower);
if (!vars.liquidationAllowed) revert UnauthorisedLiquidate();
} else vars.closeFactor = DEFAULT_LIQUIDATION_CLOSE_FACTOR;
} else vars.closeFactor = MAX_BASIS_POINTS; // Allow liquidation of the whole debt.

vars.amountToLiquidate = Math.min(
_amount,
Expand Down
10 changes: 6 additions & 4 deletions contracts/compound/PositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,16 @@ contract PositionsManager is IPositionsManager, MatchingEngine {
_updateP2PIndexes(_poolTokenBorrowed);
_updateP2PIndexes(_poolTokenCollateral);

if (!borrowedMarket.isDeprecated && !_isLiquidatable(_borrower, address(0), 0, 0))
revert UnauthorisedLiquidate();
uint256 closeFactor;
if (!borrowedMarket.isDeprecated) {
closeFactor = comptroller.closeFactorMantissa();
if (!_isLiquidatable(_borrower, address(0), 0, 0)) revert UnauthorisedLiquidate();
} else closeFactor = WAD; // Allow liquidation of the whole debt.

LiquidateVars memory vars;
vars.borrowBalance = _getUserBorrowBalanceInOf(_poolTokenBorrowed, _borrower);

if (_amount > vars.borrowBalance.mul(comptroller.closeFactorMantissa()))
revert AmountAboveWhatAllowedToRepay(); // Same mechanism as Compound. Liquidator cannot repay more than part of the debt (cf close factor on Compound).
if (_amount > vars.borrowBalance.mul(closeFactor)) revert AmountAboveWhatAllowedToRepay(); // Same mechanism as Compound. Liquidator cannot repay more than part of the debt (cf close factor on Compound).

_safeRepayLogic(_poolTokenBorrowed, msg.sender, _borrower, _amount, 0);

Expand Down

0 comments on commit 74fbc84

Please sign in to comment.