# \_calculateDebt

## Overview

<figure><img src="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2Fv1eFIDfMZBGmuKTeA6eH%2Fimage.png?alt=media&#x26;token=8226c766-002d-4162-bc86-12350dedde39" 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="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2Fa2CetyWEjkKDHUsxRwZE%2Ffile.excalidraw.svg?alt=media&#x26;token=cb9d378f-6a3d-4c42-85b4-6d117f958ad9" alt="" class="gitbook-drawing">

* [getUserCurrentDebt](https://calnix.gitbook.io/aave-book/functions/repay/get-current-debt) 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: 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:

```
GET https://calnix.gitbook.io/aave-book/functions/liquidate/_calculatedebt.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
