Skip to content
Business logic Audit finding Advanced

Slippage tolerance vulnerability in StableSwap

The slippage tolerance check for StableSwap deposits compared a simple ratio of total pool balances to pool token supply against the deposit ratio, ignoring the StableSwap invariant and amplification factor. This could let deposits through whose real price impact exceeds the user's stated tolerance, causing unexpected losses.

DOWSERS Follow with Lambda

Originally disclosed on Code4rena

Submitted by DOWSERS, also found by Lambda

The assert_slippage_tolerance function does not properly account for the non-linear pricing curve of StableSwap pools, which are influenced by the amplification factor (A). Specifically, the function calculates slippage tolerance based solely on a simple ratio of total deposits and pool tokens without integrating the StableSwap invariant (D). This oversight can lead to the acceptance of transactions with higher actual slippage than allowed, exposing the protocol to user losses or economic inefficiencies.

Link to code

/contracts/pool-manager/src/helpers.rs#L423

match pool_type {
    PoolType::StableSwap { .. } => {
        let pools_total: Uint256 = pools
            .into_iter()
            .fold(Uint256::zero(), |acc, x| acc.checked_add(x).unwrap());

        let deposits_total: Uint256 = deposits
            .into_iter()
            .fold(Uint256::zero(), |acc, x| acc.checked_add(x).unwrap());

        let pool_ratio = Decimal256::from_ratio(pools_total, pool_token_supply);
        let deposit_ratio = Decimal256::from_ratio(deposits_total, amount);

        if pool_ratio * one_minus_slippage_tolerance > deposit_ratio {
            return Err(ContractError::MaxSlippageAssertion);
        }
    }
}

Impact

Failure to account for the StableSwap invariant (D) and amplification factor (A) allows for:

  1. Incorrect slippage validation: Transactions with actual slippage exceeding the user-defined tolerance may be accepted.
  2. User losses: Users may incur unexpected losses due to high slippage.

Example: Incorrect slippage validation:

  • Initial pool: [1000, 1000].
  • Amplification factor (A): 100.
  • User deposit: [100, 50].
  • Slippage tolerance: 1% (0.01).
  • pool_token_supply: 2000.
  • Amount (pool token received): 150.

Steps:

  1. Calculate total values:

    • pools_total = 1000 + 1000 = 2000.
    • deposits_total = 100 + 50 = 150.
  2. Calculate ratios:

    • pool_ratio = pools_total / pool_token_supply = 2000 / 2000 = 1.0.
    • deposit_ratio = deposits_total / amount = 150 / 150 = 1.0.
  3. Slippage check:

    • pool_ratio * (1 - slippage_tolerance) = 1.0 * 0.99 = 0.99.
    • deposit_ratio = 1.0.
    • Result: 0.99 > 1.0 is false, so the transaction is accepted.

Real slippage using StableSwap invariant

  • Before deposit: 𝐷initial = calculation based on [1000,1000] and 𝐴 = 100.

Let’s say 𝐷initial = 2000 (perfect equilibrium)

  • After deposit: [1000 + 100, 1000 + 50] = [1100, 1050].

𝐷final = StableSwap invariant recalculated

Let’s say 𝐷final = 2105.

Relative change:

  • Actual slippage = (𝐷final - 𝐷initial) / 𝐷initial = (2105 - 2000) / 2000 = 0.0525 (5.25%).

Comparison with Tolerance:

  • Specified tolerance : 1% (0.01).
  • Actual slippage = 5.25% > 1%, so the transaction should have been rejected.

Risk

  • Likelihood: Medium, the vulnerability depends on specific deposit patterns, such as highly imbalanced deposits.
  • Impact: Medium, users face financial losses.

Recommended mitigation steps

Implement a StableSwap-specific slippage calculation that incorporates the invariant (D) and amplification factor (A):

  • Calculate (D) before and after the deposit.
  • Derive the price impact based on (D) and compare it to the user-defined slippage tolerance.

jvr0x (MANTRA) confirmed


The original report as published on Code4rena, by DOWSERS. 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 slippage check used a linear ratio of summed balances to token supply instead of computing the StableSwap invariant before and after the deposit.

Impact

Users could incur unexpected losses because deposits with real slippage above their tolerance may be accepted; the report rates likelihood and impact as medium.

What the attacker needed

Ability to submit a liquidity deposit; imbalanced deposit amounts; StableSwap pool with amplification factor

The fix

The slippage check should compute the StableSwap invariant before and after the deposit and compare the resulting price impact against the user's tolerance.

How it was found

  1. Take a pool with equal balances and a high amplification factor.
  2. Submit an imbalanced deposit with a tight slippage tolerance.
  3. Observe the ratio-based check accepts the deposit.
  4. Recompute the invariant before and after to see the real slippage exceeds the tolerance.

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 holds two kinds of tokens, and when you add more of one than the other you shift the balance and effectively get a slightly worse deal, called slippage. Users set a maximum slippage they will accept, and the code is meant to reject anything worse. Here the check used a rough ratio that ignores how StableSwap pools actually price trades, so a deposit could pass the check even though the real slippage was far above the limit.

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 from MANTRA DEX

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.