Closed sherlock-admin2 closed 8 months ago
1 comment(s) were left on this issue during the judging contest.
takarez commented:
invalid
Escalate
This finding is invalid, it's not a dup of the primary issue. This finding says lastPrice
should be denominator, but actually using currentPrice
as denominator is correct.
Let's use the case in this finding for illustration
lastPrice = 100, additionalSize = 1000, and new price = 120
=> profitInUsd = 1000 rETH * ($120 - $100) = $20,000
// the current implementation, match the intended profit as $20,000
profitInRETH = profitInUsd / currentPrice = $20,000 / $120 = 166.6 rETH
profitInUsd = profitInRETH * currentRETHPrice = 166.6 rETH * $120 = $20,000
// the recommended solution by this finding, not match
profitInRETH = profitInUsd / lastPrice = $20,000 / $100 = 200 rETH
profitInUsd = profitInRETH * currentRETHPrice = 200 rETH * $120 = $24,000
We can see the current implementation, using currentPrice
as denominator, is correct. And the recommended solution is wrong.
All in all, the root cause of the primary issue, as described in another dup(https://github.com/sherlock-audit/2023-12-flatmoney-judging/issues/161), is settling PnL before close or liquidation of position, rather than using currentPrice
as denominator.
Escalate
This finding is invalid, it's not a dup of the primary issue. This finding says
lastPrice
should be denominator, but actually usingcurrentPrice
as denominator is correct.Let's use the case in this finding for illustration
lastPrice = 100, additionalSize = 1000, and new price = 120 => profitInUsd = 1000 rETH * ($120 - $100) = $20,000 // the current implementation, match the intended profit as $20,000 profitInRETH = profitInUsd / currentPrice = $20,000 / $120 = 166.6 rETH profitInUsd = profitInRETH * currentRETHPrice = 166.6 rETH * $120 = $20,000 // the recommended solution by this finding, not match profitInRETH = profitInUsd / lastPrice = $20,000 / $100 = 200 rETH profitInUsd = profitInRETH * currentRETHPrice = 200 rETH * $120 = $24,000
We can see the current implementation, using
currentPrice
as denominator, is correct. And the recommended solution is wrong.All in all, the root cause of the primary issue, as described in another dup(https://github.com/sherlock-audit/2023-12-flatmoney-judging/issues/161), is settling PnL before close or liquidation of position, rather than using
currentPrice
as denominator.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
Agree with @ydspa, this issue should be invalid.
Agree with the escalation, the report fails to describe the core issue.
Planning to accept the escalation and remove duplication status
Result: Invalid Unique
chaduke
high
PerpMath._profitLoss() caluclates the profit/loss wrongly.
Summary
PerpMath._profitLoss() caluclates the profit/loss wrongly. The main problem is that is uses
price
instead oflastPrice
as the denominator. As a result, profit is calculated larger than expected and loss is calculated smaller than exepcted.Vulnerability Detail
price
instead oflastPrice
as the denominator. As a result, profit is calculated larger than expected and loss is calculated smaller than exepcted.https://github.com/sherlock-audit/2023-12-flatmoney/blob/bba4f077a64f43fbd565f8983388d0e985cb85db/flatcoin-v1/src/libraries/PerpMath.sol#L175-L184
The following POC shows my finding:
Impact
PerpMath._profitLoss() caluclates the profit/loss wrongly. The main problem is that is uses
price
instead oflastPrice
as the denominator. As a result, profit is calculated larger than expected and loss is calculated smaller than exepcted.Code Snippet
Tool used
https://github.com/sherlock-audit/2023-12-flatmoney/blob/bba4f077a64f43fbd565f8983388d0e985cb85db/flatcoin-v1/src/libraries/PerpMath.sol#L175-L184
Manual Review
Recommendation