https://github.com/devZygo/zygoAuditHacken
In PerpDex.sol, several sections involving funding rate calculations do not properly account for the fact that the funding rate can be both positive and negative. This oversight leads to incorrect calculations and faulty conditional checks, which in turn results in inaccurate fee assessments and fund transfers. As a consequence, both user balances and protocol funds will be affected, causing financial losses and inconsistencies in accounting.
In Line 720 of PerpDex.sol, the code checks whether the margin is less than the fundingFee. However, since margin is always positive and the fundingFee can be either positive or negative, this check becomes unreliable. When the fundingFee is negative, the condition always evaluates to false, even in cases where it should not. As a result, the function does not return (0, 0) as expected and proceeds to Line 724, leading to incorrect fee calculations.
A similar issue appears on Line 741, where marginAfterFundingFee is calculated as "margin - fundingFee". When fundingFee is negative, this effectively becomes an addition, resulting in inflated values. This miscalculation leads to inaccurate fund and fee transfers, impacting both user balances and protocol accounting.