# Wrong repayment amount used in PositionAction::_repay, forcing users to unexpectedly lose funds

PositionAction::_repay repays the user-specified amount instead of the post-swap amount, so swapped tokens are drained without reducing debt.

- 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/526
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-loopfi%3AM-02
- 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 proxy repay with aux swap (CDP vault)
**Root cause:** _repay ignores the amount returned by _transferAndSwap and repays creditParams.amount, unlike _deposit which uses the swapped amount.
**Steps:**
1. Deploy balancer pool for underlying token
2. User deposits 1000 ETH collateral, borrows 500 ETH
3. Call positionAction.repay with CreditParams{amount:0, auxSwap: USDC->underlying EXACT_IN 100e6}
4. Swap executes, USDC drained from user, but amount=0 is repaid
5. Assert debt unchanged at 500 ETH and USDC balance 0
**How found:** Compared _repay logic against _deposit, noticing _deposit uses the swap return value while _repay uses the user-specified amount, an API inconsistency.
**Exploitation:** User calls repay with auxSwap and amount=0 (or less than swap output); the swap consumes their input tokens but the vault receives/repays only the stale creditParams.amount, so funds are lost with no debt reduction.
**Insight:** When a contract has parallel deposit/repay (or add/remove) paths sharing swap helpers, diff how each consumes the swap return value; the asymmetric one usually repays/credits the wrong amount.

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

**Balancer pool setup helper for PoC**

```
function _createBalancerUnderlyingTokenPool() internal returns (IComposableStablePool stablePool_) {
    deal(address(DAI), address(this), 5_000_000 * 1e18);
    deal(address(USDC), address(this), 5_000_000 * 1e6);
    deal(address(USDT), address(this), 5_000_000 * 1e6);
    underlyingToken.mint(address(this), 5_000_000 * 1e18);

    uint256[] memory maxAmountsIn = new uint256[](4);
    address[] memory assets = new address[](4);
    assets[0] = address(DAI);
    assets[1] = address(USDC);
    assets[2] = address(USDT);

    bool tokenPlaced;
    address tempAsset;
    for (uint256 i; i < assets.length; i++) {
        if (!tokenPlaced) {
            if (uint160(assets[i]) > uint160(address(underlyingToken))) {
                tokenPlaced = true;
                tempAsset = assets[i];
                assets[i] = address(underlyingToken);
            } else if (i == assets.length - 1) {
                assets[i] = address(underlyingToken);
            }
        } else {
            address placeholder = assets[i];
            assets[i] = tempAsset;
            tempAsset = placeholder;
        }
    }

    for (uint256 i; i < assets.length; i++) {
        maxAmountsIn[i] = ERC20(assets[i]).balanceOf(address(this));
        ERC20(assets[i]).safeApprove(address(balancerVault), maxAmountsIn[i]);
    }

    stablePool_ = stablePoolFactory.create(
        "Test Token Pool",
        "FUDT",
        assets,
        200,
        3e14, // swapFee (0.03%)
        address(this) // owner
    );

    balancerVault.joinPool(
        stablePool_.getPoolId(),
        address(this),
        address(this),
        JoinPoolRequest({
            assets: assets,
            maxAmountsIn: maxAmountsIn,
            userData: abi.encode(JoinKind.INIT, maxAmountsIn),
            fromInternalBalance: false
        })
    );
}
```

