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

The calculation of the open interest in `getOpenInterest` is wrong

Lines of code

https://github.com/Cyfrin/2024-07-zaros/blob/d687fe96bb7ace8652778797052a38763fbcbb1b/src/perpetuals/branches/PerpMarketBranch.sol#L66

Impact

Because in the function getOpenINtrest thehalfSkew is calculated in a wrong way, the returned values for longsOpenInterest and shortsOpenInterest are wrong. This will break any external service dependant on those values.

Proof of Concept

In the function getOpenInterest, first the skew is taken to determine the halfSkew which is later used to calculate the longsOpenInterest and shortsOpenInterest:

// calculate half skew
SD59x18 halfSkew = sd59x18(perpMarket.skew).div(sd59x18(2e18));

The issue arises from the fact that the skew is saved with 18 decimals and by dividing it by 2e18 the result is not half of the skew.

E.g. if the scew is 2e18 half of the skew should be 1e18 but the result is:
halfScew = 2e18 / 2e18 = 1.

This leads to the wrong calculation of the longsOpenInterest and shortsOpenInterest further down:

// prepare outputs
longsOpenInterest = halfOpenInterest.add(halfSkew).lt(SD59x18_ZERO)
? UD60x18_ZERO
: halfOpenInterest.add(halfSkew).intoUD60x18();
shortsOpenInterest = unary(halfOpenInterest).add(halfSkew).abs().intoUD60x18();

Recommended Mitigation Steps

To ensure the values for the longsOpenInterest and shortsOpenInterest are right, change the code like this:

- SD59x18 halfSkew = sd59x18(perpMarket.skew).div(sd59x18(2e18));
+ SD59x18 halfSkew = sd59x18(perpMarket.skew).div(sd59x18(2));
Updates

Lead Judging Commences

inallhonesty Lead Judge
over 1 year ago
inallhonesty Lead Judge over 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.

Give us feedback!