{"record":{"id":"4c5db339c45537a4","repo":"nautechsystems/nautilus_trader","slug":"raw-wei-value-exceeds-unsigned-128-bit-range","errorCode":null,"errorMessage":"raw wei value exceeds unsigned 128-bit range","messagePattern":"raw wei value exceeds unsigned 128-bit range","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/defi/types/quantity.rs","lineNumber":39,"sourceCode":"\nimpl Quantity {\n    /// Constructs a [`Quantity`] from a raw amount expressed in wei (18-decimal fixed-point).\n    ///\n    /// The resulting [`Quantity`] will always have `precision` equal to `18`.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the supplied `raw_wei` cannot fit into an **unsigned** 128-bit integer (this\n    /// would exceed the numeric range of the internal `QuantityRaw` representation).\n    #[must_use]\n    pub fn from_wei<U>(raw_wei: U) -> Self\n    where\n        U: Into<U256>,\n    {\n        let raw_u256: U256 = raw_wei.into();\n        let raw_u128: u128 = raw_u256\n            .try_into()\n            .expect(\"raw wei value exceeds unsigned 128-bit range\");\n\n        Self::from_raw(raw_u128, 18)\n    }\n\n    /// Converts this [`Quantity`] to a wei amount (U256).\n    ///\n    /// Only valid for prices with precision 18. For other precisions convert to precision 18 first.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the quantity has precision other than 18.\n    #[must_use]\n    pub fn as_wei(&self) -> U256 {\n        assert!(\n            self.precision == 18,\n            \"Failed to convert quantity with precision {} to wei (requires precision 18)\",\n            self.precision\n        );","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/defi/types/quantity.rs#L21-L57","documentation":"Quantity::from_wei converts a raw wei U256 into u128 with try_into().expect(\"raw wei value exceeds unsigned 128-bit range\") because Quantity stores non-negative 128-bit fixed-precision values. Amounts above u128::MAX panic with this message. The library throws it since it cannot represent the quantity losslessly.","triggerScenarios":"Calling Quantity::from_wei with raw_wei > u256::MAX; typically huge token amounts from high-decimals tokens, summed balances overflowing 128 bits, or a U256 value that was sign-extended/negative before conversion.","commonSituations":"Processing ERC-20 transfers for tokens with very large supplies (e.g. meme tokens with 10^40+ units), aggregating portfolio balances, or decoding malformed calldata into a uint256 quantity.","solutions":["Check raw_wei <= U256::from(u128::MAX) before calling from_wei and reject/flag the amount otherwise.","Confirm the amount is in raw wei units, not already human-scaled (double-scaling inflates values).","Filter the token/instrument upstream if its magnitudes exceed the supported range.","Request a big-number quantity representation upstream if such values are legitimate."],"exampleFix":"// before\nlet qty = Quantity::from_wei(raw_wei); // panics if raw > u128::MAX\n// after\nlet qty = if raw_wei <= U256::from(u128::MAX) {\n    Quantity::from_wei(raw_wei)\n} else {\n    return Err(\"quantity wei exceeds u128\".to_string());\n};","handlingStrategy":"validation","validationCode":"fn qty_wei_in_range(raw_wei: U256) -> bool {\n    raw_wei <= U256::from(u128::MAX)\n}","typeGuard":"fn to_qty_raw(raw: U256) -> Option<u128> {\n    raw.try_into().ok()\n}","tryCatchPattern":null,"preventionTips":["Bound-check amounts against u128::MAX before from_wei.","Filter out instruments whose raw magnitudes exceed 128 bits.","Confirm values are raw wei, not human-scaled, before conversion."],"tags":["rust","panic","overflow","quantity","fixed-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"}