# Invalid handling of flash loan fees in PositionAction::onCreditFlashLoan, forcing it to always revert

PositionAction::onCreditFlashLoan repays full subDebt without subtracting flash loan fee, so decreaseLever always reverts when fee>0.

- Payout: Audit contest
- Severity: medium
- Program: LoopFi
- Found by: 0xAlix2
- Disclosed: 2025-02-17
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-10-loopfi-findings/issues/27
- Library page: https://bugbountylibrary.com/r/c4%3A2024-10-loopfi%3AM-01
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:solidity technique:incomplete-fix technique:source-review vuln:accounting-error vuln:dos vuln:flash-loan

**Target:** PositionAction20 / Flashlender credit flash loan callback
**Root cause:** onCreditFlashLoan repays the full subDebt from the flash lender's constant amount without reserving the fee, so the contract lacks balance to repay debt plus fee. Prior fix only added fee to approvals, not to the repayment accounting.
**Steps:**
1. Deploy Flashlender with fee 0.01 ether and set credit manager debt limit.
2. User deposits 1000 ETH collateral and borrows 200 ETH via PositionAction.borrow.
3. Call PositionAction.decreaseLever with LeverParams (BALANCER EXACT_OUT swap, 2 ether).
4. Flash lender sends credit and calls onCreditFlashLoan; contract repays full subDebt then must return subDebt+fee.
5. Transfer of fee fails with `ERC20: transfer amount exceeds balance`, reverting the whole flow.
**How found:** Noticed prior fix (issue 524) only added fee to forceApprove amounts but the repayment used the full constant flash amount, leaving no balance for the fee; wrote a Foundry PoC with fee>0 to confirm revert.
**Exploitation:** Not weaponized; DoS: any decreaseLever call reverts whenever the flash lender charges a nonzero fee, blocking leverage-down functionality.
**Insight:** When a fix adds fee to approvals, verify the repayment/accounting path also reserves the fee from the received amount; approvals alone don't create balance.

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

**Foundry test proving decreaseLever reverts with fees**

```
function test_leverageDownNotAccountingFees() public {
    // Re-initialize the system to have fees > 0
    flashlender = new Flashlender(IPoolV3(address(liquidityPool)), 0.01 ether);
    liquidityPool.setCreditManagerDebtLimit(address(flashlender), type(uint256).max);
    positionAction = new PositionAction20(
        address(flashlender),
        address(swapAction),
        address(poolAction),
        address(vaultRegistry)
    );

    uint256 depositAmount = 1_000 ether;
    uint256 borrowAmount = 200 ether;

    deal(address(token), user, depositAmount);

    address[] memory assets = new address[](2);
    assets[0] = address(underlyingToken);
    assets[1] = address(token);

    vm.startPrank(user);

    // User deposits 1k ETH collateral
    token.approve(address(vault), type(uint256).max);
    vault.deposit(address(userProxy), depositAmount);

    // User borrows 200 ETH
    userProxy.execute(
        address(positionAction),
        abi.encodeWithSelector(
            positionAction.borrow.selector,
            address(userProxy),
            address(vault),
            CreditParams({amount: borrowAmount, creditor: user, auxSwap: emptySwap})
        )
    );

    vm.expectRevert(bytes("ERC20: transfer amount exceeds balance"));
    userProxy.execute(
        address(positionAction),
        abi.encodeWithSelector(
            positionAction.decreaseLever.selector,
            LeverParams({
                position: address(userProxy),
                vault: address(vault),
                collateralToken: address(token),
                primarySwap: SwapParams({
                    swapProtocol: SwapProtocol.BALANCER,
                    swapType: SwapType.EXACT_OUT,
                    assetIn: address(token),
                    amount: 1 ether,
                    limit: 2 ether,
                    recipient: address(positionAction),
                    residualRecipient: address(positionAction),
                    deadline: block.timestamp,
                    args: abi.encode(weightedPoolIdArray, assets)
                }),
                auxSwap: emptySwap,
                auxAction: emptyPoolActionParams
            }),
            2 ether,
            address(userProxy)
        )
    );

    vm.stopPrank();
}
```

