From 391841190c8bac55215fa01e1a376c16f4814d1e Mon Sep 17 00:00:00 2001 From: The3D Date: Tue, 22 Mar 2022 13:56:18 +0100 Subject: [PATCH] fix: fixes reentrancy issue on LendingPoolCollateralManager --- .../LendingPoolCollateralManager.sol | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/contracts/protocol/lendingpool/LendingPoolCollateralManager.sol b/contracts/protocol/lendingpool/LendingPoolCollateralManager.sol index 80692726..c6a6f2d3 100644 --- a/contracts/protocol/lendingpool/LendingPoolCollateralManager.sol +++ b/contracts/protocol/lendingpool/LendingPoolCollateralManager.sol @@ -150,8 +150,9 @@ contract LendingPoolCollateralManager is // If the liquidator reclaims the underlying asset, we make sure there is enough available liquidity in the // collateral reserve if (!receiveAToken) { - uint256 currentAvailableCollateral = - IERC20(collateralAsset).balanceOf(address(vars.collateralAtoken)); + uint256 currentAvailableCollateral = IERC20(collateralAsset).balanceOf( + address(vars.collateralAtoken) + ); if (currentAvailableCollateral < vars.maxCollateralToLiquidate) { return ( uint256(Errors.CollateralManagerErrors.NOT_ENOUGH_LIQUIDITY), @@ -190,6 +191,13 @@ contract LendingPoolCollateralManager is 0 ); + // If the collateral being liquidated is equal to the user balance, + // we set the currency as not being used as collateral anymore + if (vars.maxCollateralToLiquidate == vars.userCollateralBalance) { + userConfig.setUsingAsCollateral(collateralReserve.id, false); + emit ReserveUsedAsCollateralDisabled(collateralAsset, user); + } + if (receiveAToken) { vars.liquidatorPreviousATokenBalance = IERC20(vars.collateralAtoken).balanceOf(msg.sender); vars.collateralAtoken.transferOnLiquidation(user, msg.sender, vars.maxCollateralToLiquidate); @@ -217,13 +225,6 @@ contract LendingPoolCollateralManager is ); } - // If the collateral being liquidated is equal to the user balance, - // we set the currency as not being used as collateral anymore - if (vars.maxCollateralToLiquidate == vars.userCollateralBalance) { - userConfig.setUsingAsCollateral(collateralReserve.id, false); - emit ReserveUsedAsCollateralDisabled(collateralAsset, user); - } - // Transfers the debt asset being repaid to the aToken, where the liquidity is kept IERC20(debtAsset).safeTransferFrom( msg.sender,