When a user's order gets filled their profits/losses from the funding fee get calculated in order to verify the position. The problem arises in the fact that when the skew is positive and was also positive for the last trade the user did their accruedFunding will be doubled than what it should be meaning they will loose or receive double the fees.
First the funding fee per unit is calculated and updated for the market:
The sign of the funding rate per unit will generally be opposite of the value of the skew if the skew hasn't switched signs (e.g positive skew => negative fundingFeePerUnit).
Then the unrealized pnl is calculated by calling TradingAccount::getAccountMarginRequirementUsdAndUnrealizedPnlUsd
. Where the accrued funding (profit / losses from funding fee) is calculated.
Finally if we take a look at the function to calculated the accrued funding we see that we subtract the old funding fee from the new one. The logic is generally correct if both values are positive but in the situations where self.lastInteractionFundingFeePerUnit < 0
that would actually lead to increasing the funding fee.
Under normal situation: self.lastInteractionFundingFeePerUnit = -0. 1; fundingFeePerUnit = -0.05; = > netFundingFeePerUnit = -0.05 ...
But in the current code: self.lastInteractionFundingFeePerUnit = -0. 1; fundingFeePerUnit = -0.05; = > netFundingFeePerUnit = -0.15 ...
Calculation of the accrued funding is wrong when the skew is > 0 which leads to loss of funds for the contract and users.
Manual Review
Check if fundingFeePerUnit
and self.lastInteractionFundingFeePerUnit
are both less than 0 and adjust the formula for that case by changing sub
to add
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.