{"record":{"id":"1cffea0dd520a76d","repo":"nautechsystems/nautilus_trader","slug":"swap-quote-output-amount-amount-is-not-a-positiv","errorCode":null,"errorMessage":"Swap quote output amount {amount} is not a positive output","messagePattern":"Swap quote output amount (.+?) is not a positive output","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/execution/client.rs","lineNumber":5278,"sourceCode":"    };\n\n    if quantity.is_zero() {\n        anyhow::bail!(\n            \"Executed amount {amount} is below representable quantity precision {FIXED_PRECISION}\"\n        );\n    }\n    Ok(quantity)\n}\n\nfn exact_output_amount(quote: &SwapQuote, zero_for_one: bool) -> anyhow::Result<U256> {\n    let amount = if zero_for_one {\n        quote.amount1\n    } else {\n        quote.amount0\n    };\n\n    if !amount.is_negative() {\n        anyhow::bail!(\"Swap quote output amount {amount} is not a positive output\");\n    }\n    Ok(amount.unsigned_abs())\n}\n\n/// Derives the minimum output accepted for the swap: the quoted output reduced by\n/// `slippage_bps`, in exact integer arithmetic. Rejects a zero minimum, which would leave\n/// the swap without slippage protection.\nfn derive_min_amount_out(quoted_amount_out: U256, slippage_bps: u32) -> anyhow::Result<U256> {\n    if slippage_bps >= BPS_DENOMINATOR {\n        anyhow::bail!(\"Slippage {slippage_bps} bps must be below {BPS_DENOMINATOR}\");\n    }\n    let min_amount_out = quoted_amount_out\n        .checked_mul(U256::from(BPS_DENOMINATOR - slippage_bps))\n        .and_then(|scaled| scaled.checked_div(U256::from(BPS_DENOMINATOR)))\n        .ok_or_else(|| anyhow::anyhow!(\"Minimum output derivation overflow\"))?;\n    if min_amount_out.is_zero() {\n        anyhow::bail!(\n            \"Derived minimum output is zero for quoted output {quoted_amount_out} at {slippage_bps} bps slippage\"","sourceCodeStart":5260,"sourceCodeEnd":5296,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/execution/client.rs#L5260-L5296","documentation":"When extracting a swap quote's output amount, the code selects amount0 or amount1 (zeroForOne) and requires it to be negative (an output is a decrease in that token's balance). If the selected amount is not negative, the quote does not represent a positive output and the adapter bails before converting to U256 via unsigned_abs.","triggerScenarios":"Calling the swap-quote helper with a quote whose selected output amount (amount0 for zeroForOne==false, amount1 for zeroForOne==true) is >= 0, e.g. wrong zeroForOne flag or an inverted/invalid quote from the pool.","commonSituations":"zeroForOne direction computed inconsistently with the pool's token ordering; quote fetched from a pool where tokens were flipped; simulator returning zero/negative-output quotes on illiquid pools.","solutions":["Check the zeroForOne flag matches the swap direction (zeroForOne means output is amount1)","Re-fetch the quote; it may be from a stale or empty pool state","Verify pool token0/token1 ordering matches the adapter's assumption"],"exampleFix":"// before\nlet amount = if zero_for_one { quote.amount1 } else { quote.amount0 };\n// after\nlet zero_for_one = quote.currency0 < quote.currency1 ? zero_for_one_in : !zero_for_one_in;\nlet amount = if zero_for_one { quote.amount1 } else { quote.amount0 };","handlingStrategy":"validation","validationCode":"let selected = if zero_for_one { quote.amount1 } else { quote.amount0 };\nif !selected.is_negative() {\n    return Err(anyhow::anyhow!(\"quote output not negative; check zeroForOne direction\"));\n}","typeGuard":"fn has_positive_output(q: &Quote, zero_for_one: bool) -> bool {\n    let amt = if zero_for_one { q.amount1 } else { q.amount0 };\n    amt.is_negative()\n}","tryCatchPattern":"match build_swap_output(quote, zero_for_one) {\n    Ok(out) => proceed(out),\n    Err(e) if e.to_string().contains(\"not a positive output\") => recheck_direction_and_requote(),\n    Err(e) => return Err(e),\n}","preventionTips":["Derive zeroForOne from sorted token addresses consistently everywhere","Re-quote once before failing; stale pool state can produce inverted amounts"],"tags":["blockchain","swap-quote","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}