> 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="https://1829638638-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0k7YXwFGMFZcsyqkM4q1%2Fuploads%2Fv1eFIDfMZBGmuKTeA6eH%2Fimage.png?alt=media&amp;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&amp;token=cb9d378f-6a3d-4c42-85b4-6d117f958ad9" 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`.
