{
    "Function": "_createLegInAMM",
    "File": "contracts/SemiFungiblePositionManager.sol",
    "Parent Contracts": [
        "contracts/multicall/Multicall.sol",
        "contracts/tokens/ERC1155Minimal.sol"
    ],
    "High-Level Calls": [
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "TokenId",
        "LeftRight",
        "LiquidityChunk",
        "LiquidityChunk",
        "TokenId",
        "LiquidityChunk",
        "LeftRight"
    ],
    "Internal Calls": [
        "_burnLiquidity",
        "revert NotEnoughLiquidity()",
        "keccak256(bytes)",
        "_collectAndWritePositionData",
        "_mintLiquidity",
        "_getFeesBase",
        "abi.encodePacked()"
    ],
    "Library Calls": [
        "rightSlot",
        "rightSlot",
        "toLeftSlot",
        "toRightSlot",
        "liquidity",
        "leftSlot",
        "tickLower",
        "toLeftSlot",
        "isLong",
        "tickUpper",
        "leftSlot",
        "rightSlot",
        "tokenType",
        "toRightSlot"
    ],
    "Low-Level Calls": [],
    "Code": "function _createLegInAMM(\n        IUniswapV3Pool _univ3pool,\n        uint256 _tokenId,\n        uint256 _leg,\n        uint256 _liquidityChunk,\n        bool _isBurn\n    ) internal returns (int256 _moved, int256 _itmAmounts, int256 _totalCollected) {\n        uint256 _tokenType = TokenId.tokenType(_tokenId, _leg);\n        // unique key to identify the liquidity chunk in this uniswap pool\n        bytes32 positionKey = keccak256(\n            abi.encodePacked(\n                address(_univ3pool),\n                msg.sender,\n                _tokenType,\n                _liquidityChunk.tickLower(),\n                _liquidityChunk.tickUpper()\n            )\n        );\n\n        // update our internal bookkeeping of how much liquidity we have deployed in the AMM\n        // for example: if this _leg is short, we add liquidity to the amm, make sure to add that to our tracking\n        uint128 updatedLiquidity;\n        uint256 isLong = TokenId.isLong(_tokenId, _leg);\n        uint256 currentLiquidity = s_accountLiquidity[positionKey]; //cache\n\n        unchecked {\n            // did we have liquidity already deployed in Uniswap for this chunk range from some past mint?\n\n            // s_accountLiquidity is a LeftRight. The right slot represents the liquidity currently sold (added) in the AMM owned by the user\n            // the left slot represents the amount of liquidity currently bought (removed) that has been removed from the AMM - the user owes it to a seller\n            // the reason why it is called \"removedLiquidity\" is because long options are created by removing -ie.short selling LP positions\n            uint128 startingLiquidity = currentLiquidity.rightSlot();\n            uint128 removedLiquidity = currentLiquidity.leftSlot();\n            uint128 chunkLiquidity = _liquidityChunk.liquidity();\n\n            if (isLong == 0) {\n                // selling/short: so move from msg.sender *to* uniswap\n                // we're minting more liquidity in uniswap: so add the incoming liquidity chunk to the existing liquidity chunk\n                updatedLiquidity = startingLiquidity + chunkLiquidity;\n\n                /// @dev If the isLong flag is 0=short but the position was burnt, then this is closing a long position\n                /// @dev so the amount of short liquidity should decrease.\n                if (_isBurn) {\n                    removedLiquidity -= chunkLiquidity;\n                }\n            } else {\n                // the _leg is long (buying: moving *from* uniswap to msg.sender)\n                // so we seek to move the incoming liquidity chunk *out* of uniswap - but was there sufficient liquidity sitting in uniswap\n                // in the first place?\n                if (startingLiquidity < chunkLiquidity) {\n                    // the amount we want to move (liquidityChunk.legLiquidity()) out of uniswap is greater than\n                    // what the account that owns the liquidity in uniswap has (startingLiquidity)\n                    // we must ensure that an account can only move its own liquidity out of uniswap\n                    // so we revert in this case\n                    revert Errors.NotEnoughLiquidity();\n                } else {\n                    // we want to move less than what already sits in uniswap, no problem:\n                    updatedLiquidity = startingLiquidity - chunkLiquidity;\n                }\n\n                /// @dev If the isLong flag is 1=long and the position is minted, then this is opening a long position\n                /// @dev so the amount of short liquidity should increase.\n                if (!_isBurn) {\n                    removedLiquidity += chunkLiquidity;\n                }\n            }\n\n            // update the starting liquidity for this position for next time around\n            s_accountLiquidity[positionKey] = uint256(0).toLeftSlot(removedLiquidity).toRightSlot(\n                updatedLiquidity\n            );\n        }\n\n        // track how much liquidity we need to collect from uniswap\n        // add the fees that accumulated in uniswap within the liquidityChunk:\n        {\n            /** if the position is NOT long (selling a put or a call), then _mintLiquidity to move liquidity\n                from the msg.sender to the uniswap v3 pool:\n                Selling(isLong=0): Mint chunk of liquidity in Uniswap (defined by upper tick, lower tick, and amount)\n                       \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                \u25b2     \u250c\u25bc\u2510 liquidityChunk                 \u2502\n                \u2502  \u250c\u2500\u2500\u2534\u2500\u2534\u2500\u2500\u2510                         \u250c\u2500\u2500\u2500\u2534\u2500\u2500\u2510\n                \u2502  \u2502       \u2502                         \u2502      \u2502\n                \u2514\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u25ba                      \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                   Uniswap v3                      msg.sender\n            \n             else: the position is long (buying a put or a call), then _burnLiquidity to remove liquidity from univ3\n                Buying(isLong=1): Burn in Uniswap\n                       \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                \u25b2     \u250c\u253c\u2510                \u2502\n                \u2502  \u250c\u2500\u2500\u2534\u2500\u2534\u2500\u2500\u2510         \u250c\u2500\u2500\u2500\u25bc\u2500\u2500\u2510\n                \u2502  \u2502       \u2502         \u2502      \u2502\n                \u2514\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u25ba      \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                    Uniswap v3      msg.sender \n            */\n            _moved = isLong == 0\n                ? _mintLiquidity(_liquidityChunk, _univ3pool)\n                : _burnLiquidity(_liquidityChunk, _univ3pool); // from msg.sender to Uniswap\n            // add the moved liquidity chunk to amount we need to collect from uniswap:\n\n            // Is this _leg ITM?\n            // if tokenType is 1, and we transacted some token0: then this leg is ITM!\n            if (_tokenType == 1) {\n                // extract amount moved out of UniswapV3 pool\n                _itmAmounts = _itmAmounts.toRightSlot(_moved.rightSlot());\n            }\n            // if tokenType is 0, and we transacted some token1: then this leg is ITM\n            if (_tokenType == 0) {\n                // Add this in-the-money amount transacted.\n                _itmAmounts = _itmAmounts.toLeftSlot(_moved.leftSlot());\n            }\n        }\n\n        // if there was liquidity at that tick before the transaction, collect any accumulated fees\n        if (currentLiquidity.rightSlot() > 0) {\n            _totalCollected = _collectAndWritePositionData(\n                _liquidityChunk,\n                _univ3pool,\n                currentLiquidity,\n                positionKey,\n                _moved,\n                isLong\n            );\n        }\n\n        // position has been touched, update s_accountFeesBase with the latest values from the pool.positions\n        s_accountFeesBase[positionKey] = _getFeesBase(\n            _univ3pool,\n            updatedLiquidity,\n            _liquidityChunk\n        );\n    }"
}