> For the complete documentation index, see [llms.txt](https://docs.bsc.lista.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bsc.lista.org/zh-cn/kai-fa-zhe/services/liquidation-logic.md).

# 清算逻辑 (服务)

本页说明**平台服务**如何判定可清算仓位，以及如何执行或支持清算：批处理流程、资格检查、风险控制，以及与清算合约的交互。

## 输入与依赖

* **执行上下文** — 链、运行配置、待处理的市场列表。
* **市场数据** — 抵押品/借款资产、预言机、市场参数（LLTV 等）。
* **风险配置** — 资格规则、最小规模阈值、延迟规则、稳定资产处理方式。
* **价格与状态** — 预言机价格、市场状态、用于去重的近期处理状态 / 缓存。

## 资格判定

* **条件** — 当某仓位在其所属市场满足 `LTV > LLTV` 时可被清算（LLTV 取自市场参数）。等价表述为：**清算触发价格 > 当前市场价格**（触发价格即该仓位变为可清算时的抵押品/借款资产价格比）。
* **LTV** — `LTV = (借款量 × 借款资产预言机价格) / (抵押品数量 × 抵押品预言机价格)`，并采用合约的精度缩放（例如 1e36）。
* **数据** — 使用与合约一致的预言机与市场参数；在执行时重新获取预言机与仓位状态，以避免数据陈旧或被抢跑。

## 批处理流程

1. **运行准备** — 校验运行环境与市场列表。
2. **扫描** — 查询 `borrowedAmount > 0` 的仓位（例如从 `MoolahUserPosition` 获取）。
3. **风险识别** — 针对每个仓位所属市场获取当前价格；计算清算触发价格并与当前市场价格比较；保留触发价格 > 当前价格的仓位。
4. **延迟与阈值** — 应用可配置的延迟与最小规模过滤（见下方风险控制）。
5. **执行** — 对每个符合条件的仓位选择清算路径（主 Liquidator 或预清算合约）并执行；根据已偿还金额与 LIF 计算应扣押的抵押品。
6. **结果与去重** — 记录结果；进行短期去重，使同一仓位在配置的时间窗口内不会被重复处理。

## 风险控制

* **价格有效性** — 跳过价格缺失或异常的市场，避免误清算。
* **延迟** — 对特定仓位可选择延迟处理，以降低波动带来的误判。
* **规模阈值** — 对低于最小规模（例如 gas 或经济性阈值）的仓位不触发清算。
* **去重** — 处理完一个仓位后进行标记（例如写入缓存），使其在短时间内不会被重复提交。

## 可观测性

* 日志应覆盖：批次运行、逐市场处理、逐仓位资格判定，以及结果（已执行 / 已跳过 / 原因）。
* 输出：处理数量、耗时、跳过原因（例如价格无效、低于阈值、去重）。

## 合约侧公式（参考）

服务侧的资格判定应与合约的健康度逻辑保持一致。抽象公式如下：

* **无债务** — `isHealthy = true`。
* **借款量** — 标准市场：`borrowed = convertBorrowSharesToAssets(shares, totalBorrowAssets, totalBorrowShares)`。经纪人市场：`borrowed = getBrokerTotalDebt(borrower, market)`。
* **最大可借** — `maxBorrow = collateral × price × lltv`（price 为抵押品/借款资产的相对价格）。
* **健康度** — `isHealthy = (maxBorrow ≥ borrowed)`。
* **风险仓位** — `isRisky = ¬isHealthy`；这些即为清算候选（仍需通过上述服务侧风险控制）。

## 实现要点

* **舍入** — 与合约的舍入方式保持一致（对用户金额通常向下舍入），避免资格判定出现 off-by-one。
* **Gas / 批处理** — 清算通常由 keeper bot 执行；服务可以只提供可清算列表 API（例如 `GET /api/moolah/liquidation/liquidatable`）与历史记录。
* **预清算** — 当某市场使用外部预清算合约时，识别处于 `preLLTV ≤ LTV < LLTV` 区间的仓位，并在适用情况下将调用方路由至该合约。

关于产品层面的清算说明与示例，请参见[清算](/zh-cn/yin-yan/lista-lending/liquidation.md)。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.bsc.lista.org/zh-cn/kai-fa-zhe/services/liquidation-logic.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
