Skip to content
Business logic Audit finding Intermediate

Pool creators can manipulate the slippage calculation for liquidity providers

Pool creators can specify the same token denominations in a different order when creating a pool, which causes the slippage tolerance check to use an inverted ratio. This can lead liquidity providers to accept unfavourable deposit ratios, resulting in a loss of funds.

DadeKuma Follow with 0x1982us

Originally disclosed on Code4rena

Submitted by DadeKuma, also found by 0x1982us

Pool creation is permissionless, and users can create a pool by specifying asset denoms. The issue is that they can put a different order of the same token denoms, which should result in the same pool, but in fact, it does not.

This ultimately cause the slippage mechanism to use the inverse ratio instead of the correct one, as in other parts of the codebase these values are always ordered, which will cause a loss of funds for the users that provide liquidity as they use the inverted slippage.

Proof of Concept

Users can create a pool by calling pool_manager::create_pool and specifying the asset_denoms.

They can call this function with the same denoms, but in a different order. In theory, this should result in the same pool, but this isn’t the case.

Suppose Bob adds liquidity on a [uwhale, uluna] pool instead of a [uluna, uwhale] pool. The slippage tolerance check is triggered:

    // assert slippage tolerance
    helpers::assert_slippage_tolerance(
        &slippage_tolerance,
        &deposits,
        &pool_assets,
        pool.pool_type.clone(),
        share,
        total_share,
    )?;

/contracts/pool-manager/src/liquidity/commands.rs#L271

This results in the following issue: supposing a k-product pool with n = 2 tokens, we calculate the slippage check:

    PoolType::ConstantProduct => {
	    if deposits.len() != 2 || pools.len() != 2 {
	        return Err(ContractError::InvalidPoolAssetsLength {
	            expected: 2,
	            actual: deposits.len(),
	        });
	    }
      //@audit-info added these logs to check the actual ratios
      println!("------> Slippage ratios, d1: {}, p1: {}, d2: {}, p2: {}", deposits[0], pools[0], deposits[1], pools[1]);
      println!("1st ratio check: {} > {}", Decimal256::from_ratio(deposits[0], deposits[1]) * one_minus_slippage_tolerance, Decimal256::from_ratio(pools[0], pools[1]));
      println!("2nd ratio check: {} > {}", Decimal256::from_ratio(deposits[1], deposits[0]) * one_minus_slippage_tolerance, Decimal256::from_ratio(pools[1], pools[0]));
	    if Decimal256::from_ratio(deposits[0], deposits[1]) * one_minus_slippage_tolerance
->	        > Decimal256::from_ratio(pools[0], pools[1])
	        || Decimal256::from_ratio(deposits[1], deposits[0])
	            * one_minus_slippage_tolerance
->	            > Decimal256::from_ratio(pools[1], pools[0])
	    {
	        return Err(ContractError::MaxSlippageAssertion);
	    }
	}

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

deposits are always sorted but the pool order is determined on creation. As the pool has [uwhale, uluna] instead of [uluna, uwhale] denominations, the slippage checks are inverted.

Coded POC

Run the following test in contracts/pool-manager/src/tests/integration_tests.rs:

    #[test]
    fn audit_test_wrong_slippage_2_kproduct() {
        let mut suite = TestingSuite::default_with_balances(
            vec![
                coin(1_000_000_001u128, "uwhale".to_string()),
                coin(1_000_000_000u128, "uluna".to_string()),
                coin(1_000_000_001u128, "uusd".to_string()),
                coin(1_000_000_001u128, "uom".to_string()),
            ],
            StargateMock::new("uom".to_string(), "8888".to_string()),
        );
        let creator = suite.creator();
        let _other = suite.senders[1].clone();
        let _unauthorized = suite.senders[2].clone();
        // Asset infos with uwhale and uluna

        let asset_infos = vec![
            "uwhale".to_string(),
            "uluna".to_string(),
        ];

        // Protocol fee is 0.01% and swap fee is 0.02% and burn fee is 0%
        let pool_fees = PoolFee {
            protocol_fee: Fee {
                share: Decimal::from_ratio(1u128, 1000u128),
            },
            swap_fee: Fee {
                share: Decimal::from_ratio(1u128, 10_000_u128),
            },
            burn_fee: Fee {
                share: Decimal::zero(),
            },
            extra_fees: vec![],
        };

        // Create a 2 token pool
        suite.instantiate_default().create_pool(
            &creator,
            asset_infos,
            vec![6u8, 6u8],
            pool_fees,
            PoolType::ConstantProduct { },
            Some("whale.uluna".to_string()),
            vec![coin(1000, "uusd"), coin(8888, "uom")],
            |result| {
                result.unwrap();
            },
        );

        // first liquidity
        suite.provide_liquidity_slippage(
            &creator,
            "o.whale.uluna".to_string(),
            None,
            None,
            None,
            None,
            None,
            vec![
                Coin {
                    denom: "uwhale".to_string(),
                    amount: Uint128::from(100_000u128),
                },
                Coin {
                    denom: "uluna".to_string(),
                    amount: Uint128::from(1_000_000u128),
                },
            ],
            |result| {
                // Ensure we got 999000 in the response which is 1mil less the initial liquidity amount
                for event in result.unwrap().events {
                    println!("{:?}", event);
                }
            },
        );

        // second liquidity
        suite.provide_liquidity_slippage(
            &creator,
            "o.whale.uluna".to_string(),
            None,
            None,
            None,
            Some(Decimal::percent(60)),
            None,
            vec![
                Coin {
                    denom: "uluna".to_string(),
                    amount: Uint128::from(1_000_000u128),
                },
                Coin {
                    denom: "uwhale".to_string(),
                    amount: Uint128::from(1_200_00u128),
                },

            ],
            |result| {
                // Ensure we got 999000 in the response which is 1mil less the initial liquidity amount
                for event in result.unwrap().events {
                    println!("{:?}", event);
                }
            },
        );
    }

Output:

------> Slippage ratios, d1: 1000000, p1: 100000, d2: 120000, p2: 1000000
1st ratio check: 3.333333333333333333 > 0.1
2nd ratio check: 0.048 > 10
thread 'tests::integration_tests::provide_liquidity::audit_test_wrong_slippage_2_kproduct' panicked at contracts/pool-manager/src/tests/integration_tests.rs:5418:37:
called `Result::unwrap()` on an `Err` value: Error executing WasmMsg:
  sender: mantra15n2dapfyf7mzz70y0srycnduw5skp0s9u9g74e
  Execute { contract_addr: "mantra1zwv6feuzhy6a9wekh96cd57lsarmqlwxdypdsplw6zhfncqw6ftqlydlr9", msg: {"provide_liquidity":{"slippage_tolerance":"0.6","max_spread":null,"receiver":null,"pool_identifier":"o.whale.uluna","unlocking_duration":null,"lock_position_identifier":null}}, funds: [Coin { 1000000 "uluna" }, Coin { 120000 "uwhale" }] }

If we switch the asset_infos order while creating the pool:

    let asset_infos = vec![
-       "uwhale".to_string(),
        "uluna".to_string(),
+       "uwhale".to_string(),
    ];

Output:

------> Slippage ratios, d1: 1000000, p1: 1000000, d2: 120000, p2: 100000
1st ratio check: 3.333333333333333333 > 10
2nd ratio check: 0.048 > 0.1
Event { ty: "execute", attributes: [Attribute { key: "_contract_address", value: "mantra1zwv6feuzhy6a9wekh96cd57lsarmqlwxdypdsplw6zhfncqw6ftqlydlr9" }] }
Event { ty: "wasm", attributes: [Attribute { key: "_contract_address", value: "mantra1zwv6feuzhy6a9wekh96cd57lsarmqlwxdypdsplw6zhfncqw6ftqlydlr9" }, Attribute { key: "action", value: "provide_liquidity" }, Attribute { key: "sender", value: "mantra15n2dapfyf7mzz70y0srycnduw5skp0s9u9g74e" }, Attribute { key: "receiver", value: "mantra15n2dapfyf7mzz70y0srycnduw5skp0s9u9g74e" }, Attribute { key: "assets", value: "2000000uluna, 220000uwhale" }, Attribute { key: "share", value: "316227" }] }

Recommended mitigation steps

In pool_manager::create_pool, consider sorting the asset denoms, similarly to other parts of the code, by introducing a new struct to encapsulate both asset_denoms and asset_decimals (as they are tied together) and reorder it before creating the pool.

jvr0x (MANTRA) confirmed


The original report as published on Code4rena, by DadeKuma. 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

Impact

Liquidity providers could lose funds because the slippage tolerance check uses an inverted ratio, allowing deposits at unfavourable ratios.

What the attacker needed

Permissionless pool creation; liquidity provider interacts with pool

The fix

The pool creation function should sort the asset denominations (and corresponding decimals) before creating the pool, as other parts of the codebase do.

How it was found

  1. Create a pool with asset denominations in a non-sorted order (e.g., uwhale then uluna).
  2. Provide liquidity to that pool with a slippage tolerance set.
  3. Observe that the slippage check uses inverted ratios, allowing a deposit that should fail the tolerance check.

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.

When you add money to a trading pool, the system checks that the ratio of the two coins you deposit is close to the pool's current ratio, to protect you from a bad trade. The bug is that if the pool was created with the coins listed in a different order, this check compares the wrong way round, so you might deposit at a much worse ratio than you intended and lose money.

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 DadeKuma

View all

19 more disclosed reports, best first.

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.