MEDIUM severity: The vulnerability in the protocol's accounting mechanism, specifically in the handling of Profit and. The vulnerability in the protocol's accounting mechanism, specifically in the handling of Profit and Loss (PnL) during liquidation, can lead to incorrect calculations and potential losses for the LPs. This issue arises when the protocol fails to accurately account for the PnL and funding fees accrue...

Vulnerable code:
```\nsettleFundingFees = Short/LP need to pay Long 100\n\nmarginDepositedTotal = marginDepositedTotal + funding_fee\nmarginDepositedTotal = y + (-100) = y - 100\n\nstableCollateralTotal = x + (-(-100)) = x + 100\n```\n```\nif uint256(settledMargin) > expectedLiquidationFee:\n    if +20 > +25:\n        False\n    liquidatorFee = settledMargin\n    liquidatorFee = +20\n```\n```\nvault.updateStableCollateralTotal(remainingMargin - positionSummary.profitLoss)\nvault.updateStableCollateralTotal(0 - (100))\nvault.updateStableCollateralTotal(-100)\n\nstableCollateralTotal = x + 100 - 100\n```\n```\nFile: LiquidationModule.sol\n\nfunction liquidate(uint256 tokenId) public nonReentrant whenNotPaused liquidationInvariantChecks(vault, tokenId) {\n    // Check that the total margin deposited by the long traders is not -ve.\n    // To get this amount, we will have to account for the PnL and funding fees accrued.\n    int256 settledMargin = positionSummary.marginAfterSettlement;\n\n    uint256 liquidatorFee;\n\n    if (settledMargin > 0) {\n        // Calculate the liquidation fees to be sent to the caller.\n        uint256 expectedLiquidationFee = PerpMath._liquidationFee(\n            position.additionalSize,\n            liquidationFeeRatio,\n            liquidationFeeLowerBound,\n            liquidationFeeUpperBound,\n            currentPrice\n        );\n\n        uint256 remainingMargin;\n\n        // Calculate the remaining margin after accounting for liquidation fees.\n        // If the settled margin is less than the liquidation fee, then the liquidator fee is the settled margin.\n        if (uint256(settledMargin) > expectedLiquidationFee) {\n            liquidatorFee = expectedLiquidationFee;\n            remainingMargin = uint256(settledMargin) - expectedLiquidationFee;\n        } else {\n            liquidatorFee = uint256(settledMargin);\n        }\n\n        // Adjust the stable collateral total to account for user's remaining margin.\n        // If the remaining margin is greater than 0, this goes to the LPs.\n        // Note that `remainingMargin - profitLoss` is the same as `marginDeposited + accruedFunding`.\n        vault.updateStableCollateralTotal(int256(remainingMargin) - positionSummary.profitLoss);\n\n        // Send the liquidator fee to the caller of the function.\n        // If the liquidation fee is greater than the remaining margin, then send the remaining margin.\n        vault.sendCollateral(msg.sender, liquidatorFee);\n    } else {\n        // If the settled margin is -ve then the LPs have to bear the cost.\n        // Adjust the stable collateral total to account for user's profit/loss and the negative margin.\n        // Note: We are adding `settledMargin` and `profitLoss` instead of subtracting because of their sign (which will be -ve).\n        vault.updateStableCollateralTotal(settledMargin - positionSummary.profitLoss);\n    }\n}\n```\n```\nvault.updateGlobalPositionData({marginDelta: -(position.marginDeposited + positionSummary.accruedFunding)})\nvault.updateGlobalPositionData({marginDelta: -(20 + positionSummary.accruedFunding)})\nvault.updateGlobalPositionData({marginDelta: 80})\n\nprofitLossTotal = 100\nnewMarginDepositedTotal = globalPositions.marginDepositedTotal + marginDelta + profitLossTotal\nnewMarginDepositedTotal = globalPositions.marginDepositedTotal + 80 + 100\n\nstableCollateralTotal = stableCollateralTotal + profitLossTotal\nstableCollateralTotal = x + 100\n```\n```\nvault.updateStableCollateralTotal(settledMargin - positionSummary.profitLoss);\nvault.updateStableCollateralTotal(-3 * rETH - (-5 * rETH));\nvault.updateStableCollateralTotal(+2);\n```\n```\nFile: LiquidationModule.sol\n\nfunction liquidate(uint256 tokenId) public nonReentrant whenNotPaused liquidationInvariantChecks(vault, tokenId) {\n    //...SNIP...\n    vault.updateGlobalPositionData({\n        price: position.lastPrice,\n        marginDelta: -(int256(position.marginDeposited) + positionSummary.accruedFunding),\n        additionalSizeDelta: -int256(position.additionalSize) // Since position is being closed, additionalSizeDelta should be negative.\n    });\n```\n```\nFile: FlatcoinVault.sol\n\nfunction updateGlobalPositionData(\n    uint256 _price,\n    int256 _marginDelta,\n    int256 _additionalSizeDelta\n) external onlyAuthorizedModule {\n    int256 profitLossTotal = PerpMath._profitLossTotal({\n        globalPosition: _globalPositions,\n        price: _price\n    });\n\n    int256 newMarginDepositedTotal = int256(_globalPositions.marginDepositedTotal) + _marginDelta + profitLossTotal;\n\n    if (newMarginDepositedTotal < 0) {\n        revert FlatcoinErrors.InsufficientGlobalMargin();\n    }\n\n    _globalPositions = FlatcoinStructs.GlobalPositions({\n        marginDepositedTotal: uint256(newMarginDepositedTotal),\n        sizeOpenedTotal: (int256(_globalPositions.sizeOpenedTotal) + _additionalSizeDelta).toUint256(),\n        lastPrice: _price\n    });\n\n    _updateStableCollateralTotal(-profitLossTotal);\n}\n```\n```\nsettleFundingFees = Long need to pay short 5\n\nmarginDepositedTotal = marginDepositedTotal + fundingFees\nmarginDepositedTotal = 70 + (-5) = 65\n\nstableCollateralTotal = 100 + (-(-5)) = 105\n```\n```\nsettled_margin > 0 => True\n(settled_margin > expected_liquidation_fee) => (60 > 50) => True\nremaining_margin = uint256(settled_margin) - expected_liquidation_fee = 10\n```\n```\nvault.updateStableCollateralTotal(remainingMargin - positionSummary.profitLoss) \n    => \n    stableCollateralTotal = 105 ETH + (remainingMargin - positionSummary.profitLoss)\n    stableCollateralTotal = 105 ETH + (10 ETH - (-5 ETH))\n    stableCollateralTotal = 105 ETH + (15 ETH) = 120 ETH\n```\n```\nvault.updateGlobalPositionData({marginDelta: -(position.marginDeposited + positionSummary.accruedFunding)})\n\nvault.updateGlobalPositionData({marginDelta: -(70 + positionSummary.accruedFunding)})\n\nvault.updateGlobalPositionData({marginDelta: -65})\n\nprofitLossTotal = -5\nnewMarginDepositedTotal = globalPositions.marginDepositedTotal + marginDelta + profitLossTotal\nnewMarginDepositedTotal = 70 + (-65) + (-5)\nnewMarginDepositedTotal = 0\n\nstableCollateralTotal = stableCollateralTotal + profitLossTotal\nstableCollateralTotal = 120 + (-5)\nstableCollateralTotal = 125\n```