{"record":{"id":"9ae1129f525e72f3","repo":"nautechsystems/nautilus_trader","slug":"raw-wei-value-exceeds-128-bit-range-9ae112","errorCode":null,"errorMessage":"raw wei value exceeds 128-bit range","messagePattern":"raw wei value exceeds 128-bit range","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/defi/types/price.rs","lineNumber":39,"sourceCode":"\nimpl Price {\n    /// Constructs a [`Price`] from a raw amount expressed in wei (18-decimal fixed-point).\n    ///\n    /// The resulting [`Price`] will always have `precision` equal to `18`.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the supplied `raw_wei` cannot fit into a signed 128-bit integer (this would\n    /// exceed the numeric range of the internal `PriceRaw` 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 128-bit range\");\n\n        assert!(\n            raw_u128 <= i128::MAX as u128,\n            \"raw wei value exceeds signed 128-bit range\"\n        );\n\n        let raw_i128: i128 = raw_u128 as i128;\n        Self::from_raw(raw_i128, 18)\n    }\n\n    /// Converts this [`Price`] 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 price has precision other than 18 or if the raw value is negative.\n    #[must_use]","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/defi/types/price.rs#L21-L57","documentation":"Price::from_wei converts a raw wei U256 into the crate's 128-bit fixed-precision internal representation via try_into().expect(...). Any wei value larger than u128::MAX panics with this message, and values above i128::MAX are rejected by the following assert. It exists because Price cannot represent arbitrarily large 256-bit values losslessly.","triggerScenarios":"Calling Price::from_wei with raw_wei > u128::MAX — e.g. a price quoted in a token with extreme decimals, a garbage/oracle-corrupted value, or a wrongly scaled raw amount (multiplying instead of keeping raw units).","commonSituations":"Ingesting mis-scaled prices from a DEX event (raw units double-scaled), bad oracle feeds, or decoding dust/uint256 fields from contracts where the value is not actually a price.","solutions":["Guard before calling: ensure raw_wei <= U256::from(i128::MAX) before from_wei; otherwise reject the tick/update.","Re-check the scaling math that produced the raw wei (off-by-10^decimals scaling is the usual culprit).","Sanity-clamp incoming prices to a plausible band for the instrument before conversion.","If real prices can exceed 128 bits, use a string/decimal representation rather than from_wei."],"exampleFix":"// before\nlet price = Price::from_wei(raw_wei, 18); // panics on overflow\n// after\nassert!(raw_wei <= U256::from(i128::MAX), \"price wei out of range\");\nlet price = Price::from_wei(raw_wei, 18);","handlingStrategy":"validation","validationCode":"fn price_wei_in_range(raw_wei: U256) -> bool {\n    raw_wei <= U256::from(i128::MAX)\n}","typeGuard":"fn to_price_raw(raw: U256) -> Option<u128> {\n    let r: u128 = raw.try_into().ok()?;\n    (r <= i128::MAX as u128).then_some(r)\n}","tryCatchPattern":null,"preventionTips":["Clamp incoming prices to a plausible band for the instrument before conversion.","Verify scaling/decimals math when producing raw wei prices.","Never feed unvalidated uint256 oracle values directly into Price::from_wei."],"tags":["rust","panic","overflow","price","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-14T00:17:10.932Z"}