# When sellCreditMarket() is called to sell credit for a specific cash amount, the protocol might receive a lower swapping fee than expected

Size's sellCreditMarket fee formula undercharges swap fees when selling credit for a fixed cash amount, letting users pay less fee than documented.

- Payout: Audit contest
- Severity: high
- Program: Size
- Found by: 0xpiken
- Disclosed: 2024-09-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-06-size-findings/issues/288
- Library page: https://bugbountylibrary.com/r/c4%3A2024-06-size%3AH-01
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:solidity technique:source-review vuln:accounting-error vuln:fee-bypass

**Target:** Solidity lending protocol sellCreditMarket / AccountingLibrary.getCreditAmountIn
**Root cause:** getCreditAmountIn computes fees as cashAmountOut*swapFeePercent/PERCENT instead of cashAmountOut*swapFeePercent/(PERCENT-swapFeePercent), inconsistent with the creditAmountIn gross-up formula and the documented fee schedule.
**Steps:**
1. Deploy Size, deposit collateral for bob and USDC for alice, set a buy credit limit curve.
2. Call `sellCreditMarket` with `exactAmountIn: true`, amount 120e6, tenor 365 days; observe feeRecipient gets 0.5e6.
3. Revert to snapshot; call `sellCreditMarket` with `exactAmountIn: false`, amount 99.5e6; observe feeRecipient gets only 497500.
4. Compare: same 99.5e6 cash out and 120e6 debt, but fee differs by 2500 due to wrong fee formula.
**How found:** Code-reading of AccountingLibrary.getCreditAmountIn against the documented fee examples (Example 2 in docs shows 0.5 fee but formula yields a different value), spotting the mismatch between creditAmountIn gross-up and fee computation.
**Exploitation:** Any user selling credit for a specific cash amount (exactAmountIn=false) pays a lower swap fee than the documented formula requires; the shortfall accrues to the user and is lost to the protocol fee recipient. No attacker capital or chaining needed.
**Insight:** When a protocol grosses up an input by dividing by (1-fee), the fee itself must also be computed on the grossed-up base (fee = out*fee/(1-fee)), not out*fee; cross-check every fee formula against the protocol's own documented numeric examples.

## Proof of concept (copied exactly from the original report)

**Foundry test proving incorrect swap fee**

```
function test_SellCreditMarket_sellCreditMarket_incorrectFee() public {
        _deposit(bob, weth, 100e18);
        _deposit(alice, usdc, 100e6);
        _buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.2e18));
        uint256 tenor = 365 days;
        vm.startPrank(bob);
        uint256 apr = size.getLoanOfferAPR(alice, tenor);
        //@audit-info alice has 100e6 szaUSDC 
        assertEq(size.getUserView(alice).borrowATokenBalance, 100e6);
        uint256 snapshot = vm.snapshot();
        //@audit-info bob sell 120e6 credit to alice for szaUSDC
        size.sellCreditMarket(
            SellCreditMarketParams({
                lender: alice,
                creditPositionId: RESERVED_ID,
                amount: 120e6,
                tenor: tenor,
                deadline: block.timestamp,
                maxAPR: apr,
                exactAmountIn: true
            })
        );
        //@audit-info alice has 0 szaUSDC left
        assertEq(size.getUserView(alice).borrowATokenBalance, 0);
        //@audit-info bob received 99.5e6 szaUSDC
        assertEq(size.getUserView(bob).borrowATokenBalance, 99.5e6);
        //@audit-info bob owed 120e6 debt
        assertEq(size.getUserView(bob).debtBalance, 120e6);
        //@audit-info feeRecipient received 0.5e6 szaUSDC as fee
        assertEq(size.getUserView(feeRecipient).borrowATokenBalance, 500000);
        //@audit-info restore to the snapshot before sellCreditMarket
        vm.revertTo(snapshot);
        //@audit-info bob sell credit to alice for 99.5e6 szaUSDC 
        size.sellCreditMarket(
            SellCreditMarketParams({
                lender: alice,
                creditPositionId: RESERVED_ID,
                amount: 99.5e6,
                tenor: tenor,
                deadline: block.timestamp,
                maxAPR: apr,
                exactAmountIn: false
            })
        );
        //@audit-info alice has 2500 szaUSDC left
        assertEq(size.getUserView(alice).borrowATokenBalance, 2500);
        //@audit-info bob received 99.5e6 szaUSDC
        assertEq(size.getUserView(bob).borrowATokenBalance, 99.5e6);
        //@audit-info bob owed 120e6 debt
        assertEq(size.getUserView(bob).debtBalance, 120e6);
        //@audit-info feeRecipient received 497500 szaUSDC as fee
        assertEq(size.getUserView(feeRecipient).borrowATokenBalance, 497500);
    }
```

