# Users won't liquidate positions because the logic used to calculate the liquidator's profit is incorrect

Size liquidation reward used 6-decimal USDC futureValue against 18-decimal collateral, shrinking liquidator profit by 1e12 and discouraging liquidations.

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

**Target:** Solidity lending protocol liquidation logic (executeLiquidate)
**Root cause:** liquidatorReward multiplies debtPosition.futureValue (USDC, 6 decimals) by liquidationRewardPercent instead of debtInCollateralToken (WETH, 18 decimals), so the reward is 1e12 times too small.
**Steps:**
1. Deploy Size, deposit WETH/USDC for alice, bob, liquidator.
2. alice sets buyCreditLimit via pointCurve; bob _sellCreditMarket 80e6 USDC for 365 days.
3. _setPrice(1e18) so position is liquidatable.
4. Call _liquidate(liquidator, debtPositionId, 0) and compare returned profit minus debtInCollateralToken against Math.mulDivUp(debtInCollateralToken, rewardPercent, PERCENT).
5. Observe liquidator profit ~4140704 wei vs exact ~4140703550000000000 wei.
**How found:** Code review of executeLiquidate reward formula; noticed futureValue is USDC-denominated while assignedCollateral/debtInCollateralToken are WETH-denominated, then wrote a Foundry test logging both values to confirm the 1e12 discrepancy.
**Insight:** When a reward or fee formula mixes a raw debt amount with a collateral-denominated amount, check the token decimals of each operand; a unit mismatch silently scales incentives and can be proven with a two-log Foundry test comparing computed vs expected profit.

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

**Foundry test proving liquidator profit miscalculation**

```
function test_liquidate_profit_of_liquidator() public {
        uint256 rewardPercent = size.feeConfig().liquidationRewardPercent;

        // current reward percent is 5%
        assertEq(rewardPercent, 50000000000000000);

        _deposit(alice, weth, 100e18);
        _deposit(alice, usdc, 100e6);
        _deposit(bob, weth, 100e18);
        _deposit(liquidator, weth, 100e18);
        _deposit(liquidator, usdc, 100e6);

        _buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.03e18));
        uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, 80e6, 365 days, false);

        _setPrice(1e18);
        assertTrue(size.isDebtPositionLiquidatable(debtPositionId));

        uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;
        uint256 assignedCollateral = size.getDebtPositionAssignedCollateral(debtPositionId);
        uint256 debtInCollateralToken =
            size.debtTokenAmountToCollateralTokenAmount(futureValue);
        
        console2.log('future value in usdc        ==> ', futureValue);
        console2.log('future value in WETH        ==> ', debtInCollateralToken);
        console2.log('assigned collateral in WETH ==> ', assignedCollateral);

        console2.log('********************');

        uint256 liquidatorProfit = _liquidate(liquidator, debtPositionId, 0);
        console2.log('liquidator profit in WETH   ==> ', liquidatorProfit - debtInCollateralToken);

        uint256 exactProfit = Math.min(
            assignedCollateral - debtInCollateralToken,
            Math.mulDivUp(debtInCollateralToken, rewardPercent, PERCENT)    
        );
        console2.log('exact profit in WETH        ==> ', exactProfit);
    }
```

**Liquidate function showing reward calculation bug**

```
function executeLiquidate(State storage state, LiquidateParams calldata params)
        external
        returns (uint256 liquidatorProfitCollateralToken)
    {
        DebtPosition storage debtPosition = state.getDebtPosition(params.debtPositionId);
        LoanStatus loanStatus = state.getLoanStatus(params.debtPositionId);
        uint256 collateralRatio = state.collateralRatio(debtPosition.borrower);

        uint256 collateralProtocolPercent = state.isUserUnderwater(debtPosition.borrower)
            ? state.feeConfig.collateralProtocolPercent
            : state.feeConfig.overdueCollateralProtocolPercent;

        uint256 assignedCollateral = state.getDebtPositionAssignedCollateral(debtPosition);  // @audit, step 1
        uint256 debtInCollateralToken = state.debtTokenAmountToCollateralTokenAmount(debtPosition.futureValue);  // @audit, step 2
        uint256 protocolProfitCollateralToken = 0;

        if (assignedCollateral > debtInCollateralToken) {
            uint256 liquidatorReward = Math.min(
                assignedCollateral - debtInCollateralToken,
                Math.mulDivUp(debtPosition.futureValue, state.feeConfig.liquidationRewardPercent, PERCENT)
            );  // @audit, step 3
            liquidatorProfitCollateralToken = debtInCollateralToken + liquidatorReward;

            ...
        } else {
            liquidatorProfitCollateralToken = assignedCollateral;
        }
```

