-
Notifications
You must be signed in to change notification settings - Fork 7
ZeroTrust - In Cross Margin mode, the calculation for users borrowing from the pool is incorrect #272
Comments
request poc |
PoC requested from @ZeroTrust01 Requests remaining: 11 |
Very similar to issue #273. |
POC: In isolated mode:
So the borrow fee is like: 3000USD*BORROW_FEE_RATE However, in cross margin mode:
In this case, the user doesn’t use the funds of the trading pair directly; instead, they use BTC and position value as collateral to borrow from the pool. Similarly, an example of 1x leverage is as follows:
So the borrow fee is like: 0USD*BORROW_FEE_RATE However, in cross margin mode:
In this case, the user doesn’t use the funds of the trading pair directly; instead, they use BTC and position value as collateral to borrow from the pool. |
@ZeroTrust01 Sponsor comments, could be invalid
|
This issue concerns the source of funds used for trading. Regardless of the currency(token) in which the trade is ultimately settled, it involves understanding how much funds the user has borrowed and how much fees they need to pay. |
@ZeroTrust01 is right |
In this case, because using 1 leverage, whether it is cross mode or isolated mode, there is no borrowing from the LP. so finally gets 1WETH (Both cross and isolate) |
@0xELFi02 Not quite sure if I am getting your point. You are agreeing this is a valid issue? |
@nevillehuang No, It should be invalid |
In a market for a trading pair (such as WETH-USDC), only the two tokens of the trading pair are involved in trade. In isolated mode, a user can go long with one WETH or go short with 1,000 USDC. In cross margin mode, however, even if a user has one WBTC(Using 1000 USDT as an example would be more appropriate), I doubt they can directly use a portion of its value (1,000 USD) to participate in the market directly If it’s not using WBTC as collateral to borrow WETH or USDC. For a trading pair market, only the two tokens of the pair can directly participate in trading. This is the focus of our discussion. For example, in a USD-Tesla stock trading pair, even if my account has a large amount of euros, can I directly use my euros to buy Tesla stock? If you believe that in a WETH-USDC trading pair market, more than just WETH and USDC can directly participate in the trading, then I have nothing further to add. |
Escalate A very simple scenario:
I think 1000 USDT can not directly participate in trading in a market for a trading pair (such as WETH-USDC). |
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. |
@0502lian "in cross margin mode the collateral is held in the PortfolioVault, which is just like a collateralized lending market. Let's focus on the controversial points, let's discuss the "A very simple scenario" above you mentioned: |
So use long position(1 leverage) as a example , do you borrow `eth' or not ??? |
@0xELFi02 could you clarify this part:
|
With 1 leverage, the user does not need to borrow funds. For isolate margin, the principal comes from the user's own wallet. For cross margin mode, the principal is borrowed through collateral, meaning the margin principal is also effectively borrowed |
Let’s focus our discussion on users in the cross margin mode scenario. Just as you said "the principal is borrowed through collateral". |
Please correct me if I am wrong: Both of the trades isolated/cross the initial margin is same. However, when you close the position in isolated mode you get the entire settle margin which is profit + initial margin which is 1.5WETH. When closing the cross position you only get the profit (naturally) which is 0.5WETH. Then you can withdraw your cross funds if you like and you can end up as same as the isolated one. I don't see the problem here |
We talked about 1x leverage here. |
If we are talking about 1x Leverage then I don't see any difference between this issue and the other one. |
I'm not sure what the issue here is exactly. The example from the report looks correct and how it should be, no?
In the isolated case, the user receives 1 WETH (2000 USD), because when they opened a position 1 WETH (1000 USD) was transferred from the user into the contract, so in the end, they receive their initial USD value (1000 USD == 0.5 WETH) and profit in USD (1000 USD == 0.5 WETH), though WETH value didn't change. In the cross case, the user didn't provide 1 WETH, but opened a position of 1 WETH (1000 USD), with collateral in another token (say USDC). When closing the position, they got their profit value in USD (1000 USD == 0.5 WETH), but didn't receive initial value cause they didn't provide any. The impact of the report is that the borrowing fee wasn't taken, but based on the comment here there is no borrowing with 1x leverage, hence, no borrowing fee. So the impact is incorrect and report seems to be just explaining how the protocol works. Planning to reject the escalation and leave the issue as it is. |
Agree. |
Result: |
Escalations have been resolved successfully! Escalation status:
|
ZeroTrust
High
In Cross Margin mode, the calculation for users borrowing from the pool is incorrect
Summary
In Cross Margin mode, the calculation for users borrowing from the pool is incorrect.
Vulnerability Detail
We know that isolated and cross margin are different. When a position is created, in isolated mode, the corresponding assets need to be transferred from the user’s wallet to the MarketVault, while in cross margin mode, the user only needs to have sufficient collateral in the PortfolioVault (any supported collateral will do).
For example, with 1x leverage going long on WETH-USDC, the position size is 1 WETH, and the price of WETH is 1000 USD.
When the price of WETH rises to 2000 USD, closing the position makes it more evident.
In isolated mode: The user profits 1000 USD (2000 USD - 1000 USD initial capital), and finally still gets their original 1 WETH (2000 USD), which is used for trading.
In cross margin mode: The user profits 1000 USD (2000 USD - 1000 USD initial borrowed funds), and finally gets 0.5 WETH.
The above 10,000 USDC collateral could entirely be 10 BTC (or other non-market trading pair tokens). It can be seen that the code in increaseHoldAmount does not distinguish between isolated mode and cross margin mode, which is therefore incorrect.
Impact
This results in financial loss for the protocol borrow fee.
Code Snippet
https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/main/elfi-perp-contracts/contracts/process/IncreasePositionProcess.sol#L34
Tool used
Manual Review
Recommendation
Distinguish between the handling methods for isolated mode and cross margin mode.
The text was updated successfully, but these errors were encountered: