Long trader's deposited margin can be wiped out
Long Trader's deposited margin can be wiped out due to a logic error, leading to a loss of assets.\\n```\\nFile: FlatcoinVault.sol\\n    function settleFundingFees() public returns (int256 _fundingFees) {\\n..SNIP..\\n        // Calculate the funding fees accrued to the longs.\\n        // This will be used to adjust the global margin and collateral amounts.\\n        _fundingFees = PerpMath._accruedFundingTotalByLongs(_globalPositions, unrecordedFunding);\\n\\n        // In the worst case scenario that the last position which remained open is underwater,\\n        // we set the margin deposited total to 0. We don't want to have a negative margin deposited total.\\n        _globalPositions.marginDepositedTotal = (int256(_globalPositions.marginDepositedTotal) > _fundingFees)\\n            ? uint256(int256(_globalPositions.marginDepositedTotal) + _fundingFees)\\n            : 0;\\n\\n        _updateStableCollateralTotal(-_fundingFees);\\n```\\n\\nIssue 1\\nAssume that there are two long positions in the system and the `_globalPositions.marginDepositedTotal` is $X$.\\nAssume that the funding fees accrued to the long positions at Line 228 is $Y$. $Y$ is a positive value indicating the overall gain/profit that the long traders received from the LPs.\\nIn this case, the `_globalPositions.marginDepositedTotal` should be set to $(X + Y)$ after taking into consideration the funding fee gain/profit accrued by the long positions.\\nHowever, in this scenario, $X < Y$. Thus, the condition at Line 232 will be evaluated as `false,` and the `_globalPositions.marginDepositedTotal` will be set to zero. This effectively wipes out all the margin collateral deposited by the long traders in the system, and the deposited margin of the long traders is lost.\\nIssue 2\\nThe second issue with the current implementation is that it does not accurately capture scenarios where the addition of `_globalPositions.marginDepositedTotal` and `_fundingFees` result in a negative number. This is because `_fundingFees` could be a large negative number that, when added to `_globalPositions.marginDepositedTotal`, results in a negative total, but the condition at Line 232 above still evaluates as true, resulting in an underflow revert.
If the intention is to ensure that `_globalPositions.marginDepositedTotal` will never become negative, consider summing up $(X + Y)$ first and determine if the result is less than zero. If yes, set the `_globalPositions.marginDepositedTotal` to zero.\\nThe following is the pseudocode:\\n```\\nnewMarginTotal = globalPositions.marginDepositedTota + _fundingFees;\\nglobalPositions.marginDepositedTotal = newMarginTotal > 0 ? uint256(newMarginTotal) : 0;\\n```\\n
Loss of assets for the long traders as mentioned above.
```\\nFile: FlatcoinVault.sol\\n    function settleFundingFees() public returns (int256 _fundingFees) {\\n..SNIP..\\n        // Calculate the funding fees accrued to the longs.\\n        // This will be used to adjust the global margin and collateral amounts.\\n        _fundingFees = PerpMath._accruedFundingTotalByLongs(_globalPositions, unrecordedFunding);\\n\\n        // In the worst case scenario that the last position which remained open is underwater,\\n        // we set the margin deposited total to 0. We don't want to have a negative margin deposited total.\\n        _globalPositions.marginDepositedTotal = (int256(_globalPositions.marginDepositedTotal) > _fundingFees)\\n            ? uint256(int256(_globalPositions.marginDepositedTotal) + _fundingFees)\\n            : 0;\\n\\n        _updateStableCollateralTotal(-_fundingFees);\\n```\\n