Skip to content

Latest commit

 

History

History
290 lines (268 loc) · 11 KB

LiquidationHelper.md

File metadata and controls

290 lines (268 loc) · 11 KB

The Liquidation Helper contract. (LiquidationHelper.sol)

View Source: contracts/mixins/LiquidationHelper.sol

↗ Extends: State ↘ Derived Contracts: LoanClosingsLiquidation, LoanClosingsRollover, LoanMaintenance

LiquidationHelper contract

This contract code comes from bZx. bZx is a protocol for tokenized margin trading and lending https://bzx.network similar to the dYdX protocol.

  • This contract computes the liquidation amount.

Functions


_getLiquidationAmounts

Compute how much needs to be liquidated in order to restore the desired margin (maintenance + 5%). *

function _getLiquidationAmounts(uint256 principal, uint256 collateral, uint256 currentMargin, uint256 maintenanceMargin, uint256 collateralToLoanRate) internal view
returns(maxLiquidatable uint256, maxSeizable uint256, incentivePercent uint256)

Arguments

Name Type Description
principal uint256 The total borrowed amount (in loan tokens).
collateral uint256 The collateral (in collateral tokens).
currentMargin uint256 The current margin.
maintenanceMargin uint256 The maintenance (minimum) margin.
collateralToLoanRate uint256 The exchange rate from collateral to loan tokens. *

Returns

maxLiquidatable The collateral you can get liquidating.

Source Code
function _getLiquidationAmounts(
        uint256 principal,
        uint256 collateral,
        uint256 currentMargin,
        uint256 maintenanceMargin,
        uint256 collateralToLoanRate
    )
        internal
        view
        returns (
            uint256 maxLiquidatable,
            uint256 maxSeizable,
            uint256 incentivePercent
        )
    {
        incentivePercent = liquidationIncentivePercent;
        if (currentMargin > maintenanceMargin || collateralToLoanRate == 0) {
            return (maxLiquidatable, maxSeizable, incentivePercent);
        } else if (currentMargin <= incentivePercent) {
            return (principal, collateral, currentMargin);
        }

        /// 5 percentage points above maintenance.
        uint256 desiredMargin = maintenanceMargin.add(5 ether);

        /// maxLiquidatable = ((1 + desiredMargin)*principal - collateralToLoanRate*collateral) / (desiredMargin - 0.05)
        maxLiquidatable = desiredMargin.add(10**20).mul(principal).div(10**20);
        maxLiquidatable = maxLiquidatable.sub(collateral.mul(collateralToLoanRate).div(10**18));
        maxLiquidatable = maxLiquidatable.mul(10**20).div(desiredMargin.sub(incentivePercent));
        if (maxLiquidatable > principal) {
            maxLiquidatable = principal;
        }

        /// maxSeizable = maxLiquidatable * (1 + incentivePercent) / collateralToLoanRate
        maxSeizable = maxLiquidatable.mul(incentivePercent.add(10**20));
        maxSeizable = maxSeizable.div(collateralToLoanRate).div(100);
        if (maxSeizable > collateral) {
            maxSeizable = collateral;
        }

        return (maxLiquidatable, maxSeizable, incentivePercent);
    }

Contracts