{
    "Function": "_createPositionInAMM",
    "File": "contracts/SemiFungiblePositionManager.sol",
    "Parent Contracts": [
        "contracts/multicall/Multicall.sol",
        "contracts/tokens/ERC1155Minimal.sol"
    ],
    "High-Level Calls": [
        "TokenId",
        "LeftRight",
        "Math",
        "LeftRight",
        "Math",
        "IUniswapV3Pool",
        "LeftRight",
        "PanopticMath"
    ],
    "Internal Calls": [
        "_createLegInAMM",
        "revert PositionTooLarge()"
    ],
    "Library Calls": [
        "getAmount1ForLiquidity",
        "countLegs",
        "add",
        "getAmount0ForLiquidity",
        "getLiquidityChunk",
        "add",
        "add"
    ],
    "Low-Level Calls": [],
    "Code": "function _createPositionInAMM(\n        IUniswapV3Pool univ3pool,\n        uint256 tokenId,\n        uint128 positionSize,\n        bool isBurn\n    ) internal returns (int256 totalMoved, int256 totalCollected, int256 itmAmounts) {\n        // upper bound on amount of tokens contained across all legs of the position at any given tick\n        uint256 amount0;\n        uint256 amount1;\n\n        uint256 numLegs = tokenId.countLegs();\n        // loop through up to the 4 potential legs in the tokenId\n        for (uint256 leg = 0; leg < numLegs; ) {\n            int256 _moved;\n            int256 _itmAmounts;\n            int256 _totalCollected;\n\n            {\n                // cache the univ3pool, tokenId, isBurn, and _positionSize variables to get rid of stack too deep error\n                IUniswapV3Pool _univ3pool = univ3pool;\n                uint256 _tokenId = tokenId;\n                bool _isBurn = isBurn;\n                uint128 _positionSize = positionSize;\n                uint256 _leg;\n\n                unchecked {\n                    // Reverse the order of the legs if this call is burning a position (LIFO)\n                    // We loop in reverse order if burning a position so that any dependent long liquidity is returned to the pool first,\n                    // allowing the corresponding short liquidity to be removed\n                    _leg = _isBurn ? numLegs - leg - 1 : leg;\n                }\n\n                // for this _leg index: extract the liquidity chunk: a 256bit word containing the liquidity amount and upper/lower tick\n                // @dev see `contracts/types/LiquidityChunk.sol`\n                uint256 liquidityChunk = PanopticMath.getLiquidityChunk(\n                    _tokenId,\n                    _leg,\n                    _positionSize,\n                    _univ3pool.tickSpacing()\n                );\n\n                (_moved, _itmAmounts, _totalCollected) = _createLegInAMM(\n                    _univ3pool,\n                    _tokenId,\n                    _leg,\n                    liquidityChunk,\n                    _isBurn\n                );\n\n                unchecked {\n                    // increment accumulators of the upper bound on tokens contained across all legs of the position at any given tick\n                    amount0 += Math.getAmount0ForLiquidity(liquidityChunk);\n\n                    amount1 += Math.getAmount1ForLiquidity(liquidityChunk);\n                }\n            }\n\n            totalMoved = totalMoved.add(_moved);\n            itmAmounts = itmAmounts.add(_itmAmounts);\n            totalCollected = totalCollected.add(_totalCollected);\n\n            unchecked {\n                ++leg;\n            }\n        }\n\n        // Ensure upper bound on amount of tokens contained across all legs of the position on any given tick does not exceed a maximum of (2**127-1).\n        // This is the maximum value of the `int128` type we frequently use to hold token amounts, so a given position's size should be guaranteed to\n        // fit within that limit at all times.\n        if (amount0 > uint128(type(int128).max) || amount1 > uint128(type(int128).max))\n            revert Errors.PositionTooLarge();\n    }"
}