Ideal balance is not calculated correctly when providing imbalanced liquidity #150
Labels
3 (High Risk)
Assets can be stolen/lost/compromised directly
bug
Something isn't working
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Handle
jonah1005
Vulnerability details
Impact
When a user provides imbalanced liquidity, the fee is calculated according to the ideal balance. In saddle finance, the optimal balance should be the same ratio as in the Pool.
Take, for example, if there's 10000 USD and 10000 DAI in the saddle's USD/DAI pool, the user should get the optimal lp if he provides lp with ratio = 1.
However, if the customSwap pool is created with a target price = 2. The user would get 2 times more lp if he deposits DAI.
SwapUtils.sol#L1227-L1245
The current implementation does not calculates ideal balance correctly.
If the target price is set to be 10, the ideal balance deviates by 10.
The fee deviates a lot. I consider this is a high-risk issues.
Proof of Concept
We can observe the issue if we initiates two pools DAI/LINK pool and set the target price to be 4.
For the first pool, we deposit more dai.
For the second pool, one we deposit more dai.
We can get roughly 4x more lp in the first case
Tools Used
None
Recommended Mitigation Steps
The current implementation uses
self.balances
https://github.com/code-423n4/2021-11-bootfinance/blob/main/customswap/contracts/SwapUtils.sol#L1231-L1236
Replaces
self.balances
with_xp(self, newBalances)
would be a simple fix.I consider the team can take balance's weighted pool as a reference. WeightedMath.sol#L149-L179
The text was updated successfully, but these errors were encountered: