Skip to main content

bybit_api/api/
crypto_loan.rs

1//! Crypto Loan API endpoints.
2
3use crate::client::BybitClient;
4use crate::error::Result;
5use crate::models::crypto_loan::*;
6
7impl BybitClient {
8    /// Adjust Collateral (Add or Remove)
9    pub async fn adjust_ltv(&self, params: AdjustLtvParams) -> Result<AdjustLtvResponse> {
10        self.post("/v5/crypto-loan-common/adjust-ltv", &params)
11            .await
12    }
13
14    /// Calculate Max Borrowable Amount
15    pub async fn calculate_max_loan(
16        &self,
17        params: CalculateMaxLoanParams,
18    ) -> Result<CalculateMaxLoanResponse> {
19        self.post("/v5/crypto-loan-common/max-loan", &params).await
20    }
21
22    /// Cancel Borrow Order
23    pub async fn cancel_fixed_borrow_order(
24        &self,
25        params: CancelFixedBorrowOrderParams,
26    ) -> Result<CancelFixedBorrowOrderResponse> {
27        self.post("/v5/crypto-loan-fixed/borrow-order-cancel", &params)
28            .await
29    }
30
31    /// Create Fixed-Term Borrow Order
32    pub async fn create_fixed_borrow(
33        &self,
34        params: CreateFixedBorrowParams,
35    ) -> Result<CreateFixedBorrowResponse> {
36        self.post("/v5/crypto-loan-fixed/borrow", &params).await
37    }
38
39    /// Fully Repay Loan
40    pub async fn fully_repay_fixed_loan(
41        &self,
42        params: FullyRepayFixedLoanParams,
43    ) -> Result<FullyRepayFixedLoanResponse> {
44        self.post("/v5/crypto-loan-fixed/fully-repay", &params)
45            .await
46    }
47
48    /// Get Collateral Adjustment History
49    pub async fn get_adjustment_history(
50        &self,
51        adjust_id: Option<i64>,
52        collateral_currency: Option<&str>,
53        limit: Option<i64>,
54        cursor: Option<i64>,
55    ) -> Result<GetAdjustmentHistoryResponse> {
56        let adjust_id_str = adjust_id.map(|v| v.to_string());
57        let limit_str = limit.map(|v| v.to_string());
58        let cursor_str = cursor.map(|v| v.to_string());
59        let mut params: Vec<(&str, &str)> = vec![];
60        if let Some(ref s) = adjust_id_str {
61            params.push(("adjustId", s.as_str()));
62        }
63        if let Some(s) = collateral_currency {
64            params.push(("collateralCurrency", s));
65        }
66        if let Some(ref s) = limit_str {
67            params.push(("limit", s.as_str()));
68        }
69        if let Some(ref s) = cursor_str {
70            params.push(("cursor", s.as_str()));
71        }
72        self.get("/v5/crypto-loan-common/adjustment-history", &params)
73            .await
74    }
75
76    /// Get Collateral Currency Data
77    pub async fn get_collateral_data(
78        &self,
79        currency: Option<&str>,
80    ) -> Result<GetCollateralDataResponse> {
81        let mut params: Vec<(&str, &str)> = vec![];
82        if let Some(s) = currency {
83            params.push(("currency", s));
84        }
85        self.get_public("/v5/crypto-loan-common/collateral-data", &params)
86            .await
87    }
88
89    /// Get Repayment History
90    // FIXME(typed-signature): falls back to `serde_json::Value` because the
91    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
92    // not auto-resolve. Replace with a typed struct in a follow-up PR.
93    pub async fn get_fixed_repayment_history(&self) -> Result<serde_json::Value> {
94        self.get("/v5/crypto-loan-fixed/repayment-history", &[])
95            .await
96    }
97
98    /// Get Supply Contract Info
99    // FIXME(typed-signature): falls back to `serde_json::Value` because the
100    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
101    // not auto-resolve. Replace with a typed struct in a follow-up PR.
102    pub async fn get_fixed_supply_contract_info(&self) -> Result<serde_json::Value> {
103        self.get("/v5/crypto-loan-fixed/supply-contract-info", &[])
104            .await
105    }
106
107    /// Get Supply Order Info
108    // FIXME(typed-signature): falls back to `serde_json::Value` because the
109    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
110    // not auto-resolve. Replace with a typed struct in a follow-up PR.
111    pub async fn get_fixed_supply_order_info(&self) -> Result<serde_json::Value> {
112        self.get("/v5/crypto-loan-fixed/supply-order-info", &[])
113            .await
114    }
115
116    /// Get Supply Market Quotes
117    pub async fn get_fixed_supply_order_quote(
118        &self,
119    ) -> Result<GetCryptoLoanFixedSupplyOrderQuoteResponse> {
120        self.get_public("/v5/crypto-loan-fixed/supply-order-quote", &[])
121            .await
122    }
123
124    /// Get Flexible Borrow History
125    pub async fn get_flexible_borrow_history(
126        &self,
127    ) -> Result<GetCryptoLoanFlexibleBorrowHistoryResponse> {
128        self.get("/v5/crypto-loan-flexible/borrow-history", &[])
129            .await
130    }
131
132    /// Get Ongoing Flexible Borrow Info
133    // FIXME(typed-signature): falls back to `serde_json::Value` because the
134    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
135    // not auto-resolve. Replace with a typed struct in a follow-up PR.
136    pub async fn get_flexible_ongoing_coin(&self) -> Result<serde_json::Value> {
137        self.get("/v5/crypto-loan-flexible/ongoing-coin", &[]).await
138    }
139
140    /// Get Flexible Repayment History
141    pub async fn get_flexible_repayment_history(
142        &self,
143    ) -> Result<GetCryptoLoanFlexibleRepaymentHistoryResponse> {
144        self.get("/v5/crypto-loan-flexible/repayment-history", &[])
145            .await
146    }
147
148    /// Get Borrow Contract Info
149    // FIXME(typed-signature): falls back to `serde_json::Value` because the
150    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
151    // not auto-resolve. Replace with a typed struct in a follow-up PR.
152    pub async fn get_fixed_borrow_contract_info(
153        &self,
154        order_id: Option<&str>,
155        loan_id: Option<&str>,
156        order_currency: Option<&str>,
157        term: Option<&str>,
158        limit: Option<i64>,
159        cursor: Option<i64>,
160    ) -> Result<serde_json::Value> {
161        let limit_str = limit.map(|v| v.to_string());
162        let cursor_str = cursor.map(|v| v.to_string());
163        let mut params: Vec<(&str, &str)> = vec![];
164        if let Some(s) = order_id {
165            params.push(("orderId", s));
166        }
167        if let Some(s) = loan_id {
168            params.push(("loanId", s));
169        }
170        if let Some(s) = order_currency {
171            params.push(("orderCurrency", s));
172        }
173        if let Some(s) = term {
174            params.push(("term", s));
175        }
176        if let Some(ref s) = limit_str {
177            params.push(("limit", s.as_str()));
178        }
179        if let Some(ref s) = cursor_str {
180            params.push(("cursor", s.as_str()));
181        }
182        self.get("/v5/crypto-loan-fixed/borrow-contract-info", &params)
183            .await
184    }
185
186    /// Get Borrow Order Info
187    // FIXME(typed-signature): falls back to `serde_json::Value` because the
188    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
189    // not auto-resolve. Replace with a typed struct in a follow-up PR.
190    pub async fn get_fixed_borrow_order_info(
191        &self,
192        order_id: Option<&str>,
193        order_currency: Option<&str>,
194        state: Option<&str>,
195        term: Option<&str>,
196        limit: Option<i64>,
197        cursor: Option<i64>,
198    ) -> Result<serde_json::Value> {
199        let limit_str = limit.map(|v| v.to_string());
200        let cursor_str = cursor.map(|v| v.to_string());
201        let mut params: Vec<(&str, &str)> = vec![];
202        if let Some(s) = order_id {
203            params.push(("orderId", s));
204        }
205        if let Some(s) = order_currency {
206            params.push(("orderCurrency", s));
207        }
208        if let Some(s) = state {
209            params.push(("state", s));
210        }
211        if let Some(s) = term {
212            params.push(("term", s));
213        }
214        if let Some(ref s) = limit_str {
215            params.push(("limit", s.as_str()));
216        }
217        if let Some(ref s) = cursor_str {
218            params.push(("cursor", s.as_str()));
219        }
220        self.get("/v5/crypto-loan-fixed/borrow-order-info", &params)
221            .await
222    }
223
224    /// Get Borrow Market Quotes
225    pub async fn get_fixed_borrow_order_quote(
226        &self,
227        order_currency: Option<&str>,
228        term: Option<&str>,
229        order_by: Option<&str>,
230        sort: Option<i64>,
231        limit: Option<i64>,
232    ) -> Result<GetFixedBorrowOrderQuoteResponse> {
233        let sort_str = sort.map(|v| v.to_string());
234        let limit_str = limit.map(|v| v.to_string());
235        let mut params: Vec<(&str, &str)> = vec![];
236        if let Some(s) = order_currency {
237            params.push(("orderCurrency", s));
238        }
239        if let Some(s) = term {
240            params.push(("term", s));
241        }
242        if let Some(s) = order_by {
243            params.push(("orderBy", s));
244        }
245        if let Some(ref s) = sort_str {
246            params.push(("sort", s.as_str()));
247        }
248        if let Some(ref s) = limit_str {
249            params.push(("limit", s.as_str()));
250        }
251        self.get_public("/v5/crypto-loan-fixed/borrow-order-quote", &params)
252            .await
253    }
254
255    /// Get Renewal Information
256    // FIXME(typed-signature): falls back to `serde_json::Value` because the
257    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
258    // not auto-resolve. Replace with a typed struct in a follow-up PR.
259    pub async fn get_fixed_renew_info(
260        &self,
261        order_id: Option<&str>,
262        order_currency: Option<&str>,
263        limit: Option<i64>,
264        cursor: Option<i64>,
265    ) -> Result<serde_json::Value> {
266        let limit_str = limit.map(|v| v.to_string());
267        let cursor_str = cursor.map(|v| v.to_string());
268        let mut params: Vec<(&str, &str)> = vec![];
269        if let Some(s) = order_id {
270            params.push(("orderId", s));
271        }
272        if let Some(s) = order_currency {
273            params.push(("orderCurrency", s));
274        }
275        if let Some(ref s) = limit_str {
276            params.push(("limit", s.as_str()));
277        }
278        if let Some(ref s) = cursor_str {
279            params.push(("cursor", s.as_str()));
280        }
281        self.get("/v5/crypto-loan-fixed/renew-info", &params).await
282    }
283
284    /// Get Crypto Loan Position
285    pub async fn get_loan_position(&self) -> Result<GetLoanPositionResponse> {
286        self.get("/v5/crypto-loan-common/position", &[]).await
287    }
288
289    /// Get Loanable Currency Data
290    pub async fn get_loanable_data(
291        &self,
292        currency: Option<&str>,
293        vip_level: Option<&str>,
294    ) -> Result<GetLoanableDataResponse> {
295        let mut params: Vec<(&str, &str)> = vec![];
296        if let Some(s) = currency {
297            params.push(("currency", s));
298        }
299        if let Some(s) = vip_level {
300            params.push(("vipLevel", s));
301        }
302        // Public endpoint per Bybit V5 docs ("Does not need authentication"):
303        // https://bybit-exchange.github.io/docs/v5/new-crypto-loan/loan-coin
304        self.get_public("/v5/crypto-loan-common/loanable-data", &params)
305            .await
306    }
307
308    /// Get Max Collateral Redeem Amount
309    pub async fn get_max_collateral_amount(
310        &self,
311        currency: &str,
312    ) -> Result<GetMaxCollateralAmountResponse> {
313        let params = vec![("currency", currency)];
314        self.get("/v5/crypto-loan-common/max-collateral-amount", &params)
315            .await
316    }
317
318    /// Repay with Collateral (Fixed)
319    pub async fn repay_fixed_collateral(
320        &self,
321        params: PostCryptoLoanFixedRepayCollateralParams,
322    ) -> Result<PostCryptoLoanFixedRepayCollateralResponse> {
323        self.post("/v5/crypto-loan-fixed/repay-collateral", &params)
324            .await
325    }
326
327    /// Create Supply Order
328    // FIXME(typed-signature): falls back to `serde_json::Value` because the
329    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
330    // not auto-resolve. Replace with a typed struct in a follow-up PR.
331    pub async fn create_fixed_supply(
332        &self,
333        params: PostCryptoLoanFixedSupplyParams,
334    ) -> Result<serde_json::Value> {
335        self.post("/v5/crypto-loan-fixed/supply", &params).await
336    }
337
338    /// Cancel Supply Order
339    pub async fn cancel_fixed_supply_order(
340        &self,
341        params: PostCryptoLoanFixedSupplyOrderCancelParams,
342    ) -> Result<PostCryptoLoanFixedSupplyOrderCancelResponse> {
343        self.post("/v5/crypto-loan-fixed/supply-order-cancel", &params)
344            .await
345    }
346
347    /// Create Flexible Borrow Order
348    pub async fn create_flexible_borrow(
349        &self,
350        params: PostCryptoLoanFlexibleBorrowParams,
351    ) -> Result<PostCryptoLoanFlexibleBorrowResponse> {
352        self.post("/v5/crypto-loan-flexible/borrow", &params).await
353    }
354
355    /// Repay Flexible Loan
356    // FIXME(typed-signature): falls back to `serde_json::Value` because the
357    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
358    // not auto-resolve. Replace with a typed struct in a follow-up PR.
359    pub async fn repay_flexible_loan(
360        &self,
361        params: PostCryptoLoanFlexibleRepayParams,
362    ) -> Result<serde_json::Value> {
363        self.post("/v5/crypto-loan-flexible/repay", &params).await
364    }
365
366    /// Repay with Collateral (Flexible)
367    pub async fn repay_flexible_collateral(
368        &self,
369        params: PostCryptoLoanFlexibleRepayCollateralParams,
370    ) -> Result<PostCryptoLoanFlexibleRepayCollateralResponse> {
371        self.post("/v5/crypto-loan-flexible/repay-collateral", &params)
372            .await
373    }
374
375    /// Renew Loan
376    // FIXME(typed-signature): falls back to `serde_json::Value` because the
377    // OpenAPI spec referenced a response/request type that gen-sdk-rust could
378    // not auto-resolve. Replace with a typed struct in a follow-up PR.
379    pub async fn renew_fixed_loan(
380        &self,
381        params: RenewFixedLoanParams,
382    ) -> Result<serde_json::Value> {
383        self.post("/v5/crypto-loan-fixed/renew", &params).await
384    }
385}