# Wrong liquidation logic

Marginswap's belowMaintenanceThreshold used an inverted inequality, so healthy accounts could be liquidated and undercollateralized accounts could not.

- Payout: Audit contest
- Severity: high
- Program: Marginswap
- Disclosed: 2021-05-03
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2021-04-marginswap-findings/issues/23
- Library page: https://bugbountylibrary.com/r/c4%3A2021-04-marginswap%3AH-05
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:business-logic vuln:liquidation

**Target:** CrossMarginAccount liquidation check (Solidity contract)
**Root cause:** The comparison `100 * holdings >= liquidationThresholdPercent * loan` is inverted; it should be `<=`, so the function returns true (liquidatable) for well-collateralized accounts and false for undercollateralized ones.
**Steps:**
1. Read `belowMaintenanceThreshold`, which computes `loan = loanInPeg(account, true)` and `holdings = holdingsInPeg(account, true)`.
2. Note the intended invariant `holdings / loan >= 1.1`.
3. Observe the code returns `100 * holdings >= liquidationThresholdPercent * loan`, the inverse of the required check.
4. Conclude healthy accounts satisfy the condition and are flagged liquidatable, while unhealthy accounts are not.
**How found:** Code review of the liquidation predicate against its stated invariant comment (`holdings / loan >= 1.1`), spotting that the implemented inequality was the inverse of the documented requirement.
**Exploitation:** A liquidator calls liquidation on an account whose holdings/loan ratio is above the maintenance threshold; the inverted check returns true, allowing the healthy account to be liquidated. Conversely, genuinely undercollateralized accounts return false and escape liquidation.
**Insight:** When a function carries a comment stating the intended invariant, translate the inequality by hand and compare direction; inverted comparison operators in threshold checks silently flip who is liquidatable.

