DeFiFoundry
50,000 USDC
View results
Submission Details
Severity: high
Invalid

Dependency of User Profits/Losses on Other Users' Performance

Summary

The PerpetualVault.sol contract calculates user withdrawal amounts based on their share of the total collateral in the vault. This design allows the withdrawal amount of a user to be reduced if the total collateral decreases due to losses incurred by other users, even if the user has personally made a profit. This creates a dependency where a user's profits and losses are influenced by the performance of others, contradicting the protocol's goal of providing a simplified and independent leveraged trading experience.

Vulnerability Details

In the PerpetualVault.sol contract, the amount of collateral tokens a user receives upon withdrawal is calculated based on their share of the total collateral in the vault. This calculation is done in the _handleReturn function as follows:

if (positionClosed) {
amount = collateralToken.balanceOf(address(this)) * shares / totalShares;
}

This design means that even if a user has personally made a profit, their withdrawal amount can be reduced if the total collateral in the vault has decreased due to losses incurred by other users. This creates a dependency where the profits and losses of one user are influenced by the performance of others.

Impact

  1. Direct Financial Loss: Users who have personally made a profit may receive less collateral than they deposited if other users incur losses.

  2. Reduced Trust: This dependency undermines user trust in the protocol, as users cannot independently benefit from their own successful trades.

  3. Protocol Misalignment: This design contradicts the protocol's goal of simplifying leveraged trading for individual users, as it introduces collective risk that users cannot control.

Violation of Protocol's Stated Goals

This vulnerability contradicts the protocol's goal of providing a simplified and independent leveraged trading experience for users. Specifically:

What does the system do?

  • Simplifies the process of leveraged trading for users who may not want to actively manage their positions.

  • Provides a way for users to participate in leveraged trading with reduced active management.

The current implementation introduces a collective risk model, where users' profits and losses are interdependent, which is not aligned with the goal of simplifying independent leveraged trading.

Tool Used

  • Manual Code Review

  • Foundry (for testing and validation)

Proof of Concept

Below is a Foundry test case demonstrating how a user's withdrawal amount can be reduced due to losses incurred by other users:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../src/PerpetualVault.sol";
contract PerpetualVaultTest is Test {
PerpetualVault vault;
address user1 = address(0x1);
address user2 = address(0x2);
address collateralToken = address(0x3);
function setUp() public {
vault = new PerpetualVault();
// Initialize vault with mock collateral token and other parameters
}
function testUserWithdrawalReductionDueToOtherUserLoss() public {
// User1 deposits 1000 USDC
vm.prank(user1);
vault.deposit(1000);
// User2 deposits 1000 USDC
vm.prank(user2);
vault.deposit(1000);
// Simulate a loss in the vault (e.g., due to a bad trade)
// Assume the total collateral is reduced to 1500 USDC
vm.prank(address(vault));
collateralToken.balanceOf.returns(1500);
// User1 attempts to withdraw
vm.prank(user1);
vault.withdraw(user1, 1);
// Verify that User1 receives less than their initial deposit
uint256 user1WithdrawalAmount = collateralToken.balanceOf(user1);
assertLt(user1WithdrawalAmount, 1000, "User1 received less than their initial deposit due to other user's loss");
}
}

Recommendations

  1. Isolate User Collateral: Implement a mechanism where each user's collateral is managed separately. This ensures that a user's profits and losses are independent of other users' performance.

  2. Introduce an Insurance Fund: Create an insurance fund to cover losses, ensuring that profitable users are not penalized for the losses of others.

  3. Revise Profit/Loss Calculation: Modify the withdrawal logic to calculate profits and losses based on individual user performance rather than the collective performance of all users.

By addressing this vulnerability, the protocol can better align with its goal of simplifying leveraged trading for individual users while maintaining fairness and trust.

Updates

Lead Judging Commences

n0kto Lead Judge 9 months ago
Submission Judgement Published
Invalidated
Reason: Design choice

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!