# PoolAction::_balancerExit returns wrong token out amount

PoolAction::_balancerExit returns the recipient's entire token balance instead of the exit delta, inflating withdrawnCollateral and reverting decreaseLever with transfer-exceeds-balance.

- 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-07-loopfi-findings/issues/239
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-loopfi%3AM-08
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** PoolAction::_balancerExit / PositionAction4626 leverage-down flow
**Root cause:** _balancerExit returns IERC20(assets[outIndex]).balanceOf(address(poolActionParams.recipient)) rather than the difference before/after the exit, so pre-existing recipient balance of the same token is counted as exit proceeds.
**Steps:**
1. Deal user 2x wstETH; join Balancer pool via poolAction.transferAndJoin with 10 wstETH, receiving ~11 BPT.
2. Transfer BPT to poolAction and call poolAction.exit with EXACT_BPT_IN_FOR_ONE_TOKEN_OUT.
3. Observe amountOut returns 19 wstETH (whole balance) instead of ~9 wstETH actual exit proceeds.
4. In leverage-down: increaseLever to build Balancer position, then call decreaseLever with 70 ether subCollateral; withdrawnCollateral is inflated, residualAmount exceeds contract balance, reverting `ERC20: transfer amount exceeds balance`.
**How found:** Code reading of _balancerExit: noticed it returns the recipient's full balance of the out asset rather than a delta, and reasoned that a recipient holding the same token from other sources would get an exaggerated amount, breaking downstream accounting.
**Exploitation:** User leveraging down exits a Balancer pool while already holding tokenOut; the inflated return value makes PositionAction::onCreditFlashLoan compute residualAmount larger than actual balance, causing the transfer to revert and blocking the decreaseLever operation (DoS).
**Insight:** When a helper returns 'amount received' by reading a balance, check whether the recipient could already hold that token; any pre-existing balance inflates the return and breaks downstream residual/repayment math. Look for balanceOf-based return values in exit/withdraw/swap helpers.

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

**Test proving wrong Balancer exit amount**

```
function test_wrongBalancerExitAmount() public {
    uint256 amount = 10 ether;

    deal(wstETH, user, amount * 2);

    (uint8 v, bytes32 r, bytes32 s) = PermitMaker.getPermit2TransferFromSignature(
        address(wstETH),
        address(poolAction),
        amount,
        NONCE,
        block.timestamp,
        userPk
    );

    PermitParams[] memory permitParamsArray = new PermitParams[](3);
    permitParamsArray[1] = PermitParams(ApprovalType.PERMIT2, amount, NONCE, block.timestamp, v, r, s);

    address[] memory tokens = new address[](3);
    tokens[0] = wstETH_bb_a_WETH_BPTl;
    tokens[1] = wstETH;
    tokens[2] = bbaweth;

    uint256[] memory maxAmountsIn = new uint256[](3);
    maxAmountsIn[0] = 0;
    maxAmountsIn[1] = amount;
    maxAmountsIn[2] = 0;

    uint256[] memory tokensIn = new uint256[](2);
    tokensIn[0] = amount;
    tokensIn[1] = 0;

    uint256[] memory minAmountsOut = new uint256[](3);
    minAmountsOut[0] = 0;
    minAmountsOut[1] = 0;
    minAmountsOut[2] = 0;

    vm.startPrank(user);

    // Joins Balancer pool with 10 WSTETH
    poolAction.transferAndJoin(
        user,
        permitParamsArray,
        PoolActionParams(Protocol.BALANCER, 0, user, abi.encode(poolId, tokens, tokensIn, maxAmountsIn))
    );

    uint256 balancerLP = ERC20(wstETH_bb_a_WETH_BPTl).balanceOf(user);

    // Receives 11 Balancer LP tokens
    assertEq(balancerLP / 1 ether, 11);
    // Balance is 10 WSTETH
    assertEq(ERC20(wstETH).balanceOf(user), amount);

    // Transfer Balancer LP tokens to PoolAction
    ERC20(wstETH_bb_a_WETH_BPTl).transfer(address(poolAction), balancerLP);

    // Exits the Balancer pool
    uint256 amountOut = poolAction.exit(
        PoolActionParams(
            Protocol.BALANCER,
            0,
            user,
            abi.encode(poolId, wstETH_bb_a_WETH_BPTl, balancerLP, 0, tokens, minAmountsOut)
        )
    );

    // Amount returned by the function is 19 WSTETH instead of 9 WSTETH
    assertEq(amountOut / 1 ether, 19);
    // Balance is 19 WSTETH
    assertEq(ERC20(wstETH).balanceOf(user) / 1 ether, 19);
}
```

