{"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":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/execution/client.rs","lineNumber":5213,"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\nfn swap_token_pair(\n    side: OrderSide,\n    base: Address,\n    quote: Address,","sourceCodeStart":5195,"sourceCodeEnd":5231,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/execution/client.rs#L5195-L5231","documentation":"After scaling an order quantity up to raw token units, the client multiplies the raw U256 value by 10^(decimals - raw_precision). This error is thrown when that multiplication overflows U256, i.e. the quantity is too large to express in base token units.","triggerScenarios":"Submitting an order whose quantity, once scaled by the token decimal delta, exceeds U256::MAX — e.g. a quantity at fixed precision near 2^256/10^k with decimals > raw_precision.","commonSituations":"Order size typo (e.g. entering 1,000,000,000 instead of 1.0); wrongly configured token decimals inflating the scale factor; trading a meme token with huge supply and large configured decimals.","solutions":["Reduce the order quantity to a value representable in raw token units","Verify token decimals against the on-chain contract and correct the instrument config","Add a pre-trade sanity check that quantity scaled fits within U256 and reject oversized orders before submission"],"exampleFix":"// before\nraw.checked_mul(scale).ok_or_else(|| anyhow::anyhow!(\"Order amount overflow scaling quantity to raw token units\"))\n// after\nlet max_raw = U256::MAX / scale;\nif raw > max_raw { anyhow::bail!(\"quantity {} exceeds raw token capacity\", quantity); }\nraw.checked_mul(scale).ok_or_else(|| anyhow::anyhow!(\"Order amount overflow scaling quantity to raw token units\"))","handlingStrategy":"validation","validationCode":"fn assert_order_size(quantity_raw: U256, scale: U256) -> Result<(), String> {\n    if quantity_raw > U256::MAX / scale { Err(\"order quantity exceeds raw token capacity\".into()) } else { Ok(()) }\n}","typeGuard":null,"tryCatchPattern":"match client.submit_order(...) {\n    Err(e) if e.to_string().contains(\"Order amount overflow scaling quantity\") => { /* halve size / re-check decimals */ }\n    other => other?,\n}","preventionTips":["Add a max-notional/max-size pre-trade risk check","Verify token decimals against the chain before trading","Sanity-check order sizes against the token's total supply"],"tags":["overflow","u256","arithmetic","blockchain","order-size"],"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"}