# Math behind Liquidations

We have made our own liquidation formula to allow users to choose their preferred liquidation target. Some might want to be liquidated close to the maximum, and some close to the minimum. As the price goes down, every user gets liquidated to the same position, as there is a limit on how far stepped liquidations go, as we don't want the liquidation fees to be less than gas and swap fees.

<table><thead><tr><th>Value</th><th width="428.3333333333333">Description</th><th>Possible Values</th></tr></thead><tbody><tr><td>e</td><td>1 ETH, aka 10¹⁸ wei</td><td>1 ETH</td></tr><tr><td>debt</td><td>User's debt value in dollars</td><td>0 - ∞</td></tr><tr><td>collateral</td><td>User's real collateral value in dollars (not adjusted to the maximum collateral ratio)</td><td>0 - ∞</td></tr><tr><td>health</td><td>Target health ratio</td><td>0 - 1 ETH</td></tr><tr><td>mcr</td><td>Maximum collateral ratio</td><td>0 - 1 ETH</td></tr><tr><td>fee</td><td>Liquidation fee added onto collateral</td><td>0 - 1 ETH</td></tr></tbody></table>

<figure><img src="/files/IhhQBldT5sQbRgC8IkuR" alt=""><figcaption><p>The equation used to determine debt and borrow + fee change needed to reach target health.</p></figcaption></figure>

Code we use to calculate the debt and collateral change for a user.&#x20;

{% code title="VaultV1.sol" overflow="wrap" lineNumbers="true" %}

```solidity
  // SPDX-License-Identifier: BUSL-1.1

  /// @notice Returns the liquidation numbers for the user
  /// @param user The user id
  /// @return debtChange The debt change
  function _liquidationAmount(
    uint256 user
  ) internal view returns (uint256 debtChange) {
    VaultStorage storage s = _s();
    UserInfo storage info = s.userInfo[user];

    uint256 collateral = _scaleFromAsset(_deposit(user) * _price()) / 1 ether;
    uint256 debt = _debtValueUser(user);

    if (collateral == 0 || debt == 0) return 0;

    uint256 feefullDebt = _withFee(debt);

    if (debt < _stepMinDeposit() || feefullDebt >= collateral) {
      return debt;
    }

    uint256 liquidationFee = s.liquidationFee;
    uint256 maxCollateralRatio = s.maxCollateralRatio;
    uint256 targetHealth = info.healthTarget;

    uint256 scalar = 1 ether * 1 ether;
    uint256 debtHealth = (debt * scalar) / targetHealth;
    uint256 healthScalar = scalar / targetHealth;
    uint256 feeMultiplier = (liquidationFee * maxCollateralRatio) / 1 ether;

    debtChange =
      (debtHealth - collateral * maxCollateralRatio) /
      (healthScalar - maxCollateralRatio - feeMultiplier);
  }

```

{% endcode %}


---

# 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://docs.phase.cash/liquidation/math-behind-liquidations.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.
