{"record":{"id":"39f2fc373cb9b1a8","repo":"nautechsystems/nautilus_trader","slug":"quantity-for-currency-exceeds-signed-raw-bounds","errorCode":null,"errorMessage":"quantity for {currency} exceeds signed raw bounds","messagePattern":"quantity for (.+?) exceeds signed raw bounds","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/portfolio/src/manager.rs","lineNumber":1422,"sourceCode":"                },\n            ),\n        }\n    }\n}\n\n#[allow(\n    clippy::useless_conversion,\n    reason = \"the raw width differs when high-precision is disabled\"\n)]\nfn wallet_money_from_quantity(quantity: Quantity, currency: Currency) -> anyhow::Result<Money> {\n    anyhow::ensure!(!quantity.is_undefined(), \"quantity was undefined\");\n    Quantity::from_raw_checked(quantity.raw, quantity.precision)?;\n    check_fixed_raw_u128(u128::from(quantity.raw), quantity.precision)?;\n\n    let source_precision = quantity.precision.max(FIXED_PRECISION);\n    let target_precision = currency.precision.max(FIXED_PRECISION);\n    let raw = i128::try_from(u128::from(quantity.raw))\n        .map_err(|_| anyhow::anyhow!(\"quantity for {currency} exceeds signed raw bounds\"))?;\n    let raw = match source_precision.cmp(&target_precision) {\n        Ordering::Less => {\n            let scale = 10_i128.pow(u32::from(target_precision - source_precision));\n            raw.checked_mul(scale).ok_or_else(|| {\n                anyhow::anyhow!(\"quantity for {currency} overflowed while increasing raw scale\")\n            })?\n        }\n        Ordering::Greater => {\n            let scale = 10_i128.pow(u32::from(source_precision - target_precision));\n            anyhow::ensure!(\n                raw % scale == 0,\n                \"quantity for {currency} loses precision when decreasing raw scale\"\n            );\n            raw / scale\n        }\n        Ordering::Equal => raw,\n    };\n    check_fixed_raw_i128(raw, currency.precision)?;","sourceCodeStart":1404,"sourceCodeEnd":1440,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/portfolio/src/manager.rs#L1404-L1440","documentation":"The quantity's raw fixed-precision value is stored as u64 but Money raw values are i128-backed. Converting u128 -> i128 fails if the raw value exceeds i128::MAX; this error signals the quantity magnitude cannot fit the signed representation required for Money arithmetic.","triggerScenarios":"Passing a Quantity with an extremely large raw value (near u64::MAX at its precision) into wallet_money_from_quantity via update_balance_locked_wallet.","commonSituations":"Corrupted or overflowed quantity arithmetic upstream; wrong precision making raw values enormous; synthetic/test data with sentinel-like huge values.","solutions":["Check upstream arithmetic for overflow that produced the huge raw value","Use a smaller quantity / correct precision consistent with the instrument","Add a magnitude check before calling update_balance_locked_wallet","Verify Quantity precision matches the currency/instrument precision"],"exampleFix":"// before\nmanager.update_balance_locked_wallet(&instrument.id(), asset, huge_quantity)?;\n// after\nlet max_raw = i128::from(u64::MAX); // u64 raw always fits i128; guard anyway for high-precision paths\nanyhow::ensure!(u128::from(huge_quantity.raw) <= i128::MAX as u128, \"quantity too large\");\nmanager.update_balance_locked_wallet(&instrument.id(), asset, huge_quantity)?;","handlingStrategy":"validation","validationCode":"fn fits_signed_raw(q: &Quantity) -> bool {\n    u128::from(q.raw) <= i128::MAX as u128\n}\nanyhow::ensure!(fits_signed_raw(&quantity), \"quantity too large for Money conversion\");","typeGuard":"fn fits_signed_raw(q: &Quantity) -> bool { u128::from(q.raw) <= i128::MAX as u128 }","tryCatchPattern":"manager.update_balance_locked_wallet(&id, currency, quantity)\n    .map_err(|e| { error!(\"wallet update failed: {e}\"); e })?;","preventionTips":["Investigate any raw value near u64::MAX — it likely indicates upstream overflow","Keep quantity precision matched to the instrument spec","Never feed sentinel-like huge values into balance updates"],"tags":["rust","portfolio","overflow","precision"],"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-14T05:17:10.506Z"}