{"record":{"id":"6eece8e1ebf86dd5","repo":"nautechsystems/nautilus_trader","slug":"invalid-market-buy-price-price-must-satisfy-0","errorCode":null,"errorMessage":"invalid market-buy price {price}: must satisfy 0 < price < 1 for fee adjustment","messagePattern":"invalid market-buy price (.+?): must satisfy 0 < price < 1 for fee adjustment","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/parse.rs","lineNumber":518,"sourceCode":"/// `price` must be strictly inside `(0, 1)`. The SDK relies on its\n/// order-builder pipeline to enforce this. Because [`adjust_market_buy_amount`]\n/// is public, it repeats the precondition here.\n///\n/// # Errors\n///\n/// Returns an error if `price` is outside the open `(0, 1)` interval, or if\n/// amount or balance is non-positive, a fee input is negative, arithmetic overflows,\n/// or the adjusted amount truncates to zero.\npub fn adjust_market_buy_amount(\n    amount: Decimal,\n    user_pusd_balance: Decimal,\n    price: Decimal,\n    fee_rate: Decimal,\n    fee_exponent: Decimal,\n    builder_taker_fee_rate: Decimal,\n) -> anyhow::Result<Decimal> {\n    if price <= Decimal::ZERO || price >= Decimal::ONE {\n        anyhow::bail!(\n            \"invalid market-buy price {price}: must satisfy 0 < price < 1 for fee adjustment\",\n        );\n    }\n\n    let platform_fee_rate = fee_curve_rate(fee_rate, price, fee_exponent)?;\n\n    anyhow::ensure!(amount > Decimal::ZERO, \"market-buy amount must be positive\");\n    anyhow::ensure!(\n        user_pusd_balance > Decimal::ZERO,\n        \"market-buy balance must be positive\"\n    );\n    anyhow::ensure!(\n        builder_taker_fee_rate >= Decimal::ZERO,\n        \"builder fee rate must be non-negative\"\n    );\n    let platform_fee = amount\n        .checked_div(price)\n        .and_then(|shares| shares.checked_mul(platform_fee_rate))","sourceCodeStart":500,"sourceCodeEnd":536,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/parse.rs#L500-L536","documentation":"adjust_market_buy_amount applies Polymarket's fee curve to a market-buy amount, and that math is only defined for prices strictly between 0 and 1 (a binary-market outcome probability). The function bails out when the supplied price is zero, negative, or >= 1, since fees at such prices are meaningless or non-finite.","triggerScenarios":"Calling adjust_market_buy_amount (directly or via order building in submit-market flows) with price <= 0 or price >= 1 — typically a price derived from a stale/empty book, a percentage vs decimal unit mistake (e.g. 55 instead of 0.55), or a price of exactly 1.0 for a fully-resolved market.","commonSituations":"Unit-conversion bugs (feeding percent-scaled prices), best-ask fetched from a degenerate book, or submitting market buys on markets at the resolution boundary where the top-of-book price is 1.","solutions":["Verify the price is a Decimal in (0, 1) before calling; divide by 100 if it came from a percentage source.","Check the source of the price (book level, manual input) — reject empty or degenerate books earlier via calculate_market_price.","For markets priced at ~0 or ~1, treat the order as invalid and surface a user-facing validation error instead of calling the fee adjustment.","Add a unit test pinning boundary behavior at price=0, 1, and just inside the range."],"exampleFix":"// before\nlet price = Decimal::from(55); // percent, not decimal\nlet amount = adjust_market_buy_amount(balance, price, fee_rate, fee_exponent, builder_taker_fee_rate)?;\n// after\nlet price = Decimal::from(55) / Decimal::from(100); // 0.55\nanyhow::ensure!(price > Decimal::ZERO && price < Decimal::ONE, \"price must be in (0, 1)\");\nlet amount = adjust_market_buy_amount(balance, price, fee_rate, fee_exponent, builder_taker_fee_rate)?;","handlingStrategy":"validation","validationCode":"if price <= Decimal::ZERO || price >= Decimal::ONE {\n    return Err(format!(\"price {price} outside (0,1); check percent-vs-decimal conversion\"));\n}","typeGuard":"fn is_valid_market_price(p: &Decimal) -> bool {\n    *p > Decimal::ZERO && *p < Decimal::ONE\n}","tryCatchPattern":null,"preventionTips":["Always represent prices as decimals in (0,1), never percentages","Validate prices at the API boundary before fee math","Test boundary conditions price=0 and price=1 in unit tests","Derive prices from calculate_market_price so books are validated first"],"tags":["polymarket","price-validation","fees","decimal"],"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"}