{"record":{"id":"a760ea030f3e2364","repo":"nautechsystems/nautilus_trader","slug":"order-amount-overflow-scaling-quantity-to-raw-toke","errorCode":null,"errorMessage":"Order amount overflow scaling quantity to raw token units","messagePattern":"Order amount overflow scaling quantity to raw token units","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/execution/client.rs","lineNumber":2456,"sourceCode":"/// Converts a base-denominated order quantity into raw token units with exact integer\n/// scaling by the base token decimals.\n///\n/// `Quantity::raw` is scaled to the greater of its declared precision and `FIXED_PRECISION`;\n/// token amounts are scaled to the token's decimals. Quantities not exactly representable in\n/// token units are rejected rather than rounded.\nfn quantity_to_raw_amount(quantity: Quantity, decimals: u8) -> anyhow::Result<U256> {\n    if quantity.is_zero() {\n        anyhow::bail!(\"Order quantity must be positive\");\n    }\n\n    let raw = U256::from(quantity.raw);\n    let raw_precision = quantity.precision.max(FIXED_PRECISION);\n    if decimals >= raw_precision {\n        let scale = U256::from(10u64)\n            .checked_pow(U256::from(decimals - raw_precision))\n            .ok_or_else(|| anyhow::anyhow!(\"Order amount scaling overflow\"))?;\n        raw.checked_mul(scale).ok_or_else(|| {\n            anyhow::anyhow!(\"Order amount overflow scaling quantity to raw token units\")\n        })\n    } else {\n        let divisor = U256::from(10u64)\n            .checked_pow(U256::from(raw_precision - decimals))\n            .ok_or_else(|| anyhow::anyhow!(\"Order amount scaling overflow\"))?;\n        if !(raw % divisor).is_zero() {\n            anyhow::bail!(\n                \"Order quantity {quantity} is not exactly representable in {decimals} base token decimals\"\n            );\n        }\n        Ok(raw / divisor)\n    }\n}\n\n/// Extracts the positive output amount from an exact-input swap quote.\nfn exact_output_amount(quote: &SwapQuote, zero_for_one: bool) -> anyhow::Result<U256> {\n    let amount = if zero_for_one {\n        quote.amount1","sourceCodeStart":2438,"sourceCodeEnd":2474,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/2114cf6f761429e0adb5ca9596fcd7b895b16011/crates/adapters/blockchain/src/execution/client.rs#L2438-L2474","documentation":"Second overflow guard on the same branch of quantity_to_raw_amount: after computing the scale factor successfully, raw * scale is checked. This fires when the order quantity, in fixed-point raw units, is so large that shifting it up to the token's decimals overflows U256.","triggerScenarios":"An enormous order quantity (e.g., more than ~10^59 base units) combined with high token decimals; a fat-fingered or unit-confused quantity (treating whole tokens as raw fixed-point units or vice versa); quantity constructed with the wrong precision.","commonSituations":"Strategy sizing computed against the wrong token (18-decimals quantity applied to a 36-decimals token); copy-paste of amounts between instruments; unbounded position sizing feeding straight into submit_order.","solutions":["Log the quantity, its precision, and the token decimals at order construction and sanity-check the implied whole-token amount.","Cap order size against the wallet balance and the token's total supply.","Fix the sizing unit confusion (whole tokens vs raw fixed-point).","Reject at the strategy layer with a clear message before submission."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// bound the implied whole-token amount before submission\nlet max_units = U256::from(10u64).pow(U256::from(token.decimals + 18)); // 1e18 whole tokens ceiling\nif quantity_to_raw_amount(qty, token.decimals)? > max_units {\n    anyhow::bail!(\"order size implausibly large for {}\", token.symbol);\n}","typeGuard":null,"tryCatchPattern":"Catch the overflow, reject the order at the strategy layer with an explicit size message, and do not resubmit the same quantity.","preventionTips":["Cap order size by wallet balance and token total supply before submission.","Unit-test sizing code so whole-token vs raw fixed-point confusion is caught early."],"tags":["blockchain","order-sizing","arithmetic-overflow","token-decimals"],"backgroundTag":"arithmetic-overflow","analyzedSha":"2114cf6f761429e0adb5ca9596fcd7b895b16011","analyzedAt":"2026-08-21T11:28:30.864Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}