{"record":{"id":"a729a907a5ac2ce8","repo":"nautechsystems/nautilus_trader","slug":"market-buy-balance-must-be-positive","errorCode":null,"errorMessage":"market-buy balance must be positive","messagePattern":"market-buy balance must be positive","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/parse.rs","lineNumber":526,"sourceCode":"/// 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))\n        .context(\"market-buy platform fee overflow\")?;\n    let builder_fee = amount\n        .checked_mul(builder_taker_fee_rate)\n        .context(\"market-buy builder fee overflow\")?;\n    let total_cost = amount\n        .checked_add(platform_fee)\n        .and_then(|cost| cost.checked_add(builder_fee))\n        .context(\"market-buy total cost overflow\")?;","sourceCodeStart":508,"sourceCodeEnd":544,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/parse.rs#L508-L544","documentation":"adjust_market_buy_amount caps the fee-adjusted BUY amount by the user's pUSD balance, so the balance must be strictly positive. A zero or negative balance is rejected with \"market-buy balance must be positive\".","triggerScenarios":"Calling adjust_market_buy_amount with user_pusd_balance <= 0 — typically when the wallet reports no collateral, balance fetch failed/returned zero, or the balance was queried before funding/deposits settled.","commonSituations":"Fresh account with no USDC deposited; balance cache populated before the deposit confirmed; wrong wallet address configured so the fetched balance is zero.","solutions":["Check the account's actual pUSD/USDC balance on the venue and deposit funds if it is zero.","Verify the wallet address used to fetch the balance is the funded trading account.","In the caller, skip order submission when balance <= 0 instead of invoking the adjustment."],"exampleFix":"// before\nadjust_market_buy_amount(amount, price, Decimal::ZERO /* balance */)?;\n// after\nif user_pusd_balance > Decimal::ZERO {\n    adjust_market_buy_amount(amount, price, user_pusd_balance)?;\n}","handlingStrategy":"validation","validationCode":"let balance = fetch_pusd_balance(wallet).await?;\nif balance <= Decimal::ZERO { tracing::warn!(\"no collateral; skipping market buy\"); return Ok(()); }","typeGuard":"fn has_collateral(balance: Decimal) -> bool { balance > Decimal::ZERO }","tryCatchPattern":"match adjust_market_buy_amount(amount, price, balance, ...) {\n    Err(e) if e.to_string().contains(\"balance must be positive\") => {\n        tracing::warn!(\"insufficient/zero pUSD balance — deposit or fix wallet config\");\n    }\n    other => other?,\n}","preventionTips":["Check wallet balance before sizing market buys.","Confirm the configured wallet address is the funded trading account.","Refresh balance after deposits and handle pending-transaction balances."],"tags":["polymarket","market-order","balance","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"}