{
    "Function": "_reducePosition",
    "File": "contracts/AMM.sol",
    "Parent Contracts": [
        "contracts/legos/Governable.sol",
        "node_modules/@openzeppelin/contracts/proxy/utils/Initializable.sol",
        "contracts/legos/Governable.sol",
        "contracts/Interfaces.sol"
    ],
    "High-Level Calls": [
        "SafeCast",
        "SafeCast"
    ],
    "Internal Calls": [
        "_short",
        "getTakerNotionalPositionAndUnrealizedPnl",
        "_long",
        "getOpenNotionalWhileReducingPosition",
        "getCloseQuote"
    ],
    "Library Calls": [
        "toUint256",
        "toUint256"
    ],
    "Low-Level Calls": [],
    "Code": "function _reducePosition(address trader, int256 baseAssetQuantity, uint quoteAssetLimit)\n        internal\n        returns (int realizedPnl, uint256 quoteAsset)\n    {\n        (, int256 unrealizedPnl) = getTakerNotionalPositionAndUnrealizedPnl(trader);\n\n        Position storage position = positions[trader]; // storage because there are updates at the end\n        bool isLongPosition = position.size > 0 ? true : false;\n\n        if (isLongPosition) {\n            longOpenInterestNotional -= (-baseAssetQuantity).toUint256();\n            quoteAsset = _short(baseAssetQuantity, quoteAssetLimit);\n        } else {\n            shortOpenInterestNotional -= baseAssetQuantity.toUint256();\n            quoteAsset = _long(baseAssetQuantity, quoteAssetLimit);\n        }\n        uint256 notionalPosition = getCloseQuote(position.size + baseAssetQuantity);\n        (position.openNotional, realizedPnl) = getOpenNotionalWhileReducingPosition(position.size, notionalPosition, unrealizedPnl, baseAssetQuantity);\n        position.size += baseAssetQuantity;\n    }"
}