Submitted by KingNFT
The formula used for calculating _newPrice in addToPosition() function of Trading.sol is not correct, users will lose part of their funds/profit while using this function.
The wrong formula
The correct formula is
Why this works?
Given
Then
proven.
The following test case shows two examples that users lose some funds due to adding a new position whenever their existing position is in profit or loss state.
The test result
Hardhat
Use the correct formula, the following test case is for the same above examples after fix.
The test result
TriHaz (Tigris Trade) confirmed
Alex the Entreprenerd (judge) commented:
GainsGoblin (Tigris Trade) resolved:
