{
    "Function": "getLiquidityChunk",
    "File": "contracts/libraries/PanopticMath.sol",
    "Parent Contracts": [],
    "High-Level Calls": [
        "LiquidityChunk",
        "Math",
        "TokenId",
        "LiquidityChunk",
        "LiquidityChunk",
        "TokenId",
        "LiquidityChunk",
        "Math",
        "TokenId",
        "LiquidityChunk"
    ],
    "Internal Calls": [],
    "Library Calls": [
        "getLiquidityForAmount1",
        "getLiquidityForAmount0",
        "addTickLower",
        "addTickUpper",
        "addTickLower",
        "createChunk",
        "addTickUpper",
        "asset",
        "optionRatio",
        "asTicks"
    ],
    "Low-Level Calls": [],
    "Code": "function getLiquidityChunk(\n        uint256 tokenId,\n        uint256 legIndex,\n        uint128 positionSize,\n        int24 tickSpacing\n    ) internal pure returns (uint256 liquidityChunk) {\n        // get the tick range for this leg\n        (int24 tickLower, int24 tickUpper) = tokenId.asTicks(legIndex, tickSpacing);\n\n        // Get the amount of liquidity owned by this leg in the univ3 pool in the above tick range\n        // Background:\n        //\n        //  In Uniswap v3, the amount of liquidity received for a given amount of token0 when the price is\n        //  not in range is given by:\n        //     Liquidity = amount0 * (sqrt(upper) * sqrt(lower)) / (sqrt(upper) - sqrt(lower))\n        //  For token1, it is given by:\n        //     Liquidity = amount1 / (sqrt(upper) - sqrt(lower))\n        //\n        //  However, in Panoptic, each position has a asset parameter. The asset is the \"basis\" of the position.\n        //  In TradFi, the asset is always cash and selling a $1000 put requires the user to lock $1000, and selling\n        //  a call requires the user to lock 1 unit of asset.\n        //\n        //  Because Uni v3 chooses token0 and token1 from the alphanumeric order, there is no consistency as to whether token0 is\n        //  stablecoin, ETH, or an ERC20. Some pools may want ETH to be the asset (e.g. ETH-DAI) and some may wish the stablecoin to\n        //  be the asset (e.g. DAI-ETH) so that K asset is moved for puts and 1 asset is moved for calls.\n        //  But since the convention is to force the order always we have no say in this.\n        //\n        //  To solve this, we encode the asset value in tokenId. This parameter specifies which of token0 or token1 is the\n        //  asset, such that:\n        //     when asset=0, then amount0 moved at strike K =1.0001**currentTick is 1, amount1 moved to strike K is 1/K\n        //     when asset=1, then amount1 moved at strike K =1.0001**currentTick is K, amount0 moved to strike K is 1\n        //\n        //  The following function takes this into account when computing the liquidity of the leg and switches between\n        //  the definition for getLiquidityForAmount0 or getLiquidityForAmount1 when relevant.\n        //\n        //\n        uint128 legLiquidity;\n        uint256 amount = uint256(positionSize) * tokenId.optionRatio(legIndex);\n        if (tokenId.asset(legIndex) == 0) {\n            legLiquidity = Math.getLiquidityForAmount0(\n                uint256(0).addTickLower(tickLower).addTickUpper(tickUpper),\n                amount\n            );\n        } else {\n            legLiquidity = Math.getLiquidityForAmount1(\n                uint256(0).addTickLower(tickLower).addTickUpper(tickUpper),\n                amount\n            );\n        }\n\n        // now pack this info into the bit pattern of the uint256 and return it\n        liquidityChunk = liquidityChunk.createChunk(tickLower, tickUpper, legLiquidity);\n    }"
}