# PositionAction.sol#onCreditFlashLoan may have leftover tokens after conducting leverParams.auxSwap

In LoopFi PositionAction, decreaseLever's auxSwap is EXACT_IN with a hardcoded amount, leaving residual collateral tokens permanently stuck in the contract.

- Payout: Audit contest
- Severity: medium
- Program: LoopFi
- Found by: pkqs90
- Disclosed: 2025-02-17
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-loopfi-findings/issues/87
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-loopfi%3AM-33
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** PositionAction.sol onCreditFlashLoan / decreaseLever
**Root cause:** auxSwap is forced to EXACT_IN with a user-hardcoded inToken amount, but the residual collateral after the EXACT_OUT primarySwap is unknown, so leftover tokens remain in the contract instead of being forwarded to residualRecipient.
**Steps:**
1. Call decreaseLever with primarySwap EXACT_OUT and auxSwap EXACT_IN with a fixed amountIn.
2. onCreditFlashLoan repays debt, withdraws collateral, runs primarySwap (EXACT_OUT) leaving residualAmount.
3. residualAmount > 0 and auxSwap.assetIn != 0 triggers auxSwap for the hardcoded amountIn only.
4. Remaining collateral (residualAmount - auxSwap amountIn) stays in PositionAction with no transfer out.
**How found:** Traced decreaseLever flow: primarySwap is EXACT_OUT so residual is unpredictable, yet auxSwap is validated as EXACT_IN with a hardcoded amount, implying leftover tokens after auxSwap.
**Exploitation:** User-initiated flow; leftover collateral tokens accumulate in the contract and are unrecoverable, causing user fund loss.
**Insight:** When a contract performs a fixed-size swap on an amount that is only known at runtime (residual after an EXACT_OUT swap), check whether the remainder is swept; hardcoded EXACT_IN amounts on dynamic balances always leave dust.

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

**onCreditFlashLoan auxSwap leftover tokens code**

```
if (leverParams.auxSwap.assetIn != address(0)) {
                _delegateCall(
                    address(swapAction),
                    abi.encodeWithSelector(
                        swapAction.swap.selector,
                        leverParams.auxSwap
                    )
                );
            } else {
                // otherwise just send the collateral to `residualRecipient`
                IERC20(leverParams.primarySwap.assetIn).safeTransfer(residualRecipient, residualAmount);
```

