{"record":{"id":"f9c58ec5968c14e0","repo":"nautechsystems/nautilus_trader","slug":"failed-to-convert-quantity-to-u64","errorCode":null,"errorMessage":"Failed to convert quantity to u64","messagePattern":"Failed to convert quantity to u64","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/dydx/src/grpc/order.rs","lineNumber":159,"sourceCode":"    ///\n    /// Returns an error if conversion fails.\n    pub fn quantize_quantity(&self, quantity: Decimal) -> Result<u64, anyhow::Error> {\n        // When atomic_resolution is negative, we multiply by 10^|atomic_resolution|\n        // When atomic_resolution is positive, we divide by 10^atomic_resolution\n        let factor = if self.atomic_resolution < 0 {\n            Decimal::from(10_i64.pow(self.atomic_resolution.unsigned_abs()))\n        } else {\n            Decimal::new(1, self.atomic_resolution.unsigned_abs())\n        };\n\n        let raw_quantums = quantity * factor;\n        let step_base_quantums = Decimal::from(self.step_base_quantums);\n        let quantums = Self::quantize(&raw_quantums, &step_base_quantums);\n        let result = quantums.max(step_base_quantums);\n\n        result\n            .to_u64()\n            .ok_or_else(|| anyhow::anyhow!(\"Failed to convert quantity to u64\"))\n    }\n\n    /// A `round`-like function that quantizes a `value` to the `fraction`.\n    fn quantize(value: &Decimal, fraction: &Decimal) -> Decimal {\n        (value / fraction).round() * fraction\n    }\n\n    /// Compute worst-case subticks for a market order using oracle price + slippage.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if oracle price is not available or conversion fails.\n    pub fn market_order_subticks(&self, side: OrderSide) -> Result<u64, anyhow::Error> {\n        let oracle = self\n            .oracle_price\n            .ok_or_else(|| anyhow::anyhow!(\"Oracle price required for market orders\"))?;\n        let worst_price = match side {\n            OrderSide::Buy => oracle * (Decimal::ONE + DEFAULT_MARKET_ORDER_SLIPPAGE),","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/dydx/src/grpc/order.rs#L141-L177","documentation":"quantize_quantity rounds an order size to step_base_quantums and converts the result to u64 for the proto Order. The error fires when the quantized Decimal cannot be represented as u64 — typically a negative size or a size so large it overflows u64::MAX.","triggerScenarios":"Calling quantize_quantity (directly or via OrderBuilder::build) with a negative quantity, a quantity larger than u64::MAX / step_base_quantums, or a market whose step_base_quantums is misconfigured so quantums.max(step_base_quantums) is unrepresentable.","commonSituations":"Sizing logic that can produce negative sizes (e.g. closing more than a position holds); wrong unit scaling of size (contracts vs base currency); corrupted or wrong market step_base_quantums; test/strategy code passing raw floats converted badly to Decimal.","solutions":["Assert quantity > 0 before building the order","Check that step_base_quantums for the market matches the dYdX market configuration","Verify the size units: ensure you pass size in the base currency the adapter expects","Clamp or reject oversized sizes upstream before calling the builder"],"exampleFix":"// before\nlet order = OrderBuilder::new(params).size(size)...build()?;\n// after\nanyhow::ensure!(size > Decimal::ZERO, \"order size must be positive\");\nanyhow::ensure!(size < Decimal::from(u64::MAX), \"order size too large\");\nlet order = OrderBuilder::new(params).size(size)...build()?;","handlingStrategy":"validation","validationCode":"anyhow::ensure!(size.is_sign_positive(), \"size must be positive\");\nanyhow::ensure!(size <= Decimal::from(u64::MAX), \"size exceeds u64 range\");","typeGuard":"fn is_valid_order_size(d: &Decimal) -> bool {\n    d.is_sign_positive() && d <= Decimal::from(u64::MAX)\n}","tryCatchPattern":null,"preventionTips":["Never allow sizing logic to emit zero or negative quantities","Confirm step_base_quantums per market from exchange metadata","Keep size unit conversions centralized in one place"],"tags":["rust","decimal-conversion","overflow","dydx"],"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"}