MarginTrading.sol: The whole balance and not just the traded funds are deposited into Aave when a trade is opened
It's expected by the protocol that funds can be in the `MarginTrading` contract without being deposited into Aave as margin.\\nWe can see this by looking at the `MarginTradingFactory.depositMarginTradingETH` and `MarginTradingFactory.depositMarginTradingERC20` functions.\\nIf the user sets `margin=false` as the parameter, the funds are only sent to the `MarginTrading` contract but NOT deposited into Aave.\\nSo clearly there is the expectation for funds to be in the `MarginTrading` contract that should not be deposited into Aave.\\nThis becomes an issue when a trade is opened.\\nLet's look at the `MarginTrading._openTrade` function that is called when a trade is opened:\\nThe whole balance of the token will be deposited into Aave:\\n```\\n_tradeAmounts[i] = IERC20(_tradeAssets[i]).balanceOf(address(this)); \\n_lendingPoolDeposit(_tradeAssets[i], _tradeAmounts[i], 1); \\n```\\n\\nNot just those funds that have been acquired by the swap. This means that funds that should stay in the `MarginTrading` contract might also be deposited as margin.
It is necessary to differentiate the funds that are acquired by the swap and those funds that were there before and should stay in the contract:\\n```\\ndiff // Remove the line below\\n// Remove the line below\\ngit a/dodo// Remove the line below\\nmargin// Remove the line below\\ntrading// Remove the line below\\ncontracts/contracts/marginTrading/MarginTrading.sol b/dodo// Remove the line below\\nmargin// Remove the line below\\ntrading// Remove the line below\\ncontracts/contracts/marginTrading/MarginTrading.sol\\nindex f68c1f3..42f96cf 100644\\n// Remove the line below\\n// Remove the line below\\n// Remove the line below\\n a/dodo// Remove the line below\\nmargin// Remove the line below\\ntrading// Remove the line below\\ncontracts/contracts/marginTrading/MarginTrading.sol\\n// Add the line below\\n// Add the line below\\n// Add the line below\\n b/dodo// Remove the line below\\nmargin// Remove the line below\\ntrading// Remove the line below\\ncontracts/contracts/marginTrading/MarginTrading.sol\\n@@ // Remove the line below\\n261,6 // Add the line below\\n261,10 @@ contract MarginTrading is OwnableUpgradeable, IMarginTrading, IFlashLoanReceiver\\n         bytes memory _swapParams,\\n         address[] memory _tradeAssets\\n     ) internal {\\n// Add the line below\\n        int256[] memory _amountsBefore = new uint256[](_tradeAssets.length);\\n// Add the line below\\n        for (uint256 i = 0; i < _tradeAssets.length; i// Add the line below\\n// Add the line below\\n) {\\n// Add the line below\\n            _amountsBefore[i] = IERC20(_tradeAssets[i]).balanceOf(address(this));\\n// Add the line below\\n        }\\n         if (_swapParams.length > 0) {\\n             // approve to swap route\\n             for (uint256 i = 0; i < _swapApproveToken.length; i// Add the line below\\n// Add the line below\\n) {\\n@@ // Remove the line below\\n272,8 // Add the line below\\n276,10 @@ contract MarginTrading is OwnableUpgradeable, IMarginTrading, IFlashLoanReceiver\\n         }\\n         uint256[] memory _tradeAmounts = new uint256[](_tradeAssets.length);\\n         for (uint256 i = 0; i < _tradeAssets.length; i// Add the line below\\n// Add the line below\\n) {\\n// Remove the line below\\n            _tradeAmounts[i] = IERC20(_tradeAssets[i]).balanceOf(address(this));\\n// Remove the line below\\n            _lendingPoolDeposit(_tradeAssets[i], _tradeAmounts[i], 1);\\n// Add the line below\\n            if (_amountsBefore[i] < IERC20(_tradeAssets[i]).balanceOf(address(this))) {\\n// Add the line below\\n                _tradeAmounts[i] = IERC20(_tradeAssets[i]).balanceOf(address(this)) // Remove the line below\\n _amountsBefore[i];\\n// Add the line below\\n                _lendingPoolDeposit(_tradeAssets[i], _tradeAmounts[i], 1);\\n// Add the line below\\n            }\\n         }\\n         emit OpenPosition(_swapAddress, _swapApproveToken, _tradeAssets, _tradeAmounts);\\n     }\\n```\\n\\nIf funds that were in the contract prior to the swap should be deposited there is the separate `MarginTrading.lendingPoolDeposit` function to achieve this.
When opening a trade funds can be deposited into Aave unintentionally. Thereby the funds act as margin and the trade can incur a larger loss than expected.
```\\n_tradeAmounts[i] = IERC20(_tradeAssets[i]).balanceOf(address(this)); \\n_lendingPoolDeposit(_tradeAssets[i], _tradeAmounts[i], 1); \\n```\\n