{"record":{"id":"e87c5327c63c14c8","repo":"nautechsystems/nautilus_trader","slug":"price-overflow-price-raw-exceeds-maximum-valid","errorCode":null,"errorMessage":"Price overflow: {price_raw} exceeds maximum valid raw price {PRICE_RAW_MAX}","messagePattern":"Price overflow: (.+?) exceeds maximum valid raw price (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/defi/tick_map/sqrt_price_math.rs","lineNumber":444,"sourceCode":"                .ok_or_else(|| anyhow::anyhow!(\"Inverted price exceeds U256 range\"))?\n        }\n    } else if decimal_diff >= 0 {\n        FullMath::mul_div_scaled(\n            sqrt_price,\n            sqrt_price,\n            divisor_base,\n            &[fixed_scalar, decimal_adjustment],\n        )?\n    } else {\n        FullMath::mul_div_scaled(sqrt_price, sqrt_price, divisor_base, &[fixed_scalar])?\n            / decimal_adjustment\n    };\n\n    price_from_u256(price_raw)\n}\n\npub(crate) fn price_from_u256(price_raw: U256) -> anyhow::Result<Price> {\n    anyhow::ensure!(\n        price_raw <= U256::from(PRICE_RAW_MAX as u128),\n        \"Price overflow: {price_raw} exceeds maximum valid raw price {PRICE_RAW_MAX}\"\n    );\n    let price_raw: i128 = price_raw\n        .try_into()\n        .map_err(|_| anyhow::anyhow!(\"Price overflow: {price_raw} exceeds PriceRaw range\"))?;\n\n    Price::from_raw_checked(price_raw, FIXED_PRECISION).map_err(Into::into)\n}\n\n#[cfg(test)]\nmod tests {\n    // Most of the tests are based on https://github.com/Uniswap/v3-core/blob/main/test/SqrtPriceMath.spec.ts\n    use rstest::*;\n\n    use super::*;\n    use crate::defi::tick_map::{\n        full_math::{DECIMAL_EXPONENT_MAX, Q96_U160},","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/defi/tick_map/sqrt_price_math.rs#L426-L462","documentation":"price_from_u256 converts the decoded raw fixed-point price (U256) into the library's Price type. It first checks the value against PRICE_RAW_MAX; the decoded price exceeded that maximum, so it cannot be represented as a Price with FIXED_PRECISION. The library throws this rather than silently saturating, to preserve exactness of price values.","triggerScenarios":"Any decode path (decode_sqrt_price_x96_to_price or decode_sqrt_price_x96_to_price_tokens_adjusted, called by execution_price/spot_price) where the computed price_raw exceeds PRICE_RAW_MAX (i.e. does not fit i128), typically with extreme sqrt_price_x96 values or huge decimal adjustments.","commonSituations":"Decoding slot0 prices for pools with extreme prices (nearly all one token); misconfigured token decimals inflating the decimal adjustment; corrupted or malicious on-chain sqrt price data.","solutions":["Verify token decimals are correct for the pair; wrong decimals inflate the decoded price.","Check the sqrt_price_x96 source data for corruption; extreme prices may indicate a misconfigured or malicious pool.","Clamp at the caller: catch the error and cap the price at the maximum representable value, or skip the pool.","Use a wider custom representation if your application genuinely needs prices beyond PRICE_RAW_MAX."],"exampleFix":"// before\nlet price = decode_sqrt_price_x96_to_price(sqrt_price_x96)?;\n// after\nlet price = match decode_sqrt_price_x96_to_price(sqrt_price_x96) {\n    Ok(p) => p,\n    Err(e) if e.to_string().contains(\"Price overflow\") => {\n        // pool price beyond representable range; treat as extreme\n        return Ok(Price::max(FIXED_PRECISION));\n    }\n    Err(e) => return Err(e),\n};","handlingStrategy":"try-catch","validationCode":"fn raw_price_fits(price_raw: U256) -> bool {\n    price_raw <= U256::from(PRICE_RAW_MAX as u128)\n}","typeGuard":null,"tryCatchPattern":"match decode_sqrt_price_x96_to_price(sp) {\n    Ok(p) => p,\n    Err(e) if e.to_string().contains(\"exceeds maximum valid raw price\") => {\n        tracing::warn!(\"pool price beyond representable range\");\n        Price::max(FIXED_PRECISION)\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Verify token decimals before decoding — the top cause of price overflow","Treat extreme decoded prices as a data-quality signal for the pool","Saturate to Price::max/zero at the application boundary instead of propagating"],"tags":["rust","overflow","price","defi"],"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"}