Submitted by chaduke, also found by 0xAlix2, 13u9, zhaojohnson, lian886, lanrebayode77, Nyx, 0xbepresent, 0xpiken, 0xc0ffEE, gumgumzum, hash, pkqs90, and nnez
PositionAction.decreaseLever() allows one to decrease the leverage for a position by doing the following:
The first problem lies in PositionAction.increaseLever(): loanAmount uses leverParams.primarySwap.amount, it does not consider the protocol fee. leverParams.primarySwap.amount is the amount of underlying tokens that needs to be swapped out that will be returned back to the pool, which includes the protocol fee. In other words, the correct formula is loanAmount = leverParams.primarySwap.amount - fee.
https://github.com/code-423n4/2024-07-loopfi/blob/57871f64bdea450c1f04c9a53dc1a78223719164/src/proxy/PositionAction.sol#L364
Meanwhile, function positionAction.onCreditFlashLoan() has a similar problem:
https://github.com/code-423n4/2024-07-loopfi/blob/57871f64bdea450c1f04c9a53dc1a78223719164/src/proxy/PositionAction.sol#L444C17-L444C25
In summary, both functions do not consider the impact of protcolFee, as a result, these functions will fail.
POC:
Foundry
Correct the two functions as follows, focusing on calculating the correct loanAmount and subDebt and returnAmount:
Math
amarcu (LoopFi) confirmed
