Skip to content
Business logic Audit finding Intermediate

Ideal balance is not calculated correctly when providing imbalanced liquidity

The custom swap pool calculates the ideal balance incorrectly when liquidity is provided in an imbalanced ratio, using the pool's stored balances instead of the new balances. An attacker could deposit tokens in a skewed ratio to receive more LP tokens than deserved, potentially draining value from the pool.

jonah1005 Follow

Originally disclosed on Code4rena

Submitted by jonah1005

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.

    swap = deploy_contract('Swap'
        [dai.address, link.address], [18, 18], 'lp', 'lp', 1, 85, 10**7, 0, 0, 4* 10**18)
    link.functions.approve(swap.address, deposit_amount).transact()
    dai.functions.approve(swap.address, deposit_amount).transact()
    previous_lp = lptoken.functions.balanceOf(user).call()
    swap.functions.addLiquidity([deposit_amount, deposit_amount // 10], 10, 10**18).transact()
    post_lp = lptoken.functions.balanceOf(user).call()
    print('get lp', post_lp - previous_lp)

For the second pool, one we deposit more DAI.

    swap = deploy_contract('Swap'
        [dai.address, link.address], [18, 18], 'lp', 'lp', 1, 85, 10**7, 0, 0, 4* 10**18)
    link.functions.approve(swap.address, deposit_amount).transact()
    dai.functions.approve(swap.address, deposit_amount).transact()
    previous_lp = lptoken.functions.balanceOf(user).call()
    swap.functions.addLiquidity([deposit_amount, deposit_amount // 10], 10, 10**18).transact()
    post_lp = lptoken.functions.balanceOf(user).call()
    print('get lp', post_lp - previous_lp)

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

for (uint256 i = 0; i < self.pooledTokens.length; i++) {
    uint256 idealBalance = v.d1.mul(self.balances[i]).div(v.d0);
    fees[i] = feePerToken
        .mul(idealBalance.difference(newBalances[i]))
        .div(FEE_DENOMINATOR);
    self.balances[i] = newBalances[i].sub(
        fees[i].mul(self.adminFee).div(FEE_DENOMINATOR)
    );
    newBalances[i] = newBalances[i].sub(fees[i]);
}

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

chickenpie347 (Boot Finance) confirmed

The original report as published on Code4rena, by jonah1005. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the fee calculation uses the pool's stored balances (self.balances) instead of the new balances after deposit, so the ideal balance is computed from outdated values.

Impact

A user providing imbalanced liquidity could receive up to 4 times more LP tokens than deserved, diluting other liquidity providers and potentially causing financial loss.

What the attacker needed

A custom swap pool with a target price different from 1; ability to add liquidity

The fix

The code should be fixed by replacing self.balances with _xp(self, newBalances) in the fee calculation.

How it was found

  1. Deploy a DAI/LINK swap pool with a target price of 4.
  2. Approve the swap contract to spend DAI and LINK.
  3. Record the user's LP token balance.
  4. Call addLiquidity with a deposit ratio of 10:1 (DAI:LINK).
  5. Record the new LP token balance and observe the difference.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A liquidity pool is a shared pot of two tokens that people can add to or take from. When you add tokens, you get LP tokens representing your share. The pool charges a fee if you add tokens in a different ratio than the pool holds. This report says the fee is calculated using the wrong numbers, so someone could add tokens in a skewed ratio and get too many LP tokens, taking value from others.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by jonah1005

View all

39 more disclosed reports, best first.

More from Boot Finance contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.