{"record":{"id":"d990b3d6e2504438","repo":"nautechsystems/nautilus_trader","slug":"order-quantity-quantity-is-not-exactly-represent","errorCode":null,"errorMessage":"Order quantity {quantity} is not exactly representable in {decimals} base token decimals","messagePattern":"Order quantity (.+?) is not exactly representable in (.+?) base token decimals","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/execution/client.rs","lineNumber":5220,"sourceCode":"    if quantity.is_zero() {\n        anyhow::bail!(\"Order quantity must be positive\");\n    }\n\n    let raw = U256::from(quantity.raw);\n    let raw_precision = quantity.precision.max(FIXED_PRECISION);\n    if decimals >= raw_precision {\n        let scale = U256::from(10u64)\n            .checked_pow(U256::from(decimals - raw_precision))\n            .ok_or_else(|| anyhow::anyhow!(\"Order amount scaling overflow\"))?;\n        raw.checked_mul(scale).ok_or_else(|| {\n            anyhow::anyhow!(\"Order amount overflow scaling quantity to raw token units\")\n        })\n    } else {\n        let divisor = U256::from(10u64)\n            .checked_pow(U256::from(raw_precision - decimals))\n            .ok_or_else(|| anyhow::anyhow!(\"Order amount scaling overflow\"))?;\n        if !(raw % divisor).is_zero() {\n            anyhow::bail!(\n                \"Order quantity {quantity} is not exactly representable in {decimals} base token decimals\"\n            );\n        }\n        Ok(raw / divisor)\n    }\n}\n\nfn swap_token_pair(\n    side: OrderSide,\n    base: Address,\n    quote: Address,\n) -> anyhow::Result<(Address, Address)> {\n    match side {\n        OrderSide::Sell => Ok((base, quote)),\n        OrderSide::Buy => Ok((quote, base)),\n    }\n}\n","sourceCodeStart":5202,"sourceCodeEnd":5238,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/execution/client.rs#L5202-L5238","documentation":"When the quantity's raw precision exceeds the token's decimals, the raw amount must be divisible by 10^(raw_precision - decimals) to map exactly onto token units. If not, the conversion would require rounding, which is rejected to preserve exact discrete values, and this error names the offending quantity and decimals.","triggerScenarios":"Converting an order Quantity whose raw value has sub-base-unit precision (e.g. raw precision 9 with a token using 6 decimals) where quantity.raw is not divisible by 10^(raw_precision - decimals) — e.g. quantity 1.0000005 on a 6-decimal token.","commonSituations":"Instrument price/size precision configured finer than the token's decimals; quantities generated from USD-notional math at higher precision; a mismatch between the instrument definition's precision and the actual token contract decimals.","solutions":["Round the order quantity down to a value representable in the token's decimals before conversion","Correct the instrument definition so its size precision does not exceed the token's decimals","Fix upstream sizing math to quantize to the token's decimal grid","Verify the token decimals value passed in matches the actual contract"],"exampleFix":"// before: passing raw computed size\nlet qty = Quantity::new(notional / price, 9);\nlet raw = quantity_to_raw_amount(qty, decimals)?;\n// after: quantize to token decimals first\nlet step = 10f64.powi(-(decimals as i32));\nlet qty = Quantity::new((notional / price).floor_to(step), decimals as u8);\nlet raw = quantity_to_raw_amount(qty, decimals)?;","handlingStrategy":"validation","validationCode":"fn is_representable(raw: &U256, raw_precision: u8, decimals: u8) -> bool {\n    if decimals >= raw_precision { return true; }\n    let div = U256::from(10u64).pow(U256::from(raw_precision - decimals));\n    (raw % div).is_zero()\n}","typeGuard":null,"tryCatchPattern":"match res {\n    Err(e) if e.to_string().contains(\"not exactly representable\") => {\n        let qty = quantize_to_decimals(quantity, decimals);\n        quantity_to_raw_amount(qty, decimals)?\n    }\n    other => other?,\n}","preventionTips":["Configure instrument size precision no finer than the token's decimals","Quantize quantities to the token decimal grid before conversion","Keep precision definitions in sync with the actual token contract","Test conversions across precision/decimal combinations"],"tags":["quantity","precision","decimals","validation"],"backgroundTag":"invalid-argument-value","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"}