In the function addToPosition from the Trading contract the amount of open fees are handled using the _handleOpenFees function but when calling the _handleDeposit function the wrong margin is passed, in fact the _handleDeposit function gets _addMargin - _fee instead of _addMargin
So this mean that the open fees are calculated and handled but when depositing there value will not be transferred from the trader and will not be deposited in the stableVault.
function addToPosition(
uint _id,
uint _addMargin,
PriceData calldata _priceData,
bytes calldata _signature,
address _stableVault,
address _marginAsset,
ERC20PermitData calldata _permitData,
address _trader
)
external
{
...
/* @audit
fee are calculated and handled with _handleOpenFees
*/
uint _fee = _handleOpenFees(_trade.asset, _addMargin*_trade.leverage/1e18, _trader, _trade.tigAsset, false);
/* @audit
But only (_addMargin - _fee) amount is deposited
*/
_handleDeposit(
_trade.tigAsset,
_marginAsset,
_addMargin - _fee,
_stableVault,
_permitData,
_trader
);
...
}
As you can see from the code above the _handleDeposit function receive _addMargin - _fee as new margin, this value is used to calculate the transferred amount from the trader :
Lines of code
https://github.com/code-423n4/2022-12-tigris/blob/main/contracts/Trading.sol#L255-L305
Vulnerability details
Impact
In the function
addToPosition
from theTrading
contract the amount of open fees are handled using the_handleOpenFees
function but when calling the_handleDeposit
function the wrong margin is passed, in fact the_handleDeposit
function gets_addMargin - _fee
instead of_addMargin
So this mean that the open fees are calculated and handled but when depositing there value will not be transferred from the trader and will not be deposited in the stableVault.
Proof of Concept
The issue occurs in the
addToPosition
function :File: contracts/Trading.sol Line 255-305
As you can see from the code above the
_handleDeposit
function receive_addMargin - _fee
as new margin, this value is used to calculate the transferred amount from the trader :File: contracts/Trading.sol Line 565-576
So because of this error the open fees amount will not be transferred from the trader and will not be deposited in the StableVault.
Tools Used
Manual review
Recommended Mitigation Steps
To avoid this issue correct the margin passed to the function
_handleDeposit
, theaddToPosition
function should be modified as follow :