H-1: Wrong ternary precedence in `renew()` drops roll-over interest
Description:
In renew(), the expression that computes the additional interest for a renewal relies on a ternary without parentheses. In Solidity, arithmetic (+) has higher precedence than the ternary ? :, so the whole expression is parsed incorrectly: the sum happens first, then the boolean comparison, and finally the ternary chooses only the top-up interest, discarding the interest for extending the existing principal.
This almost always evaluates the condition to true and sets newInterest to only the top-up interest, omitting the roll-over interest for the period old end to new end on the already borrowed amount.
Impact:
High. Borrowers underpay interest on renewals. The protocol loses the interest component for extending the existing debt.
Recommendation:
Add parentheses so that only the second addend is conditional:
uint256 newInterest = _calculateInterest(userLoan.borrowedAmount, _outstandingDebt, newEndDate - userLoan.endDate)
+ (newBorrowAmount > 0 ? _calculateInterest(newBorrowAmount, _outstandingDebt, newDuration) : 0);
Developer Response:
Fixed here.