{"record":{"id":"a0d05e24e4a6a3fd","repo":"nautechsystems/nautilus_trader","slug":"quantity-for-currency-overflowed-while-increasin","errorCode":null,"errorMessage":"quantity for {currency} overflowed while increasing raw scale","messagePattern":"quantity for (.+?) overflowed while increasing raw scale","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/portfolio/src/manager.rs","lineNumber":1427,"sourceCode":"\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)?;\n    let raw: MoneyRaw = raw\n        .try_into()\n        .map_err(|_| anyhow::anyhow!(\"quantity for {currency} exceeds Money raw bounds\"))?;\n\n    Money::from_raw_checked(raw, currency).map_err(Into::into)","sourceCodeStart":1409,"sourceCodeEnd":1445,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/portfolio/src/manager.rs#L1409-L1445","documentation":"When the quantity's source precision is lower than the target (currency/FIXED) precision, the raw value is scaled up by 10^(target-source) using checked_mul. If that multiplication overflows i128, this error is thrown instead of silently wrapping.","triggerScenarios":"Converting a quantity whose precision is lower than the currency's precision while its raw value is large enough that raw * 10^scale exceeds i128 bounds.","commonSituations":"Currency precision set higher than instrument precision with large balances; misconfigured currency precision (e.g. precision 9-12 crypto tokens) combined with huge raw quantities.","solutions":["Verify currency precision is configured correctly for the asset","Keep quantity precision aligned with currency precision to avoid scaling","Reduce the quantity magnitude (data likely corrupted if balance is astronomically large)","Add checked arithmetic guards in strategy code before reporting balances"],"exampleFix":"// before\ncurrency = Currency::from(\"SHIB\"); // precision 12\nqty = Quantity::from_raw(u64::MAX, 0); // scaling overflows\n// after\nqty = Quantity::new(1_000.0, currency.precision); // precision matches; no scaling","handlingStrategy":"validation","validationCode":"let scale = 10_i128.pow((currency.precision.max(FIXED_PRECISION) - quantity.precision) as u32);\nlet raw = i128::try_from(u128::from(quantity.raw)).unwrap();\nanyhow::ensure!(raw.checked_mul(scale).is_some(), \"scaling would overflow; reduce quantity or precision\");","typeGuard":null,"tryCatchPattern":"manager.update_balance_locked_wallet(&id, currency, quantity)\n    .map_err(|e| anyhow::anyhow!(\"wallet lock failed for {currency}: {e}\"))?;","preventionTips":["Keep quantity precision >= currency precision to avoid upscaling","Audit currency precision configs for very high-precision tokens","Treat astronomically large balances as data corruption and alert"],"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"}