code-423n4 / 2021-09-sushitrident-findings

0 stars 0 forks source link

Adding imbalanced liquidity earns extra rewards #30

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

broccoli

Vulnerability details

Adding imbalanced liquidity earns extra rewards

Impact

When a user provides liquidity with unbalanced balance. It should be the same as swapping tokens and adding lp. However, the liquidity the users get is calculated as follow:

 uint256 computed = TridentMath.sqrt((balance0 - fee0) * (balance1 - fee1));
uint256 k = TridentMath.sqrt(uint256(_reserve0) * _reserve1);
liquidity = ((computed - k) * _totalSupply) / k;

The user can take a share of the swapping fee he's paying. It's like first add the liquidity then swap the tokens.

It may be a design choice. However, the pool is designed to hold billions of dollars. Any extra fee would be amplified. I consider this is a medium-risk issue.

ConstantProductPool and HybridPool have the same issue. ConstantProductPool.sol#L83-L120

HybridPool.sol#L115-L139

Proof of Concept

I noticed that imbalanced mint and burnSingle may sometimes have a better price than swapping. It may be a lot of issues involved.

Tools Used

None

Recommended Mitigation Steps

    // Note: what is the optimal value of computed
    uint256 computed = TridentMath.sqrt((balance0) * (balance1));

    // The user should get the lp based on post-swap-fee reserve.
    uint256 k = TridentMath.sqrt(uint256(_reserve0 + fee) * _reserve1 + fee);
    liquidity = ((computed - k) * _totalSupply) / k;
maxsam4 commented 2 years ago

Duplicate of https://github.com/code-423n4/2021-09-sushitrident-findings/issues/34