> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mystockline.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# LiquidationModule

> LiquidationModule sizes and settles liquidations for Stockline.

LiquidationModule sizes and settles liquidations for Stockline. It runs a Dutch auction on the discount, starting at exactly 0 when an account first crosses the line and rising linearly to the book's `maxLiqDiscount` over `auctionWindow`. Liquidations are partial by default: the module repays only what restores `targetHealth`, and closes the book when the remaining debt would fall below `dustDebt`. Stockline checks regime, pause, and health before calling in.

`LiquidationModule` lets you:

* Preview the repay, seize, and discount of a liquidation before sending it
* Read the current auction discount for an account and book
* Settle a liquidation on behalf of Stockline (core only)
* Trigger a bad-debt write-off when a liquidation leaves an account with no collateral

Addresses per network. Every address is also on [Addresses](/resources/addresses).

| Network                 | Address                                                                                                                                       |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| Robinhood Chain testnet | [0xb3276bB44dB8878F8D8b0659c622240B0bAd92E8](https://explorer.testnet.chain.robinhood.com/address/0xb3276bB44dB8878F8D8b0659c622240B0bAd92E8) |

The source code is [`src/core/LiquidationModule.sol`](https://github.com/stockline-xyz/contracts/blob/main/src/core/LiquidationModule.sol) on GitHub. The ABI is [`/abis/LiquidationModule.json`](/abis/LiquidationModule.json).

## Write methods

### liquidate

```solidity theme={"system"}
function liquidate(
    address liquidator,
    address account,
    uint16 bookId,
    uint256 repayAmount
) external
```

Sizes the liquidation with `quote`, then calls `Stockline.applyLiquidation` to cut debt and move collateral. If the account ends with debt but no collateral in any book, it calls `Stockline.realizeBadDebt`. Only Stockline can call it. Reverts with NothingToSeize when the quote has zero repay or zero seize.

<Warning>
  Core only. Reverts with NotCore for any other caller. Call `Stockline.liquidate` instead.
</Warning>

<Note>
  The registry's `liquidatorAllowlist` flag is not enforced here. Anyone can liquidate through Stockline.
</Note>

**Input parameters**

| Name        | Type      | Description                                                    |
| ----------- | --------- | -------------------------------------------------------------- |
| liquidator  | `address` | The address that pays USDG and receives the seized collateral. |
| account     | `address` | The account being liquidated.                                  |
| bookId      | `uint16`  | The book to liquidate.                                         |
| repayAmount | `uint256` | The liquidator's maximum USDG repay.                           |

## Read methods

### quote

```solidity theme={"system"}
function quote(
    address account,
    uint16 bookId,
    uint256 repayAmount
) public view returns (LiqQuote memory q)
```

Returns what a liquidation would do right now without executing it. The repay is the smallest of `repayAmount`, the book's debt, and the amount that restores target health, plus one wei on partials. If the remaining debt would be below `dustDebt`, the whole book debt is repaid. Seize is repay divided by the discounted price, capped at the account's collateral. Reverts with ZeroAmount for a zero `repayAmount` and NothingToSeize when the discounted price rounds to 0.

<Note>
  Reads the oracle price, so it reverts with PriceUnavailable when the book is HALTED, in CA\_WINDOW, or SEQ\_DOWN. The protocol share is taken only from the bonus above fair value.
</Note>

**Input parameters**

| Name        | Type      | Description                          |
| ----------- | --------- | ------------------------------------ |
| account     | `address` | The account to quote.                |
| bookId      | `uint16`  | The book to quote.                   |
| repayAmount | `uint256` | The liquidator's maximum USDG repay. |

**Return values**

| Type       | Description                                                                                                                                                                                                                 |
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `LiqQuote` | q: LiqQuote: `repay` (USDG the liquidator pays), `seizeToLiquidator` and `seizeToProtocol` (collateral tokens), and `discount` (WAD). All zero except `discount` when the account has no collateral or no debt in the book. |

### discountOf

```solidity theme={"system"}
function discountOf(address account, uint16 bookId) public view returns (uint256)
```

Returns the current Dutch auction discount for an account in a book. The discount is 0 until the block after `liquidatableSince`, then rises linearly to `maxLiqDiscount` at `auctionWindow` seconds. Reverts with InvalidAuctionWindow if the registry window is 0.

**Input parameters**

| Name    | Type      | Description                          |
| ------- | --------- | ------------------------------------ |
| account | `address` | The account to check.                |
| bookId  | `uint16`  | The book whose max discount applies. |

**Return values**

| Type      | Description      |
| --------- | ---------------- |
| `uint256` | Discount in WAD. |

## Events

### Liquidated

```solidity theme={"system"}
event Liquidated(
    address indexed account,
    uint16 indexed bookId,
    address indexed liquidator,
    uint256 repay,
    uint256 seizeToLiquidator,
    uint256 seizeToProtocol,
    uint256 discount
)
```

Fires after a settled liquidation with the repay, both seize amounts, and the discount applied.

## Errors

### ZeroAddress

```solidity theme={"system"}
error ZeroAddress()
```

A constructor address was zero.

### NotCore

```solidity theme={"system"}
error NotCore()
```

Only Stockline can call liquidate.

### ZeroAmount

```solidity theme={"system"}
error ZeroAmount()
```

The requested repay amount was zero.

### InvalidAuctionWindow

```solidity theme={"system"}
error InvalidAuctionWindow()
```

The registry auction window is 0.

### NothingToSeize

```solidity theme={"system"}
error NothingToSeize()
```

The quote produced no repay or no seize, or the discounted price rounded to 0.
