DeFiFoundry
60,000 USDC
View results
Submission Details
Severity: high
Invalid

Exponential Growth of Funding Fee Leading to Non-Linear Distribution

Summary

The calculation of NextFundingFeePerUnit involves multiplying block.timestamp - lastUpdatedTimeStamp twice, leading to the exponential growth of fundingFeePerUnit.

Vulnerability Details

If we look at the SettlementBranch:_fillOrder function, and there calculation involved to calculate the fundingFeePerUnit.

https://github.com/Cyfrin/2024-07-zaros/blob/d687fe96bb7ace8652778797052a38763fbcbb1b/src/perpetuals/branches/SettlementBranch.sol#L392-L393

ctx.fundingRateX18 = perpMarket.getCurrentFundingRate();
ctx.fundingFeePerUnitX18 = perpMarket.getNextFundingFeePerUnit(ctx.fundingRateX18, fillPriceX18);

Initially the fundingRate is getting calculated, and using that the nextFundingFeePerUnit is getting calculated. If we look and the function perpMarket:getCurrentFundingRate.

https://github.com/Cyfrin/2024-07-zaros/blob/d687fe96bb7ace8652778797052a38763fbcbb1b/src/perpetuals/leaves/PerpMarket.sol#L126-L130

If we go through the code, it will get complex, so I will explain the mathematical calculations involved in the calculation of fundingRate and nextFundingFeePerUnit.

The mathematical calculations involved in calculating the fundingRate and nextFundingFeePerUnit are:

For fundingRate

proportionalSkew = self.skew/ self.configuration.skewScale
proportionalSkewBounded = min(max(-1e18, proportionalSkew), 1e18);
CurrentFundingVelocity = (self.configuration.maxFundingVelocity) * (proportionalSkewBounded)
ProportionalElapsedSinceLastFunding = (block.timestamp - self.lastFundingTime) /1 day
ctx.fundingRateX18 = (ProportionalElapsedSinceLastFunding * CurrentFundingVelocity) + self.lastFundingRate
  • The ProportionalElapsedSinceLastFunding is calculated by subtracting current and lastupdated Timestamp, and it is divided by 1 day to convert the seconds to days as the current funding velocity is in days.

  • Calculated elapsed timestamp is getting multiplied with CurrentFundingVelocity.

  • Now that fundingRate is being used to calculate fundingFeePerUnit

For fundingFeePerUnit

avgFundingRate = -(self.lastFundingRate + fundingRate)/2;
ProportionalElapsedSinceLastFunding = (block.timestamp - self.lastFundingTime)/PROPORTIONAL_FUNDING_PERIOD(1 days);
PendingFundingFeePerUnit = avgFundingRate * ProportionalElapsedSinceLastFunding * fillPrice;
NextFundingFeePerUnit = self.lastFundingFeePerUnit + PendingFundingFeePerUnit ;
  • Now if we look calculation of PendingFundingFeePerUnit it is getting multiplied by ProportionalElapsedSinceLastFunding and avgFundingRate is already multiplied by ProportionalElapsedSinceLastFunding so double multiplication of ProportionalElapsedSinceLastFunding will grow the funding fee exponentially(Quadratically).

Impact

  • Unbalance of short and long fundingFee as the calculation will be time dependent and non-linear growth will create unbalance.

  • As funding fees grow exponentially, the cost burden on traders can become unsustainable, leading to a higher risk of liquidation, especially for leveraged positions.

  • Increased Costs for Traders

Tools Used

Manual Review

Recommendations

  • Multiply elapsedTime one time.

Updates

Lead Judging Commences

inallhonesty Lead Judge
10 months ago
inallhonesty Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.