Skip to content

Commit

Permalink
🩹 (aave-3) Don't repay too much on liquidation #1073
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Jul 11, 2022
1 parent 31b2dda commit 125c654
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
24 changes: 17 additions & 7 deletions contracts/aave-v2/ExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,25 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
vars.borrowedTokenUnit = 10**vars.borrowedReserveDecimals;
}

uint256 amountToSeize = Math.min(
((amountToLiquidate *
oracle.getAssetPrice(tokenBorrowedAddress) *
vars.collateralTokenUnit) /
(vars.borrowedTokenUnit * oracle.getAssetPrice(tokenCollateralAddress)))
.percentMul(vars.liquidationBonus), // Same mechanism as Aave.
_getUserSupplyBalanceInOf(_poolTokenCollateralAddress, _borrower)
uint256 borrowedTokenPrice = oracle.getAssetPrice(tokenBorrowedAddress);
uint256 collateralPrice = oracle.getAssetPrice(tokenCollateralAddress);
uint256 amountToSeize = ((amountToLiquidate *
borrowedTokenPrice *
vars.collateralTokenUnit) / (vars.borrowedTokenUnit * collateralPrice))
.percentMul(vars.liquidationBonus);

uint256 collateralBalance = _getUserSupplyBalanceInOf(
_poolTokenCollateralAddress,
_borrower
);

if (amountToSeize > collateralBalance) {
amountToSeize = collateralBalance;
amountToLiquidate = ((collateralBalance * collateralPrice * vars.borrowedTokenUnit) /
(borrowedTokenPrice * vars.collateralTokenUnit))
.percentDiv(vars.liquidationBonus);
}

_safeRepayLogic(_poolTokenBorrowedAddress, msg.sender, _borrower, amountToLiquidate, 0);
_safeWithdrawLogic(_poolTokenCollateralAddress, amountToSeize, _borrower, msg.sender, 0);

Expand Down
24 changes: 17 additions & 7 deletions contracts/aave-v3/ExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,25 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
vars.borrowedTokenUnit = 10**vars.borrowedReserveDecimals;
}

uint256 amountToSeize = Math.min(
((amountToLiquidate *
oracle.getAssetPrice(tokenBorrowedAddress) *
vars.collateralTokenUnit) /
(vars.borrowedTokenUnit * oracle.getAssetPrice(tokenCollateralAddress)))
.percentMul(vars.liquidationBonus), // Same mechanism as Aave.
_getUserSupplyBalanceInOf(_poolTokenCollateralAddress, _borrower)
uint256 borrowedTokenPrice = oracle.getAssetPrice(tokenBorrowedAddress);
uint256 collateralPrice = oracle.getAssetPrice(tokenCollateralAddress);
uint256 amountToSeize = ((amountToLiquidate *
borrowedTokenPrice *
vars.collateralTokenUnit) / (vars.borrowedTokenUnit * collateralPrice))
.percentMul(vars.liquidationBonus);

uint256 collateralBalance = _getUserSupplyBalanceInOf(
_poolTokenCollateralAddress,
_borrower
);

if (amountToSeize > collateralBalance) {
amountToSeize = collateralBalance;
amountToLiquidate = ((collateralBalance * collateralPrice * vars.borrowedTokenUnit) /
(borrowedTokenPrice * vars.collateralTokenUnit))
.percentDiv(vars.liquidationBonus);
}

_safeRepayLogic(_poolTokenBorrowedAddress, msg.sender, _borrower, amountToLiquidate, 0);
_safeWithdrawLogic(_poolTokenCollateralAddress, amountToSeize, _borrower, msg.sender, 0);

Expand Down

0 comments on commit 125c654

Please sign in to comment.