uint256 collateralToWithdraw = (
_amount * debtTokenPrice * (10 ** IERC20(unwindParams.collateralToken).decimals()) * LTV_PRECISION
) / (collateralTokenPrice * (10 ** IERC20(_asset).decimals()) * liqThreshold);
Normal behavior: The contract should calculate the exact amount of collateral to withdraw to repay the debt plus flash loan when unwinding a leveraged position.
Issue: The contract currently uses liquidationThreshold instead of the correct LTV for calculating how much collateral to withdraw. This results in underestimation of collateral, causing insufficient repayment for the flash loan.
Open a leveraged position using Aave.
Attempt to unwind using the contract.
Observe that the swap returns less than required to cover the flash loan.
- uint256 collateralToWithdraw = (
- _amount * debtTokenPrice * (10 ** IERC20(unwindParams.collateralToken).decimals()) * LTV_PRECISION
- ) / (collateralTokenPrice * (10 ** IERC20(_asset).decimals()) * liqThreshold);
+ (, uint256 ltv,,,,,,,,) = aaveDataProvider.getReserveConfigurationData(unwindParams.collateralToken);
+ uint256 collateralToWithdraw = (
+ _amount * debtTokenPrice * (10 ** IERC20(unwindParams.collateralToken).decimals()) * LTV_PRECISION
+ ) / (collateralTokenPrice * (10 ** IERC20(_asset).decimals()) * ltv);