# Liquidation incorrectly tries to transfer token from Market instead of liquidator if remainingMargin is negative

PredyPool liquidation transfers bad-debt repayment from the Market contract instead of the liquidator, so negative-margin vaults can never be liquidated and debt grows.

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

**Target:** PredyPool liquidation logic via BaseMarket execLiquidationCall
**Root cause:** In LiquidationLogic.liquidate, when remainingMargin < 0 the code calls safeTransferFrom(msg.sender,...) where msg.sender is the Market contract, which holds no allowance to PredyPool, so the transfer always reverts.
**Steps:**
1. Liquidator calls BaseMarket.execLiquidationCall on PerpMarket
2. PerpMarket calls PredyPool.execLiquidationCall
3. PredyPool invokes SettlementCallbackLib to swap tokens between user and pool
4. Position fully wiped (hasPosition==false) with vault.margin < 0
5. Code executes `ERC20(quoteToken).safeTransferFrom(msg.sender, address(this), uint256(-remainingMargin))` where msg.sender is the Market
6. Market has no allowance to PredyPool, transfer reverts, liquidation impossible
**How found:** Warden traced the liquidation flowchart and code path, noticed msg.sender in the bad-debt branch is the Market (not the liquidator), and confirmed the Market lacks allowance to PredyPool and no tests cover the scenario.
**Exploitation:** Not an attacker-profit exploit; the flaw makes liquidation of underwater vaults revert, leaving bad debt uncleared while lending fees keep accruing, growing protocol insolvency.
**Insight:** When auditing callback-based flows, check who msg.sender actually is at each internal transfer point — a Market/callback contract differs from the end user, so allowance and balance assumptions silently break.

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

**Liquidation safeTransferFrom from msg.sender (Market) instead of liquidator**

```
else if (remainingMargin < 0) {
                vault.margin = 0;

>               // To prevent the liquidator from unfairly profiting through arbitrage trades in the AMM and passing losses onto the protocol,
>               // any losses that cannot be covered by the vault must be compensated by the liquidator
>               ERC20(pairStatus.quotePool.token).safeTransferFrom(msg.sender, address(this), uint256(-remainingMargin));
```

