# Health factor and liquidation

{% hint style="danger" %}
All asset parameters including LTV ratio and liquidation threshold are **example values** to facilitate example calculations. If you need to look at actual values used on Strobe Protocol, please have a look at [values](https://strobe-protocol.gitbook.io/strobe-protocol/money-market/asset-parameters/values "mention")  page under [asset-parameters](https://strobe-protocol.gitbook.io/strobe-protocol/money-market/asset-parameters "mention") section.
{% endhint %}

{% hint style="info" %}
At present, liquidations are handled automatically with manual liquidations coming in the future.
{% endhint %}

## Health factor

For liquidation, the *health factor* is calculated instead of [collateralization ratio](https://strobe-protocol.gitbook.io/strobe-protocol/money-market/overcollateralization-and-collateralization-ratio).

$$
\text{Health factor} = \frac{\sum (\text{LTV ratio}\_i \times \text{Collateral Value in USD}\_i)}{\sum \text{Borrowing Value in USD}\_i}
$$

The health factor is always bigger than or equal to the collateralization ratio because we take the liquidation threshold out, which always deflates the result of the equation. When they are equal, they will always be at the positive infinity.

Using the information from the table above, the health factor would be:

$$
\text{Health factor} = \frac{(0.9 \times 5) + (0.9 \times 1)}{2 + 0.3} = \frac{(4.5) + (0.9)}{2.3} = \frac{5.4}{2.3} = 2.347
$$

As shown, the health factor is greater than the collateralization ratio. The difference between the health factor and the collateralization ratio is a buffer for the borrowers. When the health factor goes below 1, a portion of debt may be liquidated to bring the user's health ratio back to 1.

## Amount to liquidate

The liquidation must happen before the health factor drops 'too much' below 1. What would be 'too much'?: When the value of the collateral is so little that the health factor cannot be recovered back to 1 even after being liquidated. How do we derive the equation? We can use the same health factor equation we have above.

Let's give an example using the variables below, and let's say we are repaying Asset 2's debt and taking away Asset 1 collateral during liquidation:

| Parameter               | Asset 1 | Asset 2 |
| ----------------------- | ------- | ------- |
| LTV ratio               | 80%     | 85%     |
| Deposited amount in USD | 5.4 USD | 0.1 USD |
| Borrowed amount in USD  | 0.1 USD | 5 USD   |
| Liquidation bonus       | 6%      | 7%      |

Let's expand:

$$
\text{Health factor} = \frac{\sum (\text{LTV ratio}\_i \times \text{Collateral Value in USD}\_i)}{\sum \text{Borrowing Value in USD}\_i}
$$

into

$$
\text{HF}*{\text{after liquidation}} = \frac{LTV*{Asset1} \times (CV\_{Asset1} - RV\_{Asset2}(1 + LF\_{Asset2})) + LTV\_{Asset2} \times CV\_{Asset2}}{DV\_{Asset1} + DV\_{Asset2} - RV\_{Asset2}}
$$

where

* $$\text{HF}\_{\text{after liquidation}}$$ is the health factor *after* liquidation takes place.
* $$\text{LTV}\_{asset}$$ is the LTV ratio of an asset.
* $$\text{CV}\_{asset}$$ is the value of an asset deposited as a collateral in USD.
* $$\text{RV}\_{asset}$$ is the value of repayment on the debt of an asset in USD.
* $$\text{DV}\_{asset}$$ is the value of the debt of an asset in USD.
* $$\text{LF}\_{asset}$$ is the liquidation bonus factor of an asset.

Notice that the equation is just an equation for the health factor in the post-liquidation state. $$LTV\_{Asset1} \times (CV\_{Asset1} - RV\_{Asset2}(1 + LF\_{Asset1}))$$ represents the discounted collateral left after a part of Asset 1 collateral including the liquidation bonus is captured in exchange for the Asset 2 debt repayment by the liquidator. $$LTV\_{Asset2} \times CV\_{Asset2}$$ represents the discounted Asset 2 collateral. $$DV\_{Asset1} + DV\_{Asset2} - RV\_{Asset2}$$ represents total debt after the Asset 2 debt repayment.

Let's rearrange the equation so we can solve for $$\text{RV}\_{Asset2}$$:

$$
\text{HF}*{\text{after liquidation}} = \frac{LTV*{Asset1} \times (CV\_{Asset1} - RV\_{Asset2}(1 + LF\_{Asset1})) + LTV\_{Asset2} \times CV\_{Asset2}}{DV\_{Asset1} + DV\_{Asset2} - RV\_{Asset2}}
$$

$$
\implies \text{HF}*{\text{after liquidation}}(DV*{Asset1}) + \text{HF}*{\text{after liquidation}}(DV*{Asset2}) - \text{HF}*{\text{after liquidation}}(RV*{Asset2})
$$

$$
\= (LTV\_{Asset1})(CV\_{Asset1}) - (RV\_{Asset2})(LTV\_{Asset1})(1 + LF\_{Asset1})) + (LTV\_{Asset2})(CV\_{Asset2})
$$

$$
\implies (RV\_{Asset2})(LTV\_{Asset1})(1 + LF\_{Asset1})) - \text{HF}*{\text{after liquidation}}(RV*{Asset2})
$$

$$
\= -\text{HF}*{\text{after liquidation}}(DV*{Asset1}) -\text{HF}*{\text{after liquidation}}(DV*{Asset2}) + (LTV\_{Asset1})(CV\_{Asset1}) + (LTV\_{Asset2})(CV\_{Asset2})
$$

$$
\implies (RV\_{Asset2})((LTV\_{Asset1})(1 + LF\_{Asset1})) - \text{HF}\_{\text{after liquidation}})
$$

$$
\= -\text{HF}*{\text{after liquidation}}(DV*{Asset1} + DV\_{Asset2}) + \sum\limits\_{i}{(LTV\_i \times CV\_i)}
$$

$$
\implies (RV\_{Asset2})((LTV\_{Asset1})(1 + LF\_{Asset1})) - \text{HF}\_{\text{after liquidation}})
$$

$$
\= -\text{HF}*{\text{after liquidation}}(\sum\limits*{i}{DV\_i}) + \sum\limits\_{i}{(LTV\_i \times CV\_i)}
$$

$$
\implies RV\_{Asset2} = \frac{-\text{HF}*{\text{after liquidation}}(\sum\limits*{i}{DV\_i}) + \sum\limits\_{i}{(LTV\_i \times CV\_i)}}{((LTV\_{Asset1})(1 + LF\_{Asset1})) - \text{HF}\_{\text{after liquidation}}}
$$

Now that we have the equation ready, substitute the variables. We know that:

$$
\text{HF}\_{\text{after liquidation}} \le 1
$$

so let's substitute:

$$
\text{HF}\_{\text{after liquidation}} = 1
$$

in this equation below:

$$
RV\_{Asset2} = \frac{-(1)(5 + 0.1) + (0.8 \times 5.4 + 0.85 \times 0.1)}{((0.8)(1 + 0.06)) - 1}
$$

We can run this Python script to confirm the result:

```py
RV_Asset2_NUMERATOR = (-1 * (0.1 + 5) + (0.8 * 5.4 + 0.85 * 0.1))
RV_Asset2_DENOMINATOR = ((0.8) * (1 + 0.06) - 1)
RV_Asset2 = RV_Asset2_NUMERATOR / RV_Asset2_DENOMINATOR

print(RV_Asset2_NUMERATOR)
print(RV_Asset2_DENOMINATOR)
print(RV_Asset2)

HF_CHECK_NUMERATOR = 0.8 * (5.4 - RV_Asset2 * (1 + 0.06)) + 0.85 * 0.1
HF_CHECK_DENOMINATOR = 5.1 - RV_Asset2
HF = HF_CHECK_NUMERATOR/HF_CHECK_DENOMINATOR

print(HF_CHECK_NUMERATOR)
print(HF_CHECK_DENOMINATOR)
print(HF)
```

The console will print:

```bash
-0.6949999999999994 # RV_Asset2_NUMERATOR
-0.1519999999999999 # RV_Asset2_DENOMINATOR
4.57236842105263 # RV_Asset2
0.5276315789473698 # HF_CHECK_NUMERATOR
0.5276315789473696 # HF_CHECK_DENOMINATOR
1.0000000000000004 # HF
```

Disregarding the floating point number errors, we can confirm that we deduced the correct $$RV\_{Asset2}$$.

We can now generalize the equation further, from:

$$
RV\_{Asset2} = \frac{-\text{HF}*{\text{after liquidation}}(\sum\limits*{i}{DV\_i}) + \sum\limits\_{i}{(LTV\_i \times CV\_i)}}{((LTV\_{Asset1})(1 + LF\_{Asset1})) - \text{HF}\_{\text{after liquidation}}}
$$

to:

$$
RV\_{\text{repaid asset}} = \frac{-\text{HF}*{\text{after liquidation}}(\sum\limits*{i}{DV\_i}) + \sum\limits\_{i}{(LTV\_i \times CV\_i)}}{(LTV\_{\text{liquidated asset}})(1 + LF\_{\text{liquidated asset}}) - \text{HF}\_{\text{after liquidation}}}
$$

However, in some cases where more than two assets are borrowed or deposited as collaterals, $$RV\_{\text{repaid asset}}$$ can exceed $$DV\_{\text{repaid asset}}$$ or $$CV\_{\text{liquidated asset}}$$. In either case, repayment is not valid because repayment amount must be smaller than the debt and must be smaller than the captured collateral amount plus liquidation bonus in order for the liquidation to work.

In such cases, $$\text{Final Repaid Amount}*{\text{repaid asset}}$$ needs to be reduced down to $$min(RV*{\text{repaid asset}}, min(DV\_{\text{repaid asset}}, \frac{CV\_{\text{liquidated asset}}}{1 + LF\_{\text{liquidated asset}}}))$$.

Below, we review all of the different possibilities for liquidation. We use $$\text{Asset2} = \text{repaid asset}$$, and $$\text{Asset1} = \text{liquidated asset}$$.

1. $$\text{HF} \ge 1$$

| Parameter               | Asset 1 | Asset 2 |
| ----------------------- | ------- | ------- |
| LTV ratio               | 80%     | 85%     |
| Deposited amount in USD | 5.4 USD | 0.1 USD |
| Borrowed amount in USD  | 0.1 USD | 0 USD   |
| Liquidation bonus       | 6%      | 7%      |

In this case, there can't be a liquidation on this account, because the user's position is healthy:

$$
\text{HF} = \frac{0.8 \times 5.4 + 0.85 \times 0.1}{0.1} \ge 1
$$

2. $$\text{HF} \le 1$$ and $$RV\_{\text{repaid asset}} = min(RV\_{\text{repaid asset}}, min(DV\_{\text{repaid asset}}, \frac{CV\_{\text{liquidated asset}}}{1 + LF\_{\text{liquidated asset}}}))$$

| Parameter               | Asset 1 | Asset 2 |
| ----------------------- | ------- | ------- |
| LTV ratio               | 80%     | 85%     |
| Deposited amount in USD | 5.4 USD | 0.1 USD |
| Borrowed amount in USD  | 0.1 USD | 5 USD   |
| Liquidation bonus       | 6%      | 7%      |

This is the example we used previously. HF is smaller than 1:

$$
\text{HF} = \frac{0.8 \times 5.4 + 0.85 \times 0.1}{0.1 + 5} = \frac{4.405}{5.1} \le 1
$$

and $$RV\_{\text{repaid asset}} = min(RV\_{\text{repaid asset}}, min(DV\_{\text{repaid asset}}, \frac{CV\_{\text{liquidated asset}}}{1 + LF\_{\text{liquidated asset}}})$$:

$$
RV\_{\text{repaid asset}} = \frac{-\text{HF}*{\text{after liquidation}}(\sum\limits*{i}{DV\_i}) + \sum\limits\_{i}{(LTV\_i \times CV\_i)}}{((LTV\_{\text{liquidated asset}})(1 + LF\_{\text{liquidated asset}})) - \text{HF}\_{\text{after liquidation}}}
$$

$$
\implies RV\_{\text{Asset2}} = \frac{-(1)(5 + 0.1) + (0.8 \times 5.4 + 0.85 \times 0.1)}{((0.8)(1 + 0.06)) - 1}
$$

$$
\implies RV\_{\text{Asset2}} = 4.57236842105263...
$$

3. $$\text{HF} \le 1$$ and $$\text{Final Repaid Amount}*{\text{repaid asset}} = \frac{CV*{\text{liquidated asset}}}{1 + LF\_{\text{liquidated asset}}} = min(RV\_{\text{repaid asset}}, min(DV\_{\text{repaid asset}}, \frac{CV\_{\text{liquidated asset}}}{1 + LF\_{\text{liquidated asset}}})$$

Put simply, this is when the amount of collateral to be liquidated isn't enough to bring HF back to 1, because the user has multiple collaterals.

| Parameter               | Asset 1 | Asset 2 |
| ----------------------- | ------- | ------- |
| LTV ratio               | 80%     | 85%     |
| Deposited amount in USD | 3 USD   | 2.5 USD |
| Borrowed amount in USD  | 0.1 USD | 5 USD   |
| Liquidation bonus       | 6%      | 7%      |

Calculate HF first:

$$
\text{HF} = \frac{0.8 \times 3 + 0.85 \times 2.5}{0.1 + 5} = \frac{4.525}{5.1} \le 1
$$

$$
RV\_{\text{Asset2}} = \frac{-(1)(5 + 0.1) + (0.8 \times 3 + 0.85 \times 2.5)}{((0.8)(1 + 0.06)) - 1} = 3.7828947368421026...
$$

$$
3.7828947368421026... \ge CV\_{Asset1} \because CV\_{Asset1} = 3
$$

$$
3.7828947368421026... \le DV\_{Asset2} \because DV\_{Asset2} = 5
$$

$$
\implies \text{Final Repaid Amount}*{Asset2} = \frac{CV*{\text{Asset1}}}{1 + LF\_{\text{Asset1}}} = min(RV\_{\text{repaid asset}}, min(DV\_{\text{repaid asset}}, \frac{CV\_{\text{Asset1}}}{1 + LF\_{\text{Asset1}}})
$$

Therefore, we can only repay $$\frac{3}{1 + LF\_{\text{Asset1}}} = \frac{3}{1.06} = 2.830188679....$$ in this scenario. HF will not be fully restored back to 1. The denominator exists to account for the liquidation bonus, so that we can get 3 USD as the value of captured collateral plus liquidation bonus, which is the maximum we can get from the deposited Asset 2 collateral. But liquidation should still run regardless.

Here's $$\text{HF}\_{\text{after liquidation}}$$:

$$
\text{HF}*{\text{after liquidation}} = \frac{LTV*{Asset1} \times (CV\_{Asset1} - (\text{Final Repaid Amount}*{Asset2})(1 + LF*{\text{Asset1}})) + LTV\_{Asset2} \times CV\_{Asset2}}{DV\_{Asset1} + DV\_{Asset2} - \text{Final Repaid Amount}\_{Asset2}}
$$

$$
\= \frac{0 + LTV\_{Asset2} \times CV\_{Asset2}}{DV\_{Asset1} + DV\_{Asset2} - \text{Final Repaid Amount}\_{Asset2}}
$$

$$
\= \frac{0 + 2.5 \times 0.85}{5.1 - 2.5} = \frac{2.125}{2.6} = 0.81730769....
$$

Notice that we will need to liquidate the other collateral to fully recover the health factor back to 1.

4. $$DV\_{\text{repaid asset}} = min(RV\_{\text{repaid asset}}, min(DV\_{\text{repaid asset}}, \frac{CV\_{\text{liquidated asset}}}{1 + LF\_{\text{liquidated asset}}})$$

This is when the amount of debt to be repaid isn't enough to bring HF back to 1 because the user has multiple assets in debt, and this amount is smaller than the collateral that can be captured.

| Parameter               | Asset 1 | Asset 2 |
| ----------------------- | ------- | ------- |
| LTV ratio               | 80%     | 85%     |
| Deposited amount in USD | 5.4 USD | 0.1 USD |
| Borrowed amount in USD  | 2.5 USD | 2.6 USD |
| Liquidation bonus       | 6%      | 7%      |

Again, we are repaying Asset 2 and liquidating Asset 1:

$$
RV\_{\text{Asset 2}} = \frac{-(1)(2.5 + 2.6) + (0.8 \times 5.4 + 0.85 \times 0.1)}{((0.8)(1 + 0.06)) - 1}
$$

$$
\= 4.57236842105263...
$$

This means we want to liquidate 4.57236842105263 USD equivalent of Asset 2, but we cannot because the user only has 2.6 USD worth of Asset 2 in debt, thus:

$$
\text{Final repaid amount}\_{Asset2} = min(4.57236842105263, min(2.6, \frac{5.4}{1.06}))
$$

$$
\= 2.6
$$

This won't recover HF back to 1. It will require another liquidation to be run to repay Asset 2 and liquidate Asset 1.
