{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/types/TokenId.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "library TokenId {\n    using TokenId for uint256;\n\n    // this mask in hex has a 1 bit in each location of the \"isLong\" of the tokenId:\n    uint256 internal constant LONG_MASK =\n        0x100_000000000100_000000000100_000000000100_0000000000000000;\n    // This mask contains zero bits where the poolId is. It is used via & to strip the poolId section from a number, leaving the rest.\n    uint256 internal constant CLEAR_POOLID_MASK =\n        0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_0000000000000000;\n    // This mask is used to clear all bits except for the option ratios\n    uint256 internal constant OPTION_RATIO_MASK =\n        0x0000000000FE_0000000000FE_0000000000FE_0000000000FE_0000000000000000;\n    int256 internal constant BITMASK_INT24 = 0xFFFFFF;\n    // this mask in hex has a 1 bit in each location except in the riskPartner of the 48bits on a position's tokenId:\n    // this RISK_PARTNER_MASK will make sure that two tokens will have the exact same parameters\n    uint256 internal constant RISK_PARTNER_MASK = 0xFFFFFFFFF3FF;\n\n    /*//////////////////////////////////////////////////////////////\n                                DECODING\n    //////////////////////////////////////////////////////////////*/\n\n    /// @notice The Uniswap v3 Pool pointed to by this option position.\n    /// @param self the option position Id\n    /// @return the poolId (Panoptic's uni v3 pool fingerprint) of the Uniswap v3 pool\n    function univ3pool(uint256 self) internal pure returns (uint64) {\n        unchecked {\n            return uint64(self);\n        }\n    }\n\n    /// @notice Get the asset basis for this position.\n    /// @dev which token is the asset - can be token0 (return 0) or token1 (return 1)\n    /// @param self the option position Id\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @dev occupies the leftmost bit of the optionRatio 4 bits slot.\n    /// @dev The final mod: \"% 2\" = takes the leftmost bit of the pattern.\n    /// @return 0 if asset is token0, 1 if asset is token1\n    function asset(uint256 self, uint256 legIndex) internal pure returns (uint256) {\n        unchecked {\n            return uint256((self >> (64 + legIndex * 48)) % 2);\n        }\n    }\n\n    /// @notice Get the number of contracts per leg.\n    /// @param self the option position Id.\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @dev The final mod: \"% 2**7\" = takes the rightmost (2 ** 7 = 128) 7 bits of the pattern.\n    function optionRatio(uint256 self, uint256 legIndex) internal pure returns (uint256) {\n        unchecked {\n            return uint256((self >> (64 + legIndex * 48 + 1)) % 128);\n        }\n    }\n\n    /// @notice Return 1 if the nth leg (leg index `legIndex`) is a long position.\n    /// @param self the option position Id\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return 1 if long; 0 if not long.\n    function isLong(uint256 self, uint256 legIndex) internal pure returns (uint256) {\n        unchecked {\n            return uint256((self >> (64 + legIndex * 48 + 8)) % 2);\n        }\n    }\n\n    /// @notice Get the type of token moved for a given leg (implies a call or put). Either Token0 or Token1.\n    /// @param self the tokenId in the SFPM representing an option position\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return 1 if the token moved is token1 or 0 if the token moved is token0\n    function tokenType(uint256 self, uint256 legIndex) internal pure returns (uint256) {\n        unchecked {\n            return uint256((self >> (64 + legIndex * 48 + 9)) % 2);\n        }\n    }\n\n    /// @notice Get the associated risk partner of the leg index (generally another leg index in the position).\n    /// @notice that returning the riskPartner for any leg is 0 by default, this does not necessarily imply that token 1 (index 0)\n    /// @notice is the risk partner of that leg. We are assuming here that the position has been validated before this and that\n    /// @notice the risk partner of any leg always makes sense in this way. A leg btw. does not need to have a risk partner.\n    /// @notice the point here is that this function is very low level and must be used with utmost care because it comes down\n    /// @notice to the caller to interpret whether 00 means \"no risk partner\" or \"risk partner leg index 0\".\n    /// @notice But in general we can return 00, 01, 10, and 11 meaning the partner is leg 0, 1, 2, or 3.\n    /// @param self the tokenId in the SFPM representing an option position\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the leg index of `legIndex`'s risk partner.\n    function riskPartner(uint256 self, uint256 legIndex) internal pure returns (uint256) {\n        unchecked {\n            return uint256((self >> (64 + legIndex * 48 + 10)) % 4);\n        }\n    }\n\n    /// @notice Get the strike price tick of the nth leg (with index `legIndex`).\n    /// @param self the tokenId in the SFPM representing an option position\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the strike price (the underlying price of the leg).\n    function strike(uint256 self, uint256 legIndex) internal pure returns (int24) {\n        unchecked {\n            return int24(int256(self >> (64 + legIndex * 48 + 12)));\n        }\n    }\n\n    /// @notice Get the width of the nth leg (index `legIndex`). This is half the tick-range covered by the leg (tickUpper - tickLower)/2.\n    /// @dev return as int24 to be compatible with the strike tick format (they naturally go together)\n    /// @param self the tokenId in the SFPM representing an option position\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the width of the position.\n    function width(uint256 self, uint256 legIndex) internal pure returns (int24) {\n        unchecked {\n            return int24(int256((self >> (64 + legIndex * 48 + 36)) % 4096));\n        } // \"% 4096\" = take last (2 ** 12 = 4096) 12 bits\n    }\n\n    /*//////////////////////////////////////////////////////////////\n                                ENCODING\n    //////////////////////////////////////////////////////////////*/\n\n    /// @notice Add the Uniswap v3 Pool pointed to by this option position.\n    /// @param self the option position Id.\n    /// @return the tokenId with the Uniswap V3 pool added to it.\n    function addUniv3pool(uint256 self, uint64 _poolId) internal pure returns (uint256) {\n        unchecked {\n            return self + uint256(_poolId);\n        }\n    }\n\n    /*//////////////////////////////////////////////////////////////\n                                ENCODING\n    //////////////////////////////////////////////////////////////*/\n\n    /// @notice Add the asset basis for this position.\n    /// @param self the option position Id.\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @dev occupies the leftmost bit of the optionRatio 4 bits slot\n    /// @dev The final mod: \"% 2\" = takes the rightmost bit of the pattern\n    /// @return the tokenId with numerarire added to the incoming leg index\n    function addAsset(\n        uint256 self,\n        uint256 _asset,\n        uint256 legIndex\n    ) internal pure returns (uint256) {\n        unchecked {\n            return self + (uint256(_asset % 2) << (64 + legIndex * 48));\n        }\n    }\n\n    /// @notice Add the number of contracts to leg index `legIndex`.\n    /// @param self the option position Id\n    /// @param legIndex the leg index of the position (in {0,1,2,3})\n    /// @dev The final mod: \"% 128\" = takes the rightmost (2 ** 7 = 128) 7 bits of the pattern.\n    /// @return the tokenId with optionRatio added to the incoming leg index\n    function addOptionRatio(\n        uint256 self,\n        uint256 _optionRatio,\n        uint256 legIndex\n    ) internal pure returns (uint256) {\n        unchecked {\n            return self + (uint256(_optionRatio % 128) << (64 + legIndex * 48 + 1));\n        }\n    }\n\n    /// @notice Add \"isLong\" parameter indicating whether a leg is long (isLong=1) or short (isLong=0)\n    /// @notice returns 1 if the nth leg (leg index n-1) is a long position.\n    /// @param self the option position Id\n    /// @param _isLong whether the leg is long\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the tokenId with isLong added to its relevant leg\n    function addIsLong(\n        uint256 self,\n        uint256 _isLong,\n        uint256 legIndex\n    ) internal pure returns (uint256) {\n        unchecked {\n            return self + ((_isLong % 2) << (64 + legIndex * 48 + 8));\n        }\n    }\n\n    /// @notice Add the type of token moved for a given leg (implies a call or put). Either Token0 or Token1.\n    /// @param self the tokenId in the SFPM representing an option position\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the tokenId with tokenType added to its relevant leg.\n    function addTokenType(\n        uint256 self,\n        uint256 _tokenType,\n        uint256 legIndex\n    ) internal pure returns (uint256) {\n        unchecked {\n            return self + (uint256(_tokenType % 2) << (64 + legIndex * 48 + 9));\n        }\n    }\n\n    /// @notice Add the associated risk partner of the leg index (generally another leg in the overall position).\n    /// @param self the tokenId in the SFPM representing an option position\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the tokenId with riskPartner added to its relevant leg.\n    function addRiskPartner(\n        uint256 self,\n        uint256 _riskPartner,\n        uint256 legIndex\n    ) internal pure returns (uint256) {\n        unchecked {\n            return self + (uint256(_riskPartner % 4) << (64 + legIndex * 48 + 10));\n        }\n    }\n\n    /// @notice Add the strike price tick of the nth leg (index `legIndex`).\n    /// @param self the tokenId in the SFPM representing an option position.\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the tokenId with strike price tick added to its relevant leg\n    function addStrike(\n        uint256 self,\n        int24 _strike,\n        uint256 legIndex\n    ) internal pure returns (uint256) {\n        unchecked {\n            return self + uint256((int256(_strike) & BITMASK_INT24) << (64 + legIndex * 48 + 12));\n        }\n    }\n\n    /// @notice Add the width of the nth leg (index `legIndex`). This is half the tick-range covered by the leg (tickUpper - tickLower)/2.\n    /// @param self the tokenId in the SFPM representing an option position.\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @return the tokenId with width added to its relevant leg\n    function addWidth(\n        uint256 self,\n        int24 _width,\n        uint256 legIndex\n    ) internal pure returns (uint256) {\n        // % 4096 -> take 12 bits from the incoming 24 bits (there's no uint12)\n        unchecked {\n            return self + (uint256(uint24(_width) % 4096) << (64 + legIndex * 48 + 36));\n        }\n    }\n\n    /// @notice Add a leg to the tokenId.\n    /// @param self the tokenId in the SFPM representing an option position.\n    /// @param legIndex the leg index of this position (in {0,1,2,3})\n    /// @param _optionRatio the relative size of the leg\n    /// @param _asset the asset of the leg\n    /// @param _isLong whether the leg is long\n    /// @param _tokenType the type of token moved for the leg\n    /// @param _riskPartner the associated risk partner of the leg\n    /// @param _strike the strike price tick of the leg\n    /// @param _width the width of the leg\n    /// @return tokenId the tokenId with the leg added\n    function addLeg(\n        uint256 self,\n        uint256 legIndex,\n        uint256 _optionRatio,\n        uint256 _asset,\n        uint256 _isLong,\n        uint256 _tokenType,\n        uint256 _riskPartner,\n        int24 _strike,\n        int24 _width\n    ) internal pure returns (uint256 tokenId) {\n        tokenId = addOptionRatio(self, _optionRatio, legIndex);\n        tokenId = addAsset(tokenId, _asset, legIndex);\n        tokenId = addIsLong(tokenId, _isLong, legIndex);\n        tokenId = addTokenType(tokenId, _tokenType, legIndex);\n        tokenId = addRiskPartner(tokenId, _riskPartner, legIndex);\n        tokenId = addStrike(tokenId, _strike, legIndex);\n        tokenId = addWidth(tokenId, _width, legIndex);\n    }\n\n    /*//////////////////////////////////////////////////////////////\n                                HELPERS\n    //////////////////////////////////////////////////////////////*/\n\n    /// @notice Flip all the `isLong` positions in the legs in the `tokenId` option position.\n    /// @dev uses XOR on existing isLong bits.\n    /// @dev useful during burning an option position. So we need to take an existing tokenId but now burn it.\n    /// The way to do this is to simply flip it to a short instead.\n    /// @param self the tokenId in the SFPM representing an option position.\n    function flipToBurnToken(uint256 self) internal pure returns (uint256) {\n        unchecked {\n            // NOTE: This is a hack to avoid blowing up the contract size.\n            // We copy the logic from the countLegs function, using it here adds 5K to the contract size with IR for some reason\n            // Strip all bits except for the option ratios\n            uint256 optionRatios = self & OPTION_RATIO_MASK;\n\n            // The legs are filled in from least to most significant\n            // Each comparison here is to the start of the next leg's option ratio\n            // Since only the option ratios remain, we can be sure that no bits above the start of the inactive legs will be 1\n            if (optionRatios < 2 ** 64) {\n                optionRatios = 0;\n            } else if (optionRatios < 2 ** 112) {\n                optionRatios = 1;\n            } else if (optionRatios < 2 ** 160) {\n                optionRatios = 2;\n            } else if (optionRatios < 2 ** 208) {\n                optionRatios = 3;\n            } else {\n                optionRatios = 4;\n            }\n\n            // We need to ensure that only active legs are flipped\n            // In order to achieve this, we shift our long bit mask to the right by (4-# active legs)\n            // i.e the whole mask is used to flip all legs with 4 legs, but only the first leg is flipped with 1 leg so we shift by 3 legs\n            // We also clear the poolId area of the mask to ensure the bits that are shifted right into the area don't flip and cause issues\n            return self ^ ((LONG_MASK >> (48 * (4 - optionRatios))) & CLEAR_POOLID_MASK);\n        }\n    }\n\n    /// @notice Get the number of longs in this option position.\n    /// @notice count the number of legs (out of a maximum of 4) that are long positions.\n    /// @param self the tokenId in the SFPM representing an option position.\n    /// @return the number of long positions (in the range {0,...,4}).\n    function countLongs(uint256 self) internal pure returns (uint256) {\n        unchecked {\n            return self.isLong(0) + self.isLong(1) + self.isLong(2) + self.isLong(3);\n        }\n    }\n\n    /// @notice Get the option position's nth leg's (index `legIndex`) tick ranges (lower, upper).\n    /// @dev NOTE does not extract liquidity which is the third piece of information in a LiquidityChunk.\n    /// @param self the option position id.\n    /// @param legIndex the leg index of the position (in {0,1,2,3}).\n    /// @param tickSpacing the tick spacing of the underlying Univ3 pool.\n    /// @return legLowerTick the lower tick of the leg/liquidity chunk.\n    /// @return legUpperTick the upper tick of the leg/liquidity chunk.\n    function asTicks(\n        uint256 self,\n        uint256 legIndex,\n        int24 tickSpacing\n    ) internal pure returns (int24 legLowerTick, int24 legUpperTick) {\n        unchecked {\n            int24 selfWidth = self.width(legIndex);\n            int24 selfStrike = self.strike(legIndex);\n\n            // The max/min ticks that can be initialized are the closest multiple of tickSpacing to the actual max/min tick abs()=887272\n            // Dividing and multiplying by tickSpacing rounds down and forces the tick to be a multiple of tickSpacing\n            int24 minTick = (Constants.MIN_V3POOL_TICK / tickSpacing) * tickSpacing;\n            int24 maxTick = (Constants.MAX_V3POOL_TICK / tickSpacing) * tickSpacing;\n\n            // The width is from lower to upper tick, the one-sided range is from strike to upper/lower\n            int24 oneSidedRange = (selfWidth * tickSpacing) / 2;\n\n            (legLowerTick, legUpperTick) = (selfStrike - oneSidedRange, selfStrike + oneSidedRange);\n\n            // Revert if the upper/lower ticks are not multiples of tickSpacing\n            // Revert if the tick range extends from the strike outside of the valid tick range\n            // These are invalid states, and would revert silently later in `univ3Pool.mint`\n            if (\n                legLowerTick % tickSpacing != 0 ||\n                legUpperTick % tickSpacing != 0 ||\n                legLowerTick < minTick ||\n                legUpperTick > maxTick\n            ) revert Errors.TicksNotInitializable();\n        }\n    }\n\n    /// @notice Return the number of active legs in the option position.\n    /// @param self the option position Id (tokenId).\n    /// @dev ASSUMPTION: There is at least 1 leg in this option position.\n    /// @dev ASSUMPTION: For any leg, the option ratio is always > 0 (the leg always has a number of contracts associated with it).\n    /// @return the number of legs in the option position.\n    function countLegs(uint256 self) internal pure returns (uint256) {\n        // Strip all bits except for the option ratios\n        uint256 optionRatios = self & OPTION_RATIO_MASK;\n\n        // The legs are filled in from least to most significant\n        // Each comparison here is to the start of the next leg's option ratio section\n        // Since only the option ratios remain, we can be sure that no bits above the start of the inactive legs will be 1\n        if (optionRatios < 2 ** 64) {\n            return 0;\n        } else if (optionRatios < 2 ** 112) {\n            return 1;\n        } else if (optionRatios < 2 ** 160) {\n            return 2;\n        } else if (optionRatios < 2 ** 208) {\n            return 3;\n        }\n        return 4;\n    }\n\n    /// @notice Clear a leg in an option position with index `i`.\n    /// @dev set bits of the leg to zero. Also sets the optionRatio and asset to zero of that leg.\n    /// @dev NOTE it's important that the caller fills in the leg details after.\n    /// @dev  - optionRatio is zeroed\n    /// @dev  - asset is zeroed\n    /// @dev  - width is zeroed\n    /// @dev  - strike is zeroed\n    /// @dev  - tokenType is zeroed\n    /// @dev  - isLong is zeroed\n    /// @dev  - riskPartner is zeroed\n    /// @param self the tokenId to reset the leg of\n    /// @param i the leg index to reset, in {0,1,2,3}\n    /// @return `self` with the `i`th leg zeroed including optionRatio and asset.\n    function clearLeg(uint256 self, uint256 i) internal pure returns (uint256) {\n        if (i == 0)\n            return self & 0xFFFFFFFFFFFF_FFFFFFFFFFFF_FFFFFFFFFFFF_000000000000_FFFFFFFFFFFFFFFF;\n        if (i == 1)\n            return self & 0xFFFFFFFFFFFF_FFFFFFFFFFFF_000000000000_FFFFFFFFFFFF_FFFFFFFFFFFFFFFF;\n        if (i == 2)\n            return self & 0xFFFFFFFFFFFF_000000000000_FFFFFFFFFFFF_FFFFFFFFFFFF_FFFFFFFFFFFFFFFF;\n        if (i == 3)\n            return self & 0x000000000000_FFFFFFFFFFFF_FFFFFFFFFFFF_FFFFFFFFFFFF_FFFFFFFFFFFFFFFF;\n\n        return self;\n    }\n\n    /*//////////////////////////////////////////////////////////////\n                               VALIDATION\n    //////////////////////////////////////////////////////////////*/\n\n    /// @notice Validate an option position and all its active legs; return the underlying AMM address.\n    /// @dev used to validate a position tokenId and its legs.\n    /// @param self the option position id.\n    /// @return the first 64 bits of the underlying Uniswap V3 address.\n    function validate(uint256 self) internal pure returns (uint64) {\n        if (self.optionRatio(0) == 0) revert Errors.InvalidTokenIdParameter(1);\n\n        // loop through the 4 (possible) legs in the tokenId `self`\n        unchecked {\n            for (uint256 i = 0; i < 4; ++i) {\n                if (self.optionRatio(i) == 0) {\n                    // final leg in this position identified;\n                    // make sure any leg above this are zero as well\n                    // (we don't allow gaps eg having legs 1 and 4 active without 2 and 3 is not allowed)\n                    if ((self >> (64 + 48 * i)) != 0) revert Errors.InvalidTokenIdParameter(1);\n\n                    break; // we are done iterating over potential legs\n                }\n                // now validate this ith leg in the position:\n\n                // The width cannot be 0; the minimum is 1\n                if ((self.width(i) == 0)) revert Errors.InvalidTokenIdParameter(5);\n                // Strike cannot be MIN_TICK or MAX_TICK\n                if (\n                    (self.strike(i) == Constants.MIN_V3POOL_TICK) ||\n                    (self.strike(i) == Constants.MAX_V3POOL_TICK)\n                ) revert Errors.InvalidTokenIdParameter(4);\n\n                // In the following, we check whether the risk partner of this leg is itself\n                // or another leg in this position.\n                // Handles case where riskPartner(i) != i ==> leg i has a risk partner that is another leg\n                uint256 riskPartnerIndex = self.riskPartner(i);\n                if (riskPartnerIndex != i) {\n                    // Ensures that risk partners are mutual\n                    if (self.riskPartner(riskPartnerIndex) != i)\n                        revert Errors.InvalidTokenIdParameter(3);\n\n                    // Ensures that risk partners have 1) the same asset, and 2) the same ratio\n                    if (\n                        (self.asset(riskPartnerIndex) != self.asset(i)) ||\n                        (self.optionRatio(riskPartnerIndex) != self.optionRatio(i))\n                    ) revert Errors.InvalidTokenIdParameter(3);\n\n                    // long/short status of associated legs\n                    uint256 isLong = self.isLong(i);\n                    uint256 isLongP = self.isLong(riskPartnerIndex);\n\n                    // token type status of associated legs (call/put)\n                    uint256 tokenType = self.tokenType(i);\n                    uint256 tokenTypeP = self.tokenType(riskPartnerIndex);\n\n                    // if the position is the same i.e both long calls, short put's etc.\n                    // then this is a regular position, not a defined risk position\n                    if ((isLong == isLongP) && (tokenType == tokenTypeP))\n                        revert Errors.InvalidTokenIdParameter(4);\n\n                    // if the two token long-types and the tokenTypes are both different (one is a short call, the other a long put, e.g.), this is a synthetic position\n                    // A synthetic long or short is more capital efficient than each leg separated because the long+short premia accumulate proportionally\n                    if ((isLong != isLongP) && (tokenType != tokenTypeP))\n                        revert Errors.InvalidTokenIdParameter(5);\n                }\n            } // end for loop over legs\n        }\n\n        return self.univ3pool();\n    }\n}"
}