Closed sherlock-admin2 closed 4 months ago
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid: seem like same report as issue 257; medium(8)
I believe the rounding errors from the example provided are too small to justify this issue and its duplicates as medium severity
deepplus
medium
_profitLoss
function of thePerpMath
calculate thePnL
incorrectly.Summary
The calculation of
PnL
in_profitLoss
function ofPerpMath
is wrong.Vulnerability Detail
The
_profitLoss
function calculates and returnspnl
based on passed params ofposition
andprice
.First, following params may be passed to this function.
position.additionalSize
is 22price
is 30priceShift
is 3 In this case, theprofitLossTimesTen
is22 * 3 * 10 / 30= 22
Next, think of the following params.
position.additionalSize
is 20price
is 30priceShift
is 3 In this case, theprofitLossTimesTen
is20 * 3 * 10 / 30 = 20
In first condition thePnL
is 1 and last conditin,PnL
is 2. This is not fair.Impact
Above wrong calculation may leads to loss of user's fund.
Code Snippet
https://github.com/sherlock-audit/2023-12-flatmoney/blob/bba4f077a64f43fbd565f8983388d0e985cb85db/flatcoin-v1/src/libraries/PerpMath.sol#L175-L184
Tool used
Manual Review
Recommendation
The
_profitLoss
function should be updated as follow.