{"record":{"id":"4d262e38afd34bfb","repo":"nautechsystems/nautilus_trader","slug":"failed-to-convert-price-raw-value-e","errorCode":null,"errorMessage":"Failed to convert Price raw value: {e}","messagePattern":"Failed to convert Price raw value: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/decode.rs","lineNumber":84,"sourceCode":"///\n/// Returns an error if scaling overflows or the result exceeds [`PRICE_RAW_MAX`].\npub fn u256_to_price(amount: U256, decimals: u8) -> anyhow::Result<Price> {\n    if decimals == 18 {\n        check_raw_range(amount, U256::from(PRICE_RAW_MAX), \"Price\", \"PRICE_RAW_MAX\")?;\n        return Ok(Price::from_wei(amount));\n    }\n\n    let precision = decimals.min(FIXED_PRECISION);\n    let raw = scale_u256_to_raw(\n        amount,\n        decimals,\n        FIXED_PRECISION,\n        U256::from(PRICE_RAW_MAX),\n        \"Price\",\n        \"PRICE_RAW_MAX\",\n    )?;\n    let raw = PriceRaw::try_from(raw)\n        .map_err(|e| anyhow::anyhow!(\"Failed to convert Price raw value: {e}\"))?;\n    Ok(Price::from_raw_checked(raw, precision)?)\n}\n\nfn scale_u256_to_raw(\n    amount: U256,\n    decimals: u8,\n    fixed_precision: u8,\n    raw_max: U256,\n    type_name: &str,\n    raw_max_name: &str,\n) -> anyhow::Result<U256> {\n    let raw = if decimals < fixed_precision {\n        let scale = U256::from(10)\n            .checked_pow(U256::from(fixed_precision - decimals))\n            .ok_or_else(|| {\n                anyhow::anyhow!(\n                    \"Scale 10^{} exceeds U256 while converting {type_name}\",\n                    fixed_precision - decimals","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/decode.rs#L66-L102","documentation":"u256_to_price mirrors u256_to_quantity for Price: after verifying the U256 stays under PRICE_RAW_MAX and scaling to the fixed precision, the scaled value is converted into PriceRaw via TryFrom. A failure there is wrapped as \"Failed to convert Price raw value: {e}\".","triggerScenarios":"Converting a U256 price whose scaled raw representation overflows the PriceRaw integer type — typically a mis-scaled amount (wrong decimals) or an unreasonably huge price like raw reserve ratios with tiny denominator tokens.","commonSituations":"Quoting prices from pools with mismatched token decimals (e.g. 0-decimal tokens) producing astronomically large ratios; passing already-scaled values with decimals=0; wrong fixed precision assumptions.","solutions":["Confirm the decimals argument reflects the correct quote/base token decimals for the price computation.","Range-check the scaled value against PriceRaw::MAX (PRICE_RAW_MAX) before conversion.","Normalize the price ratio (divide before scaling) if intermediate values are enormous.","Audit upstream callers for double-scaling of reserve-derived ratios."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// precondition check before conversion\nlet scaled = amount.checked_mul(U256::exp10(FIXED_PRECISION - decimals))\n    .ok_or(\"overflow\")?;\nassert!(scaled <= U256::from(PRICE_RAW_MAX), \"exceeds PRICE_RAW_MAX\");","typeGuard":"fn fits_price_raw(v: U256) -> bool {\n    v <= U256::from(PRICE_RAW_MAX)\n}","tryCatchPattern":null,"preventionTips":["Use correct quote/base token decimals when deriving prices","Normalize large reserve ratios before scaling","Range-check against PRICE_RAW_MAX prior to conversion"],"tags":["u256","price","overflow","decoding"],"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"}