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

When filling offChainOrders, all except the first order will receive a 0 funding rate

Summary

When filling offChainOrders, all except the first order will receive a 0 funding rate. This is because getCurrentFundingRate returns 0 when getProportionalElapsedSinceLastFunding returns 0.

Vulnerability Details

fillOffchainOrders fills orders for multiple offchainOrders in the same Tx

The first order updating the funding rate will update lastFundingTime = block.timestamp

For subsequent orders, the following code snippet from _fillOrder is how the funding rate is calculated

// get funding rates for this perp market
ctx.fundingRateX18 = perpMarket.getCurrentFundingRate();

Looking into perpMarket.getCurrentFundingRate()

function getCurrentFundingRate(Data storage self) internal view returns (SD59x18) {
return sd59x18(self.lastFundingRate).add(
getCurrentFundingVelocity(self).mul(getProportionalElapsedSinceLastFunding(self).intoSD59x18())
);
}

Looking into getProportionalElapsedSinceLastFunding()

function getProportionalElapsedSinceLastFunding(Data storage self) internal view returns (UD60x18) {
return ud60x18Convert(block.timestamp - self.lastFundingTime).div(
ud60x18Convert(Constants.PROPORTIONAL_FUNDING_PERIOD)
);
}

Since this is the subsequent order in the same tx, self.lastFundingTime == block.timestamp, therefore getProportionalElapsedSinceLastFunding will return 0, therefore getCurrentFundingRate will also return 0

This will cause the funding rate for all subsequent orders in that Tx = 0

Impact

When filling offChainOrders, all except the first order will receive a 0 funding rate.

All other calculations using funding rate will be wrong as a result, including pnl calculations

Tools Used

Manual Review

Recommendations

This requires a complete redesign of the funding fee system to make it fair

Updates

Lead Judging Commences

inallhonesty Lead Judge
about 1 year ago
inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement
Assigned finding tags:

When filling offChainOrders, all except the first order will receive a 0 funding rate

Support

FAQs

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