{"record":{"id":"4009fa756d1b287a","repo":"nautechsystems/nautilus_trader","slug":"decimal-value-does-not-fit-in-i64","errorCode":null,"errorMessage":"decimal `{value}` does not fit in i64","messagePattern":"decimal `(.+?)` does not fit in i64","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/lighter/src/websocket/dispatch.rs","lineNumber":2134,"sourceCode":"/// representation, given the instrument's price precision.\npub(crate) fn price_to_ticks(price: &Price, decimals: u8) -> anyhow::Result<u32> {\n    let scaled = price.as_decimal() * Decimal::from(10_i64.pow(u32::from(decimals)));\n    let value = decimal_trunc_to_i64(scaled)\n        .with_context(|| format!(\"price `{price}` overflows i64 at precision {decimals}\"))?;\n    u32::try_from(value).with_context(|| {\n        format!(\"price `{price}` overflows u32 (Lighter limit) at precision {decimals}\")\n    })\n}\n\n/// Truncate a [`Decimal`] toward zero and convert to `i64`, returning an\n/// error if the truncated value does not fit. Avoids the\n/// `decimal.to_string().split('.').parse()` round-trip the previous\n/// implementations used; runs on every exec submit and modify.\nfn decimal_trunc_to_i64(value: Decimal) -> anyhow::Result<i64> {\n    value\n        .trunc()\n        .to_i64()\n        .ok_or_else(|| anyhow::anyhow!(\"decimal `{value}` does not fit in i64\"))\n}\n\n/// Derive a worst-acceptable price (in venue ticks) for `MARKET` /\n/// `STOP_MARKET` / `MARKET_IF_TOUCHED` orders. Buys widen `base` upward,\n/// sells downward, by `slippage_bps`; the result rounds conservatively at\n/// `price_precision` so the venue cap never under-shoots the budget.\npub(crate) fn derive_market_order_price_ticks(\n    base: Decimal,\n    is_buy: bool,\n    price_precision: u8,\n    slippage_bps: u32,\n) -> anyhow::Result<u32> {\n    let slippage = Decimal::new(i64::from(slippage_bps), 4);\n    let widened = if is_buy {\n        base * (Decimal::ONE + slippage)\n    } else {\n        base * (Decimal::ONE - slippage)\n    };","sourceCodeStart":2116,"sourceCodeEnd":2152,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/lighter/src/websocket/dispatch.rs#L2116-L2152","documentation":"The adapter truncates a Decimal order quantity/price to an i64 (venue expects integer raw values) via trunc().to_i64(). If the decimal's integer part exceeds the i64 range it cannot be represented and this error is returned instead of silently corrupting the order.","triggerScenarios":"Any exec submit or modify where the Decimal value (after truncation) is greater than i64::MAX or less than i64::MIN — typically a huge quantity or price caused by bad sizing math or a wrong instrument multiplier.","commonSituations":"Position sizing bug multiplying by a large factor; fetching price/size from the wrong instrument; uninitialized/default order parameters; decimal points misplaced when constructing amounts.","solutions":["Fix the source of the oversized Decimal (check sizing math and units)","Clamp or validate the value against i64 bounds before submitting","Verify instrument-specific price/size multipliers and precision are correct"],"exampleFix":"// before\nlet qty = Decimal::from_str(\"1e20\")?; // overflows i64\n// after\nlet qty = Decimal::from_str(\"1e20\")?;\nanyhow::ensure!(qty <= Decimal::from(i64::MAX), \"qty too large\");","handlingStrategy":"validation","validationCode":"anyhow::ensure!(value >= Decimal::from(i64::MIN) && value <= Decimal::from(i64::MAX), \"value not representable as i64\");","typeGuard":"fn fits_i64(value: Decimal) -> bool {\n    value >= Decimal::from(i64::MIN) && value <= Decimal::from(i64::MAX)\n}","tryCatchPattern":"match decimal_trunc_to_i64(qty) {\n    Ok(v) => submit(v),\n    Err(e) => { log::error!(\"order value overflow: {e}\"); /* halt or recompute size */ }\n}","preventionTips":["Sanity-check order size/price against instrument limits before submit","Verify sizing math and multipliers","Clamp values to i64 range upstream"],"tags":["rust","decimal","overflow","order-submission"],"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-14T00:17:10.932Z"}