Submitted by pkqs90, also found by pkqs90, lian886, Bauchibred, 0xbepresent, and hash (1, 2)
First, lets inspect how deposit decreaseLever with swap enabled works:
Now, step 4 is an EXACT_OUT swap, since it is forced to swap the exact amount of debt tokens used to repay the flashloan. However, after step 4, there may be some collateral tokens left, which is the residualAmount.
If leverParams.auxSwap is not enabled, the collateral token is simply sent back to the recipient. However, if leverParams.auxSwap is enabled, a swap is performed.
The issue here is, the leverParams.auxSwap swap is an EXACT_IN swap, and user would hardcode the amount of inTokens used for this swap. There is no way to know the exact amount of collateral tokens left after step 4, so there must still be some collateral tokens leftover after the leverParams.auxSwap.
These leftover tokens are not sent to anybody, and stuck in the contract.
Send the amount of IERC20(leverParams.primarySwap.assetIn).balance(address(this)) to residualRecipient to make sure there are no leftovers.
amarcu (LoopFi) confirmed
