# Liquidation bonus logic is wrong

DYAD's liquidate() computes the 20% liquidation bonus from collateral-minus-debt equity instead of burned debt, so liquidators receive far less collateral than intended, killing liquidation incentive.

- Payout: Audit contest
- Severity: medium
- Program: DYAD
- Found by: SBSecurity
- Disclosed: 2024-06-17
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-04-dyad-findings/issues/982
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-dyad%3AM-01
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:liquidation

**Target:** VaultManagerV2.liquidate() Solidity contract
**Root cause:** liquidationEquityShare is derived from (cappedCr - 1e18), i.e. the user's equity ratio above 100%, rather than from the burned debt amount, so the bonus scales with collateral-debt surplus instead of debt repaid.
**Steps:**
1. Deploy VaultManagerV2 and a WETH vault; set WETH oracle to $1.
2. Alice deposits 1.5 WETH and mints 1 DYAD; liquidator deposits 2 WETH and mints 1 DYAD.
3. Drop WETH oracle to $0.9 so Alice's collatRatio = 1.35e18.
4. Call `vaultManagerV2.liquidate(aliceNft, liquidatorNft)` as liquidator.
5. Observe liquidator collateral goes from 1.8e18 to 2.87e18: only ~$107 received for $100 debt repaid, i.e. ~$7 bonus instead of $20.
**How found:** Code reading of liquidate(): noticed liquidationEquityShare uses (cappedCr - 1e18) instead of the burned debt, then confirmed with a Foundry PoC comparing expected $120 vs actual $107 payout.
**Insight:** In liquidation math, always verify the bonus numerator is the repaid debt, not a ratio-derived equity term; a ratio-based share silently shrinks the incentive as collateral approaches the debt, and the same formula error recurs across forks of Aave-style liquidators.

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

**Foundry test proving wrong liquidation bonus**

```
function test_liquidation_bonus_is_wrong() public {
            address liquidator = makeAddr("Liquidator");
            address alice = makeAddr("Alice");

            wethOracle.setPrice(1e8); // Using 1$ for weth for better understanding
            uint256 liquidatorNft = mintDNFTAndDepositToWethVault(liquidator, 2e18, 1e18);
            uint256 aliceNft = mintDNFTAndDepositToWethVault(alice, 1.5e18, 1e18);

            wethOracle.setPrice(0.9e8);
            assertEq(vaultManagerV2.collatRatio(aliceNft), 1.35e18);

            console.log("Liquidator collateral before liquidate Alice:", vaultManagerV2.getNonKeroseneValue(liquidatorNft));
            vm.prank(liquidator);
            vaultManagerV2.liquidate(aliceNft, liquidatorNft);
            console.log("Liquidator collateral after liquidate Alice: ", vaultManagerV2.getNonKeroseneValue(liquidatorNft));
            // The liquidator receives only 106.9999999 collateral in return.
```

**Vulnerable liquidate() bonus calculation**

```
uint cappedCr               = cr < 1e18 ? 1e18 : cr;
    uint liquidationEquityShare = (cappedCr - 1e18).mulWadDown(LIQUIDATION_REWARD);
    uint liquidationAssetShare  = (liquidationEquityShare + 1e18).divWadDown(cappedCr);
```

