# Liquidation Waterfall

`D25-F02-A05` · Digital Assets and On-Chain Finance → Liquidity and Liquidation · archetype `record-transform` · difficulty 4/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/digital-assets-and-on-chain-finance/liquidity-and-liquidation/liquidation-waterfall/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { liquidationWaterfall } from "fintech-algorithms/digital-assets-and-on-chain-finance/liquidity-and-liquidation/liquidation-waterfall";
```

## Signature

```ts
liquidationWaterfall(data, config)
```

The order in which collateral is seized and debt repaid, including liquidation bonus and any protocol fee. Determines what a borrower recovers, and it is rarely proportional.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `data` | `PositionInput` | yes | Position state — balances, prices, and the protocol parameters the calculation depends on. |
| `config` | `ProtocolConfig` | yes | Protocol-specific thresholds and factors. These differ per protocol and per asset, so a figure computed under the wrong config is confidently wrong. |

## Returns

`{ status, value, components, diagnostics }`

The result with every component, so a position's state can be reconciled against the protocol's own interface.

## Errors

- When a required protocol parameter is missing — reported as a status rather than thrown

## Complexity

Time `O(assets)`, space `O(assets)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`data`:

```json
{
  "debt_value": 10000,
  "collateral_sale_proceeds": 7000,
  "protocol_reserve": 1000,
  "insurance_fund": 2500,
  "backstop_capacity": 1000
}
```

`config`:

```json
{
  "collateral_recovery_rate": 1,
  "backstop_enabled": true
}
```

### Call

```ts
liquidationWaterfall(data, config)
```

### Returns

object with 9 fields: debt_value, effective_collateral_proceeds, total_buffer_capacity, covered_debt, uncovered_bad_debt, coverage_fraction, backstop_enabled, ledger, …

```json
{
  "debt_value": 10000,
  "effective_collateral_proceeds": 7000,
  "total_buffer_capacity": 4500,
  "covered_debt": 10000,
  "uncovered_bad_debt": 0,
  "coverage_fraction": 1,
  "backstop_enabled": true,
  "ledger": [
    {
      "layer": "collateral-recovery",
      "available_capacity": 7000,
      "absorbed": 7000,
      "remaining_debt": 3000,
      "cumulative_absorbed": 7000
    },
    {
      "layer": "protocol-reserve",
      "available_capacity": 1000,
      "absorbed": 1000,
      "remaining_debt": 2000,
      "cumulative_absorbed": 8000
    },
    {
      "layer": "insurance-fund",
      "available_capacity": 2500,
      "absorbed": 2000,
      "remaining_debt": 0,
      "cumulative_absorbed": 10000
    }
  ],
  "trace": [
    {
      "collateral_proceeds_multiplier": 0,
      "covered_debt": 4500,
      "uncovered_bad_debt": 5500
    },
    {
      "collateral_proceeds_multiplier": 0.005,
      "covered_debt": 4535,
      "uncovered_bad_debt": 5465
    },
    {
      "collateral_proceeds_multiplier": 0.01,
      "covered_debt": 4570,
      "uncovered_bad_debt": 5430
    }
  ]
}
```

## Other exports

`impermanentLoss`, `feeApr`, `healthFactor`, `liquidationPrice`, `runTopic`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/digital-assets-and-on-chain-finance/liquidity-and-liquidation/liquidation-waterfall/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/digital-assets-and-on-chain-finance/liquidity-and-liquidation/liquidation-waterfall/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/digital-assets-and-on-chain-finance/llms.txt
