> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/aave-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/aave-book/functions/liquidate/_calculatedebt.md).

# \_calculateDebt

## Overview

<figure><img src="/files/0ccGoRTL5H2XZUoohcTt" alt=""><figcaption></figcaption></figure>

### Execution flow

* [x] <mark style="color:orange;">cache + updateState + get health factor</mark>
* [ ] \_calculateDebt
* [ ] validateLiquidationCall
* [ ] getConfigurationData
* [ ] calculateAvailableCollateralToLiquidate
* [ ] <mark style="color:orange;">setBorrowing</mark>
* [ ] <mark style="color:orange;">setUsingAsCollateral</mark>
* [ ] \_burnDebtTokens
* [ ] <mark style="color:orange;">updateInterestRates</mark>
* [ ] <mark style="color:orange;">updateIsolatedDebtIfIsolated</mark>
* [ ] liquidate/burn collateral
* [ ] liquidation fee
* [ ] Wrap-up

## &#x20;\_calculateDebt

Calculates the total debt of the user and the actual amount to liquidate depending on the health factor. Returns:

* userVariableDebt
* userTotalDebt
* actualDebtToLiquidate

<img src="/files/W0Nc2wpTcEptvN9rXtWF" alt="" class="gitbook-drawing">

* [getUserCurrentDebt](/aave-book/functions/repay/get-current-debt.md) will return the user's balance of debt tokens (stable & variable)
* `healthFactor` was obtained previously through `calculateUserAccountData`
* If user's `healthFactor` > 0.95: liquidation close factor is 0.5 (50%.00)
* If user's `healthFactor` <= 0.95: liquidation close factor is 1.0 (100.00%)

{% hint style="info" %}
The close factor determines the maximum liquidatable debt, per valid liquidationCall().&#x20;

close factor = 0.5, means only a maximum of 50% of the debt can be liquidated within that liquidationCall.
{% endhint %}

**On Partial and Full liquidation: `actualDebtToLiquidate`**

If the liquidator's `debtToCover` is > `maxLiquidatableDebt`, set the `actualDebtToLiquidate` to be `maxLiquidatableDebt`.

Else, If the liquidator's `debtToCover` is **<=** `maxLiquidatableDebt`, set the `actualDebtToLiquidate` to be `debtToCover`.


---

# 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://calnix.gitbook.io/aave-book/functions/liquidate/_calculatedebt.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.
