> For the complete documentation index, see [llms.txt](https://docs.ratio.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ratio.finance/liquidation-engine/mechanics.md).

# Mechanics

## Repay to be safe

During periods of high volatility, we take for granted that the borrower is constantly monitoring their Collateral to Debt Ratio and Position's Health. In the background, Ratio's Risk Engine is periodically updating the Dynamic Collateralization Ratio of each of the open Vaults, and the user vault UI will dynamically present the market price at which the vault will be liquidated.

It is easy to calculate what amount needs to be repaid to avoid liquidation. The repay percentage is just the difference between CTD and CR

$$
\text{Repay} = \text{Collateralization Ratio} - \text{Collateral to Debt}
$$

So the borrower has two options to repay some of their debt:&#x20;

1. Borrower goes to the open market and buys some USDr and repays on the platform
2. Borrower mints more LP tokens and deposit directly on the platform

In this way the user would increase their CTD and avoid liquidation.

## Partial Liquidation

As mentioned before, in a liquidation event, the protocol will typically not unwind and liquidate the full position of the borrower; instead just a portion of the collateral will be liquidated and the borrower will return to a healthy position.

The liquidation threshold needed to put the borrower's position back to a healthy position (the Dynamic CR plus a health buffer dependent on the CR of the vault) is calculated by the following formula.

$$
\text{Liquidation %} = \frac{1-\frac{\text{Dynamic CR} + \text{Health Buffer}}{\text{CTD}}}{1-(\text{Dynamic CR + Health Buffer}) \times (1-\text{fees})}
$$

where ***fees*** takes into account the urgency of the protocol to liquidate and unwind the LP position at some cost to account for trading fees and slippage. By doing this small readjustment to the borrower's position, Ratio's Liquidation Engine is recalibrating the position of the borrower, allowing for a risk-on setting.


---

# 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:

```
GET https://docs.ratio.finance/liquidation-engine/mechanics.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.
