XYK reflect_curve omits swap fee in order sizing, leaking LP fees
The XYK pool's reflect_curve function sizes passive bid and ask orders without including the swap fee, so takers can match those orders without paying the fee. This means liquidity providers lose the fee revenue they should have earned on those fills.
Source: https://github.com/sherlock-audit/2025-09-dango-dex-judging/issues/166
Found by
blockace, haxagon
Summary
The reflect_curve function for the XYK pool sizes passive bid/ask orders without accounting for the swap fee, allowing takers to match those orders effectively fee-free. This under-collects fees that should accrue to liquidity providers, causing direct LP losses in addition to normal impermanent loss.
Root Cause
In dango/dex/src/core/xyk.rs, the total order size at price p is computed from the reserves as if there were no swap fee. For bids, the size uses quote_reserve / p - base_reserve; for asks, it uses base_reserve - quote_reserve / p. The fee factor (1 - swap_fee) is omitted, so passive orders are reflected at sizes that do not accrue the fee when taken.
Relevant code (bids):
// dango/dex/src/core/xyk.rs (bids)
let quote_reserve_div_price = quote_reserve.checked_div_dec(price).ok()?;
let mut size = quote_reserve_div_price.checked_sub(base_reserve).ok()?;
Relevant code (asks):
// dango/dex/src/core/xyk.rs (asks)
let quote_reserve_div_price = quote_reserve.checked_div_dec(price).ok()?;
let size = base_reserve.checked_sub(quote_reserve_div_price).ok()?;
Mathematically, for a bid at price p with reserves R_b and R_q and fee f, the equality s * p = (R_q * s)/(R_b + s) * (1 - f) yields s = R_q * (1 - f) / p - R_b. The implementation lacks the (1 - f) term. A symmetric adjustment is required for asks as well.
Detailed derivation
-
Bids (pool buys base, taker sells base):
- Reserves:
R_b(base),R_q(quote); pricep(quote per base); feef. - Exact-in (base in) output after fee:
Q_out = (R_q * s)/(R_b + s) * (1 - f). - Passive bid at price
ppromisess * pquote forsbase: sets * p = Q_out. - Solve:
p = (R_q/(R_b + s)) * (1 - f)⇒R_b + s = R_q * (1 - f) / p⇒s = R_q * (1 - f) / p - R_b. - Therefore total size at price
pmust beRq*(1-f)/p - Rb.
- Reserves:
-
Asks (pool sells base, taker buys base):
- Taker pays quote
x = s * p, wants to receivesbase. - Exact-in (quote in) output after fee:
B_out = (R_b * x)/(R_q + x) * (1 - f). - Set
s = B_outwithx = s * p:s = (R_b * s * p)/(R_q + s * p) * (1 - f)⇒1 = (R_b * p)/(R_q + s * p) * (1 - f). - Hence
R_q + s * p = R_b * p * (1 - f)⇒s * p = R_b * p * (1 - f) - R_q⇒s = R_b * (1 - f) - R_q / p. - Therefore total size at price
pmust beRb*(1-f) - Rq/p.
- Taker pays quote
Internal Pre-conditions
.
External Pre-conditions
.
Attack Path
- A trader creates bid/ask orders based on XYK passive pool liquidity.
- The trader doesn't pay the swap fee when taking the orders.
- The pool doesn't accrue the swap fee, causing loss for LPs.
Impact
- Liquidity providers lose expected fee revenue on reflected (passive) fills.
- Traders bypass the swap fee by taking the orders.
PoC
The following test demonstrates that, after matching the first passive bid, the pool’s invariant K = X * Y does not increase, showing that swap fees were not accrued to the pool.
// dango/testing/tests/dex.rs
#[test]
fn xyk_reflect_curve_incorrect_size_poc() {
let (mut suite, mut accounts, _, contracts, _) = setup_test_naive(Default::default());
// Setup, DANGO:USDC = 1:1
let pool_type = PassiveLiquidity::Xyk(Xyk {
spacing: Udec128::ONE.checked_div(Udec128::new(100)).unwrap(),
reserve_ratio: Bounded::new_unchecked(Udec128::ZERO),
limit: 10,
});
let lp_denom = Denom::try_from("dex/pool/dango/usdc").unwrap();
let initial_reserves = coins! {
dango::DENOM.clone() => 10000,
usdc::DENOM.clone() => 10000,
};
let swap_fee = Udec128::new_percent(1); // 1% swap fee
let oracle_prices = vec![
(dango::DENOM.clone(), Udec128::new(1)),
(usdc::DENOM.clone(), Udec128::new(1)),
];
// Add the pair to the DEX
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: lp_denom.clone(),
bucket_sizes: BTreeSet::new(),
swap_fee_rate: Bounded::new_unchecked(swap_fee),
pool_type,
min_order_size: Uint128::ZERO,
},
}])),
Coins::new(),
)
.should_succeed();
// Register the oracle prices
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();
}
// Provide initial liquidity to the Pool
suite
.execute(
&mut accounts.owner,
contracts.dex,
&dex::ExecuteMsg::ProvideLiquidity {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
},
initial_reserves.clone(),
)
.should_succeed();
// For XYK, first passive bid price should be 0.99 which is (1 - swap_fee) and size will be 101.01010101...
// So create an ask order that matches the first passive bid order
suite.balances().record(&accounts.user1);
let ask_funds = Coins::one(dango::DENOM.clone(), Uint128::new(101)).unwrap();
let msg = Message::execute(
contracts.dex,
&dex::ExecuteMsg::BatchUpdateOrders {
creates: vec![CreateOrderRequest::new_limit(
dango::DENOM.clone(),
usdc::DENOM.clone(),
Direction::Ask,
NonZero::new_unchecked(Price::checked_from_ratio(99, 100).unwrap()), // 0.99
NonZero::new_unchecked(Uint128::new(101)),
)],
cancels: None,
},
ask_funds,
)
.unwrap();
let tx = accounts
.user1
.sign_transaction(NonEmpty::new_unchecked(vec![msg]), &suite.chain_id, 100_000)
.unwrap();
suite
.make_block(vec![tx])
.block_outcome
.tx_outcomes
.into_iter()
.for_each(|outcome| {
outcome.should_succeed();
});
// Check that the user balance has changed, exchanging 101 DANGO for 99 USDC
suite.balances().should_change(&accounts.user1, btree_map! {
dango::DENOM.clone() => BalanceChange::Decreased(101),
usdc::DENOM.clone() => BalanceChange::Increased(99),
});
// Now check the reserves and K = X * Y
let reserve = suite
.query_wasm_smart(contracts.dex, QueryReserveRequest {
base_denom: dango::DENOM.clone(),
quote_denom: usdc::DENOM.clone(),
})
.unwrap();
let dango_reserve = reserve
.amount_of(&dango::DENOM.clone())
.unwrap()
.into_inner();
let usdc_reserve = reserve
.amount_of(&usdc::DENOM.clone())
.unwrap()
.into_inner();
println!("DANGO reserve: {}", dango_reserve);
println!("USDC reserve: {}", usdc_reserve);
// Verifies that swap fee not accrued that means the constant product K is not increased
assert!(dango_reserve * usdc_reserve <= 10000 * 10000);
}
Mitigation
Adjust the sizing formulas to incorporate the swap fee:
- Bids: use
s_total(p) = R_q * (1 - f) / p - R_b. - Asks: use
s_total(p) = R_b * (1 - f) - R_q / p.
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
The order-sizing formula omits the (1 - swap_fee) factor, so reflected passive orders are sized as if no fee applied.
Impact
Liquidity providers lose expected fee revenue on reflected passive fills, and traders can bypass the swap fee by taking those orders.
What the attacker needed
A deployed XYK pool with a non-zero swap fee; a trader able to place matching orders
The fix
The sizing formulas should be adjusted to include the swap fee: for bids use R_q * (1 - f) / p - R_b and for asks use R_b * (1 - f) - R_q / p.
How it was found
- Set up a pool with equal reserves and a 1% swap fee.
- Create an ask order that matches the first passive bid order.
- Observe the trader's balances change as if no fee was charged.
- Check the reserves and see that the constant product K did not increase.
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 tokens and charges a small fee each time someone trades against it; that fee goes to the people who deposited the tokens. Here the pool also posts standing orders at prices derived from its reserves, but it calculated their sizes as if no fee existed. So a trader could fill those orders and skip the fee, and the depositors lost that income.
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.