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

Incorrect calculation of funding velocity for both positive and negative market skews.

Summary

Incorrect calculation of funding velocity for both positive and negative market skews.

Vulnerability Details

/// @notice Returns the current funding velocity of the given market.
/// @param self The PerpMarket storage pointer.
function getCurrentFundingVelocity(Data storage self) internal view returns (SD59x18) {
SD59x18 maxFundingVelocity = sd59x18(uint256(self.configuration.maxFundingVelocity).toInt256());
SD59x18 skewScale = sd59x18(uint256(self.configuration.skewScale).toInt256());
SD59x18 skew = sd59x18(self.skew);
if (skewScale.isZero()) {
return SD59x18_ZERO;
}
SD59x18 proportionalSkew = skew.div(skewScale);
SD59x18 proportionalSkewBounded = Math.min(Math.max(unary(SD_UNIT), proportionalSkew), SD_UNIT);
return proportionalSkewBounded.mul(maxFundingVelocity);
}
SD59x18 proportionalSkewBounded = Math.min(Math.max(unary(SD_UNIT), proportionalSkew), SD_UNIT);

The unary function typically returns the negative of its input. So unary(SD_UNIT) is equivalent to -1 in the context of SD59x18 numbers.

  • The intention seems to be to bound the proportionalSkew between -1 and 1, but the current implementation bounds it between -1 and 1 for positive values, and between 1 and 1 for negative values.

  • This means that for any negative proportionalSkew, the function will always return SD_UNIT (which is 1 in SD59x18 format), leading to incorrect funding velocity calculations for markets with negative skew.

  • The lower bound should be negative one (-SD_UNIT) instead of unary(SD_UNIT)

Impact

Incorrect funding velocity calculations for markets with negative skew.

Tools Used

Manual Review

Recommendations

SD59x18 proportionalSkewBounded = Math.min(Math.max(SD_UNIT.neg(), proportionalSkew), SD_UNIT);

OR


SD59x18 proportionalSkewBounded = Math.min(Math.max(unary(SD_UNIT), proportionalSkew), SD_UNIT);

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

Support

FAQs

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