{"record":{"id":"77861cf8fa17c4c5","repo":"nautechsystems/nautilus_trader","slug":"bybit-repay-for-coin-returned-result-status-sta-77861c","errorCode":null,"errorMessage":"Bybit repay for {coin} returned result status {status}","messagePattern":"Bybit repay for (.+?) returned result status (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/bybit/src/http/client.rs","lineNumber":2406,"sourceCode":"    pub async fn repay_spot_borrow_with_conversion(\n        &self,\n        coin: &str,\n        amount: Option<Quantity>,\n    ) -> anyhow::Result<BybitRepayResponse> {\n        let amount_str = amount.as_ref().map(|q| q.to_string());\n        let response = self\n            .inner\n            .repay(Some(coin), amount_str.as_deref())\n            .await\n            .map_err(|e| {\n                anyhow::anyhow!(\"Failed to repay spot borrow (with conversion) for {coin}: {e}\")\n            })?;\n        Self::ensure_repay_accepted(coin, response.result.result_status)?;\n        Ok(response)\n    }\n\n    fn ensure_repay_accepted(coin: &str, status: BybitRepayStatus) -> anyhow::Result<()> {\n        anyhow::ensure!(\n            status != BybitRepayStatus::Failed,\n            \"Bybit repay for {coin} returned result status {status}\"\n        );\n        Ok(())\n    }\n\n    /// Generate SPOT position reports from wallet balances.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if:\n    /// - The wallet balance request fails.\n    /// - Parsing fails.\n    async fn generate_spot_position_reports_from_wallet(\n        &self,\n        account_id: AccountId,\n        instrument_id: InstrumentId,\n    ) -> anyhow::Result<Vec<PositionStatusReport>> {","sourceCodeStart":2388,"sourceCodeEnd":2424,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/bybit/src/http/client.rs#L2388-L2424","documentation":"After a repay request succeeds at the HTTP level, BybitHttpClient::ensure_repay_accepted checks response.result.result_status and fails with anyhow::ensure! if it equals BybitRepayStatus::Failed. Bybit accepts the request asynchronously and reports execution outcome in this status, so a 'Failed' status means the repay did not execute even though the API call returned 200.","triggerScenarios":"Repaying a spot borrow where Bybit rejected the repay operation (e.g. borrow already repaid, insufficient assets after conversion, invalid amount), yielding result_status=Failed inside an otherwise successful response.","commonSituations":"Racing repay calls where the first already cleared the liability; automated cleanup that repays more than outstanding; convert-repay attempts when no assets are convertible.","solutions":["Query the current borrow/liability state before repaying and repay only the outstanding amount","Treat Failed status as final: do not blind-retry the same amount; re-check balance and rebuild the repay request","Check Bybit's account borrow records endpoint for the reason the repay failed","Idempotency: track in-flight repay operations to avoid duplicate submissions"],"exampleFix":"// before\nclient.no_convert_repay(coin, Some(\"100\".into())).await?; // may return Failed status\n// after\nlet outstanding = client.get_borrow_amount(coin).await?;\nif outstanding > 0 {\n    client.no_convert_repay(coin, Some(outstanding.to_string().into())).await?;\n}","handlingStrategy":"try-catch","validationCode":"let outstanding = client.get_outstanding_borrow(coin).await?;\nif outstanding == 0 { return Ok(()); } // repay would return Failed status","typeGuard":null,"tryCatchPattern":"let resp = client.no_convert_repay(coin, amt).await?;\nif resp.result.result_status == BybitRepayStatus::Failed {\n    // re-query borrow records and rebuild request; do not blind-retry\n}","preventionTips":["Make repay operations idempotent (track in-flight attempts)","Re-query liability after any Failed status","Avoid concurrent repay calls for the same coin"],"tags":["rust","bybit","margin","async-response"],"backgroundTag":"api-error-response","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"}