๐Ÿ“ 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.

Value
Description
Possible Values

e

1 ETH, aka 10ยนโธ wei

1 ETH

debt

User's debt value in dollars

0 - โˆž

collateral

User's real collateral value in dollars (not adjusted to the maximum collateral ratio)

0 - โˆž

health

Target health ratio

0 - 1 ETH

mcr

Maximum collateral ratio

0 - 1 ETH

fee

Liquidation fee added onto collateral

0 - 1 ETH

The equation used to determine debt and borrow + fee change needed to reach target health.

Code we use to calculate the debt and collateral change for a user.

VaultV1.sol
  // 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);
  }

Last updated