{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/libraries/Position.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "library Position {\n\n    using FixedPoint for uint256;\n\n    struct Info {\n        address market; // the market for the position\n        bool isLong; // whether long or short\n        uint leverage; // discrete initial leverage amount\n        uint pricePoint; // pricePointIndex\n        uint256 oiShares; // shares of total open interest on long/short side, depending on isLong value\n        uint256 debt; // total debt associated with this position\n        uint256 cost; // total amount of collateral initially locked; effectively, cost to enter position\n    }\n\n    uint256 constant TWO = 2e18;\n\n    function _initialOi (\n        Info memory _self\n    ) private pure returns (\n        uint initialOi_\n    ) {\n\n        initialOi_ = _self.cost + _self.debt;\n\n    }\n\n    function _oi (\n        Info memory _self,\n        uint256 totalOi,\n        uint256 totalOiShares\n    ) private pure returns (uint256 oi_) {\n\n        oi_ = _self.oiShares\n            .mulDown(totalOi)\n            .divUp(totalOiShares);\n\n    }\n\n    /// @dev Floors to zero, so won't properly compute if self is underwater\n    function _value (\n        Info memory _self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) private pure returns (uint256 val_) {\n\n        uint256 __oi = _oi(_self, totalOi, totalOiShares);\n\n        if (_self.isLong) { // oi * priceFrame - debt\n\n            val_ = __oi.mulDown(priceFrame);\n            val_ -= Math.min(val_, _self.debt); // floor to 0\n\n        } else { // oi * (2 - priceFrame) - debt\n\n            val_ = __oi.mulDown(2e18);\n            val_ -= Math.min(val_, _self.debt + __oi.mulDown(priceFrame)); // floor to 0\n\n        }\n\n    }\n\n    /// @dev is true when position value < 0\n    function _isUnderwater(\n        Info memory _self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) private pure returns (bool isUnder) {\n\n        uint256 __oi = _oi(_self, totalOi, totalOiShares);\n\n        bool _long = _self.isLong;\n\n        if (_long) isUnder = __oi.mulDown(priceFrame) < _self.debt;\n        else isUnder = __oi.mulDown(priceFrame) + _self.debt < ( __oi * 2 );\n\n    }\n\n    /// @dev Floors to _self.debt, so won't properly compute if _self is underwater\n    function _notional (\n        Info memory _self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) private pure returns (uint256 notion) {\n\n        uint256 val = _value(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n        notion = val + _self.debt;\n\n    }\n\n    /// @dev ceils uint256.max if position value <= 0\n    function _openLeverage (\n        Info memory _self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) private pure returns (uint lev) {\n\n        uint val = _value(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n        if (val != 0) {\n\n            uint256 notion = _notional(\n                _self,\n                totalOi,\n                totalOiShares,\n                priceFrame\n            );\n\n            lev = notion.divDown(val);\n\n        } else lev = type(uint256).max;\n\n    }\n\n    /// @dev floors zero if position value <= 0; equiv to 1 / open leverage\n    function _openMargin (\n        Info memory _self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) private pure returns (uint margin) {\n\n        uint notion = _notional(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n        if (notion != 0) {\n\n            uint256 val = _value(\n                _self,\n                totalOi,\n                totalOiShares,\n                priceFrame\n            );\n\n            margin = val.divDown(notion);\n\n        } else margin = 0;\n\n    }\n\n    /// @dev is true when open margin < maintenance margin\n    function _isLiquidatable (\n        Info memory _self,\n        uint256 _totalOi,\n        uint256 _totalOiShares,\n        uint256 _priceFrame,\n        uint256 _marginMaintenance\n    ) private pure returns (\n        bool can_\n    ) {\n\n        uint _val = _value(\n            _self,\n            _totalOi,\n            _totalOiShares,\n            _priceFrame\n        );\n\n        uint _initOi = _initialOi(_self);\n\n        uint _maintenanceMargin = _initOi.mulUp(_marginMaintenance);\n\n        can_ = _val < _maintenanceMargin;\n\n    }\n\n    function _liquidationPrice (\n        Info memory _self,\n        uint256 _totalOi,\n        uint256 _totalOiShares,\n        uint256 _priceEntry,\n        uint256 _marginMaintenance\n    ) private pure returns (uint256 liqPrice) {\n\n        uint256 _posOi = _oi(_self, _totalOi, _totalOiShares);\n        uint256 _posInitialOi = _initialOi(_self);\n\n        uint256 _oiFrame = _posInitialOi.mulUp(_marginMaintenance)\n            .add(_self.debt)\n            .divDown(_posOi);\n\n        if (_self.isLong) liqPrice = _priceEntry.mulUp(_oiFrame);\n        else liqPrice = _priceEntry.mulUp(TWO.sub(_oiFrame));\n\n    }\n\n    function initialOi (\n        Info storage self\n    ) internal view returns (\n        uint256 initialOi_\n    ) {\n\n        Info memory _self = self;\n\n        initialOi_ = _initialOi(_self);\n\n    }\n\n    /// @notice Computes the open interest of a position\n    function oi (\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares\n    ) internal view returns (uint256) {\n\n        Info memory _self = self;\n\n        return _oi(_self, totalOi, totalOiShares);\n\n    }\n\n    /// @notice Computes the value of a position\n    /// @dev Floors to zero, so won't properly compute if self is underwater\n    function value(\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) internal view returns (uint256) {\n\n        Info memory _self = self;\n\n        return _value(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n    }\n\n    /// @notice Whether position is underwater\n    /// @dev is true when position value <= 0\n    function isUnderwater(\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) internal view returns (bool) {\n\n        Info memory _self = self;\n\n        return _isUnderwater(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n    }\n\n    /// @notice Computes the notional of a position\n    /// @dev Floors to _self.debt if value <= 0\n    function notional(\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) internal view returns (uint256) {\n\n        Info memory _self = self;\n\n        return _notional(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n    }\n\n    /// @notice Computes the open leverage of a position\n    /// @dev ceils uint256.max if position value <= 0\n    function openLeverage(\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) internal view returns (uint) {\n\n        Info memory _self = self;\n\n        return _openLeverage(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n    }\n\n    /// @notice Computes the open margin of a position\n    /// @dev floors zero if position value <= 0\n    function openMargin(\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame\n    ) internal view returns (uint) {\n\n        Info memory _self = self;\n\n        return _openMargin(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame\n        );\n\n    }\n\n    /// @notice Whether a position can be liquidated\n    /// @dev is true when value < maintenance margin\n    function isLiquidatable(\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceFrame,\n        uint256 marginMaintenance\n    ) internal view returns (bool) {\n\n        Info memory _self = self;\n\n        return _isLiquidatable(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceFrame,\n            marginMaintenance\n        );\n\n    }\n\n    /// @notice Computes the liquidation price of a position\n    /// @dev price when value < maintenance margin\n    function liquidationPrice(\n        Info storage self,\n        uint256 totalOi,\n        uint256 totalOiShares,\n        uint256 priceEntry,\n        uint256 marginMaintenance\n    ) internal view returns (\n        uint256 liquidationPrice_\n    ) {\n\n        Info memory _self = self;\n\n        liquidationPrice_ = _liquidationPrice(\n            _self,\n            totalOi,\n            totalOiShares,\n            priceEntry,\n            marginMaintenance\n        );\n\n    }\n}"
}