diff --git a/contracts/lendingpool/LendingPoolLiquidationManager.sol b/contracts/lendingpool/LendingPoolLiquidationManager.sol index 967a0087..e5c5b8b7 100644 --- a/contracts/lendingpool/LendingPoolLiquidationManager.sol +++ b/contracts/lendingpool/LendingPoolLiquidationManager.sol @@ -201,16 +201,12 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl //of _collateral to cover the actual amount that is being liquidated, hence we liquidate //a smaller amount - vars.collateralAtoken = AToken(payable(collateralReserve.aTokenAddress)); - - //if principalAmountNeeded < vars.ActualAmountToLiquidate, there isn't enough - //of _collateral to cover the actual amount that is being liquidated, hence we liquidate - //a smaller amount - if (vars.principalAmountNeeded < vars.actualAmountToLiquidate) { vars.actualAmountToLiquidate = vars.principalAmountNeeded; } + vars.collateralAtoken = AToken(payable(collateralReserve.aTokenAddress)); + //if liquidator reclaims the underlying asset, we make sure there is enough available collateral in the reserve if (!_receiveAToken) { uint256 currentAvailableCollateral = IERC20(_collateral).universalBalanceOf( @@ -224,6 +220,10 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl } } + //update the principal reserve + principalReserve.updateCumulativeIndexesAndTimestamp(); + principalReserve.updateInterestRates(_reserve, vars.actualAmountToLiquidate, 0); + if (vars.userVariableDebt >= vars.actualAmountToLiquidate) { IVariableDebtToken(principalReserve.variableDebtTokenAddress).burn( _user, @@ -245,6 +245,11 @@ contract LendingPoolLiquidationManager is ReentrancyGuard, VersionedInitializabl vars.collateralAtoken.transferOnLiquidation(_user, msg.sender, vars.maxCollateralToLiquidate); } else { //otherwise receives the underlying asset + + //updating collateral reserve + collateralReserve.updateCumulativeIndexesAndTimestamp(); + collateralReserve.updateInterestRates(_collateral, 0, vars.maxCollateralToLiquidate); + //burn the equivalent amount of atoken vars.collateralAtoken.burnOnLiquidation(_user, vars.maxCollateralToLiquidate); vars.collateralAtoken.transferUnderlyingTo(msg.sender, vars.maxCollateralToLiquidate); diff --git a/test/liquidation-atoken.spec.ts b/test/liquidation-atoken.spec.ts index 8019ad21..a523fb05 100644 --- a/test/liquidation-atoken.spec.ts +++ b/test/liquidation-atoken.spec.ts @@ -197,6 +197,18 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => 'Invalid principal available liquidity' ); + //the liquidity index of the principal reserve needs to be bigger than the index before + expect(daiReserveDataAfter.liquidityIndex.toString()).to.be.bignumber.gt( + daiReserveDataBefore.liquidityIndex.toString(), + 'Invalid liquidity index' + ); + + //the principal APY after a liquidation needs to be lower than the APY before + expect(daiReserveDataAfter.liquidityRate.toString()).to.be.bignumber.lt( + daiReserveDataBefore.liquidityRate.toString(), + 'Invalid liquidity APY' + ); + expect(ethReserveDataAfter.availableLiquidity).to.be.bignumber.almostEqual( new BigNumber(ethReserveDataBefore.availableLiquidity.toString()).toFixed(0), 'Invalid collateral available liquidity' @@ -317,6 +329,18 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) => 'Invalid principal available liquidity' ); + //the liquidity index of the principal reserve needs to be bigger than the index before + expect(usdcReserveDataAfter.liquidityIndex.toString()).to.be.bignumber.gt( + usdcReserveDataBefore.liquidityIndex.toString(), + 'Invalid liquidity index' + ); + + //the principal APY after a liquidation needs to be lower than the APY before + expect(usdcReserveDataAfter.liquidityRate.toString()).to.be.bignumber.lt( + usdcReserveDataBefore.liquidityRate.toString(), + 'Invalid liquidity APY' + ); + expect(ethReserveDataAfter.availableLiquidity).to.be.bignumber.almostEqual( new BigNumber(ethReserveDataBefore.availableLiquidity.toString()).toFixed(0), 'Invalid collateral available liquidity'