{"record":{"id":"39f487611bad787c","repo":"nautechsystems/nautilus_trader","slug":"inverted-price-exceeds-u256-range","errorCode":null,"errorMessage":"Inverted price exceeds U256 range","messagePattern":"Inverted price exceeds U256 range","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/defi/tick_map/sqrt_price_math.rs","lineNumber":426,"sourceCode":"                        .checked_mul(decimal_adjustment)\n                        .ok_or_else(|| {\n                            anyhow::anyhow!(\"Inverted price denominator exceeds U256 range\")\n                        })?;\n                FullMath::mul_div(numerator, U256::from(1), denominator)?\n            }\n        } else {\n            let price_square: U512 = sqrt_price.widening_mul(sqrt_price);\n            anyhow::ensure!(\n                !price_square.is_zero(),\n                \"Cannot decode inverted price from zero sqrt_price_x96\"\n            );\n            let numerator = U512::from(divisor_base)\n                .checked_mul(U512::from(decimal_adjustment))\n                .and_then(|value| value.checked_mul(U512::from(fixed_scalar)))\n                .ok_or_else(|| anyhow::anyhow!(\"Inverted price numerator exceeds U512 range\"))?;\n            let quotient = numerator / price_square;\n            U256::checked_from_limbs_slice(quotient.as_limbs())\n                .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!(","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/defi/tick_map/sqrt_price_math.rs#L408-L444","documentation":"After dividing numerator by price_square in the inverted branch (decimal_diff < 0), the resulting quotient must fit into a U256 to become the raw fixed-point price. This error is thrown when the computed inverted price exceeds U256 range, indicating a price so large it cannot be represented by the library's fixed-point Price type.","triggerScenarios":"Calling decode_sqrt_price_x96_to_price_tokens_adjusted with invert=true and token0_decimals < token1_decimals when sqrt_price_x96^2 is very small relative to numerator (tiny sqrt price with a large decimal adjustment), yielding a quotient > U256::MAX.","commonSituations":"Decoding a near-zero sqrt_price_x96 for a pool with a large decimal gap (e.g. token0 with 0 decimals, token1 with 18) — the inverted price token0/token1 becomes astronomically large.","solutions":["Sanity-check sqrt_price_x96 magnitude before decoding; extremely small values with big decimal gaps produce unusable prices.","Use the non-inverted orientation (invert=false) and invert at a higher level where the small price is representable.","Clamp or reject prices exceeding PRICE_RAW_MAX at the caller after catching the error."],"exampleFix":"// before\nlet price = decode_sqrt_price_x96_to_price_tokens_adjusted(sqrt_price_x96, 0, 18, true)?;\n// after\nlet price = match decode_sqrt_price_x96_to_price_tokens_adjusted(sqrt_price_x96, 0, 18, true) {\n    Ok(p) => p,\n    Err(_) => return Ok(Price::max(FIXED_PRECISION)), // or skip pool\n};","handlingStrategy":"fallback","validationCode":"fn quotient_may_exceed_u256(sqrt_price_x96: U160, d0: u8, d1: u8) -> bool {\n    // tiny sqrt price with large decimal gap can overflow the inverted quotient\n    sqrt_price_x96 < U160::from(1000u32) && i32::from(d1) > i32::from(d0)\n}","typeGuard":null,"tryCatchPattern":"let price = decode_sqrt_price_x96_to_price_tokens_adjusted(sp, d0, d1, true)\n    .ok()\n    .unwrap_or_else(|| Price::max(FIXED_PRECISION)); // saturate instead of failing","preventionTips":["Guard against near-zero sqrt prices when decimal_diff < 0","Clamp decoded prices to PRICE_RAW_MAX at the application boundary","Skip pools whose decoded price exceeds representable range in pool-screening logic"],"tags":["rust","overflow","defi","uniswap-v3"],"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"}