Skip to content
Smart contracts Audit finding Advanced

Incorrect rounding direction in geometric pool `ask_exact_amount_out` allows theft of funds

The geometric pool's ask_exact_amount_out function rounds the required input amount down in favour of the trader, and then truncates the decimal value again when converting to an integer. An attacker could repeatedly swap to receive more quote currency than the BTC they pay, draining the BTC pool over many swaps.

haxagon Follow with heavyw8t

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2025-09-dango-dex-judging/issues/68

Found by

haxagon, heavyw8t

Summary

Incorrect rounding direction in geometric pool ask_exact_amount_out allows theft of funds

Root Cause

There is an incorrect rounding direction in geometric pool ask_exact_amount_out which allows theft of funds.

https://github.com/left-curve/left-curve/blob/main/dango/dex/src/core/geometric.rs#L185-L206

RS
fn ask_exact_amount_out(
    ask_amount_in_quote: Uint128,
    passive_bids: Box<dyn Iterator<Item = (Price, Uint128)>>,
) -> anyhow::Result<Uint128> {
    let mut remaining_ask_in_quote = ask_amount_in_quote.checked_into_dec::<6>()?;
    let mut input_amount = Udec128::ZERO;

    for (price, amount) in passive_bids {
        let remaining_ask = remaining_ask_in_quote.checked_div_dec_floor(price)?;
        let matched_amount = cmp::min(amount.checked_into_dec()?, remaining_ask);
        input_amount.checked_add_assign(matched_amount)?;

        let matched_amount_in_quote = matched_amount.checked_mul_dec_ceil(price)?;
        remaining_ask_in_quote.checked_sub_assign(matched_amount_in_quote)?;

        if remaining_ask_in_quote.is_zero()
            || remaining_ask.is_zero()
            || matched_amount_in_quote.is_zero()
        {
            return Ok(input_amount.into_int());
        }
    }

    bail!("not enough liquidity to fulfill the swap! remaining amount: {remaining_ask_in_quote}")
}

remaining_ask_in_quote.checked_div_dec_floor favors the trader, as the matched_amount will be lower resulting in a lesser input amount for an exact output. into_int which is the bigger issue then also truncates again later, also favoring the trader.

Note: the issue also affects the bid_exact_amount_out function as it floors the input_amount using the same into_int function. But the attack path is less strong there but still an issue nevertheless because you can only round down the USDC.

Internal Pre-conditions

External Pre-conditions

Attack Path

Consider a scenario BTC-USDC pool where 1 sat unit of BTC = 1999 units of USDC. An attacker can execute a swap with exact amount out 1999 units of USDC which translates to 1.999 sat of BTC.

Since remaining_ask_in_quote.checked_div_dec_floor(price)? operates on Udec128, there isn't a big impact here yet as the floor is only occuring at the decimal point value. However once we reach input_amount.into_int() the entire decimal value of input_amount which is a Udec128 will get completely truncated, resulting in an input_amount of 1 sats of BTC

Essentially, the attacker can obtain 1999 units of USDC while paying 1 sats of BTC every swap (actual price: 1000 units of USDC per 1 sat of BTC). Eventually this can accumulate with multiple swaps to drain the BTC pool.

Impact

Loss of funds.

PoC

No response

Mitigation

No response

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

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 function uses floor division and integer truncation when computing the input amount for an exact output, so rounding always favours the trader instead of the pool.

Impact

The attacker could obtain more USDC than the BTC paid in each swap, and repeated swaps could drain the BTC pool; the text also notes a weaker variant in bid_exact_amount_out.

What the attacker needed

A BTC-USDC geometric pool with liquidity; ability to submit swaps

How it was found

  1. Set up a BTC-USDC pool where one satoshi of BTC is worth 1999 units of USDC.
  2. Request an exact output of 1999 units of USDC, which corresponds to 1.999 satoshi of BTC.
  3. The function floors the division and then truncates the decimal input amount, so the attacker pays only 1 satoshi of BTC.
  4. Repeat the swap many times to accumulate profit and drain the BTC 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 trading pool calculates how much of one coin you must pay to get an exact amount of another. Here the calculation always rounds the amount you pay down, and then throws away the decimal part entirely, so you pay slightly less than you should. By repeating the swap many times, an attacker could slowly take more value out of the pool than they put in.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Dango DEX

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.