Currently, it calculates by applying swapFeePercent to cashAmountOut but the cashAmountOut means the cash amount that should be paid to the borrower.
When we check executeSellCreditMarket(), total cash paid by a lender is cashAmountOut + fees and fees should be swapFeePercent of this total cash amount.
We can confirm this from maxCashAmountOut and maxCredit calculations.
Lines of code
https://github.com/code-423n4/2024-06-size/blob/8850e25fb088898e9cf86f9be1c401ad155bea86/src/libraries/AccountingLibrary.sol#L249 https://github.com/code-423n4/2024-06-size/blob/8850e25fb088898e9cf86f9be1c401ad155bea86/src/libraries/AccountingLibrary.sol#L256
Vulnerability details
Impact
The protocol would charge less swap fees than expected while selling credit markets.
Proof of Concept
While selling credit markets,
getCreditAmountIn()
calculates swap fees for a given cash amount out.Currently, it calculates by applying
swapFeePercent
tocashAmountOut
but thecashAmountOut
means the cash amount that should be paid to the borrower.When we check executeSellCreditMarket(), total cash paid by a lender is
cashAmountOut + fees
andfees
should beswapFeePercent
of this total cash amount. We can confirm this from maxCashAmountOut and maxCredit calculations.So
maxCashAmountOut = creditPosition.credit * (PERCENT / (PERCENT + ratePerTenor) * ((PERCENT - swapFeePercent) / PERCENT) = creditPosition.credit * (PERCENT - swapFeePercent) / (PERCENT + ratePerTenor)
- it means fees areswapFeePercent
of total cash.Also,
maxCredit = cashAmountOut * ((PERCENT + ratePerTenor) / PERCENT) * (PERCENT / (PERCENT - swapFeePercent)) = cashAmountOut * (PERCENT + ratePerTenor) / (PERCENT - swapFeePercent)
- it means the same thing as above.So
fees = swapFeePercent * (cashAmountOut + fees)
, that isfees = cashAmountOut * swapFeePercent / (1 - swapFeePercent)
.But in
getCreditAmountIn()
, it calculatesfees = cashAmountOut * swapFeePercent / PERCENT
and the protocol would charge less fees.Tools Used
Manual Review
Recommended Mitigation Steps
Fees should be calculated like
fees = cashAmountOut * swapFeePercent / (PERCENT - swapFeePercent)
.Assessed type
Math