{
    "Function": "_collectAndWritePositionData",
    "File": "contracts/SemiFungiblePositionManager.sol",
    "Parent Contracts": [
        "contracts/multicall/Multicall.sol",
        "contracts/tokens/ERC1155Minimal.sol"
    ],
    "High-Level Calls": [
        "LeftRight",
        "LeftRight",
        "IUniswapV3Pool",
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "LiquidityChunk",
        "LeftRight",
        "LiquidityChunk",
        "LeftRight",
        "LeftRight",
        "LeftRight",
        "LeftRight"
    ],
    "Internal Calls": [
        "_getFeesBase",
        "_updateStoredPremia"
    ],
    "Library Calls": [
        "leftSlot",
        "toRightSlot",
        "leftSlot",
        "toLeftSlot",
        "rightSlot",
        "tickLower",
        "rightSlot",
        "leftSlot",
        "tickUpper",
        "rightSlot",
        "sub",
        "sub",
        "rightSlot"
    ],
    "Low-Level Calls": [],
    "Code": "function _collectAndWritePositionData(\n        uint256 liquidityChunk,\n        IUniswapV3Pool univ3pool,\n        uint256 currentLiquidity,\n        bytes32 positionKey,\n        int256 movedInLeg,\n        uint256 isLong\n    ) internal returns (int256 collectedOut) {\n        uint128 startingLiquidity = currentLiquidity.rightSlot();\n        int256 amountToCollect = _getFeesBase(univ3pool, startingLiquidity, liquidityChunk).sub(\n            s_accountFeesBase[positionKey]\n        );\n\n        if (isLong == 1) {\n            amountToCollect = amountToCollect.sub(movedInLeg);\n        }\n\n        if (amountToCollect != 0) {\n            // first collect amounts from Uniswap corresponding to this position\n            // Collect only if there was existing startingLiquidity=liquidities.rightSlot() at that position: collect all fees\n\n            // Collects tokens owed to a liquidity chunk\n            (uint128 receivedAmount0, uint128 receivedAmount1) = univ3pool.collect(\n                msg.sender,\n                liquidityChunk.tickLower(),\n                liquidityChunk.tickUpper(),\n                uint128(amountToCollect.rightSlot()),\n                uint128(amountToCollect.leftSlot())\n            );\n\n            // moved will be negative if the leg was long (funds left the caller, don't count it in collected fees)\n            uint128 collected0;\n            uint128 collected1;\n            unchecked {\n                collected0 = movedInLeg.rightSlot() < 0\n                    ? receivedAmount0 - uint128(-movedInLeg.rightSlot())\n                    : receivedAmount0;\n                collected1 = movedInLeg.leftSlot() < 0\n                    ? receivedAmount1 - uint128(-movedInLeg.leftSlot())\n                    : receivedAmount1;\n            }\n\n            // CollectedOut is the amount of fees accumulated+collected (received - burnt)\n            // That's because receivedAmount contains the burnt tokens and whatever amount of fees collected\n            collectedOut = int256(0).toRightSlot(collected0).toLeftSlot(collected1);\n\n            _updateStoredPremia(positionKey, currentLiquidity, collectedOut);\n        }\n    }"
}