{"record":{"id":"15dd3c7238fb835f","repo":"nautechsystems/nautilus_trader","slug":"minimum-output-derivation-overflow","errorCode":null,"errorMessage":"Minimum output derivation overflow","messagePattern":"Minimum output derivation overflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/execution/client.rs","lineNumber":5293,"sourceCode":"    };\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\"\n        );\n    }\n    Ok(min_amount_out)\n}\n\nimpl BlockchainExecutionClient {\n    async fn build_execution_verification_migration(\n        &self,\n        snapshot: ExecutionVerificationMigrationSnapshot,\n        finalized: VerifiedBlockHeader,\n        finalized_headers: &[VerifiedBlockHeader],\n        nonce_verification: &Verified<u64>,\n    ) -> anyhow::Result<ExecutionVerificationMigration> {\n        let next_canonical_nonce = nonce_verification.value;\n        let mut hashes_by_intent: HashMap<i64, Vec<&ExecutionTransactionHashRow>> = HashMap::new();","sourceCodeStart":5275,"sourceCodeEnd":5311,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/execution/client.rs#L5275-L5311","documentation":"The client derives a swap's minimum acceptable output as quoted_amount_out * (BPS_DENOMINATOR - slippage_bps) / BPS_DENOMINATOR using checked arithmetic. This error is thrown when either the multiplication or the subsequent division overflows/cannot complete in U256, so a safe minimum output cannot be computed.","triggerScenarios":"Building a swap transaction with a quoted_amount_out large enough that multiplying by (10000 - slippage_bps) overflows U256, while slippage_bps is itself valid (< 10000).","commonSituations":"Quoting extremely large trades (huge output amounts) on low-decimal tokens; a quoting feed returning a corrupted, near-U256-max amount; slippage configured so the multiplier is near the full 10000 bps with a maximal quote.","solutions":["Reduce the trade size so quoted_amount_out fits after scaling","Sanitize/validate the quote feed — reject quotes whose raw value is implausibly large","Split the swap into multiple smaller swaps, or perform the bps scaling in a wider integer type before narrowing to U256"],"exampleFix":"// before\nquoted_amount_out.checked_mul(U256::from(BPS_DENOMINATOR - slippage_bps))\n// after\nlet factor = U256::from(BPS_DENOMINATOR - slippage_bps);\nif quoted_amount_out > U256::MAX / factor { anyhow::bail!(\"quote too large for slippage scaling\"); }\nquoted_amount_out.checked_mul(factor)","handlingStrategy":"validation","validationCode":"fn assert_min_out_derivable(quoted: U256, slippage_bps: u64) -> Result<(), String> {\n    if slippage_bps >= BPS_DENOMINATOR { return Err(\"slippage must be < 10000 bps\".into()); }\n    let factor = U256::from(BPS_DENOMINATOR - slippage_bps);\n    if quoted > U256::MAX / factor { return Err(\"quote too large for slippage scaling\".into()); }\n    if quoted * factor / U256::from(BPS_DENOMINATOR) == U256::zero() { return Err(\"min output rounds to zero\".into()); }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match client.build_swap(...) {\n    Err(e) if e.to_string().contains(\"Minimum output derivation overflow\") => { /* split trade or reject quote */ }\n    other => other?,\n}","preventionTips":["Validate quote magnitudes against a per-token cap before building swaps","Bound slippage_bps to a sane band (e.g. 1..=5000)","Split oversized swaps into tranches"],"tags":["overflow","u256","arithmetic","slippage","swap"],"backgroundTag":"value-out-of-range","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"}