1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use super::Kalshi;
use crate::kalshi_error::*;
use crate::Side;
use serde::{Deserialize, Serialize};
impl Kalshi {
// ========== Task 2.3: get_communications_id() ==========
/// Retrieves the user's public communications ID.
///
/// This ID is used to identify the user in communications with other traders.
///
/// # Returns
///
/// - `Ok(String)`: The user's communications ID on successful retrieval.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let comm_id = kalshi_instance.get_communications_id().await.unwrap();
/// println!("Communications ID: {}", comm_id);
/// ```
///
pub async fn get_communications_id(&self) -> Result<String, KalshiError> {
let path = "/communications/id";
let res: CommunicationsIdResponse = self.signed_get(path).await?;
Ok(res.communications_id)
}
/// Retrieves a communication by ID.
///
/// This method fetches a specific communication message or thread.
///
/// # Arguments
///
/// * `comm_id` - The communication ID to retrieve.
///
/// # Returns
///
/// - `Ok(Communication)`: The communication details on successful retrieval.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let comm = kalshi_instance.get_communication("comm-123").await.unwrap();
/// ```
///
pub async fn get_communication(&self, comm_id: &str) -> Result<Communication, KalshiError> {
let path = format!("/communications/{}", comm_id);
self.signed_get(&path).await
}
// ========== Task 3.3: get_rfqs() with pagination ==========
/// Retrieves RFQs (Requests for Quote) with optional filtering and pagination.
///
/// This method lists RFQs that the user has created or received, with support
/// for filtering by various parameters and pagination.
///
/// # Arguments
///
/// * `cursor` - Pagination cursor from previous request.
/// * `event_ticker` - Filter by event ticker.
/// * `market_ticker` - Filter by market ticker.
/// * `limit` - Number of results per page (default 100, max 100).
/// * `status` - Filter by RFQ status.
/// * `creator_user_id` - Filter by creator user ID.
///
/// # Returns
///
/// - `Ok((Option<String>, Vec<Rfq>))`: A tuple containing the next cursor (if any) and a vector of RFQs.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let (cursor, rfqs) = kalshi_instance.get_rfqs(
/// None, // cursor
/// None, // event_ticker
/// None, // market_ticker
/// Some(10), // limit
/// None, // status
/// None, // creator_user_id
/// ).await.unwrap();
/// ```
///
pub async fn get_rfqs(
&self,
cursor: Option<String>,
event_ticker: Option<String>,
market_ticker: Option<String>,
limit: Option<i32>,
status: Option<String>,
creator_user_id: Option<String>,
) -> Result<(Option<String>, Vec<Rfq>), KalshiError> {
let mut params: Vec<(&str, String)> = Vec::with_capacity(6);
add_param!(params, "cursor", cursor);
add_param!(params, "event_ticker", event_ticker);
add_param!(params, "market_ticker", market_ticker);
add_param!(params, "limit", limit);
add_param!(params, "status", status);
add_param!(params, "creator_user_id", creator_user_id);
let path = if params.is_empty() {
"/communications/rfqs".to_string()
} else {
let qs = params
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join("&");
format!("/communications/rfqs?{}", qs)
};
let res: RfqsResponse = self.signed_get(&path).await?;
Ok((res.cursor, res.rfqs))
}
// ========== Task 3.1: create_rfq() ==========
/// Creates a new RFQ (Request for Quote).
///
/// This method submits a new request for quote to market makers or other traders.
///
/// # Arguments
///
/// * `market_ticker` - The market ticker to request a quote for.
/// * `rest_remainder` - Whether to rest the remainder after execution.
/// * `contracts` - Number of contracts for the RFQ (optional).
/// * `target_cost_centi_cents` - Target cost in centi-cents (optional).
/// * `replace_existing` - Whether to delete existing RFQs (default: false).
/// * `subtrader_id` - Subtrader ID (FCM members only).
///
/// # Returns
///
/// - `Ok(CreateRfqResponse)`: The created RFQ response containing the new RFQ ID.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let response = kalshi_instance.create_rfq(
/// "MARKET-TICKER",
/// false,
/// Some(100),
/// None,
/// None,
/// None,
/// ).await.unwrap();
/// println!("Created RFQ with ID: {}", response.id);
/// ```
///
pub async fn create_rfq(
&self,
market_ticker: &str,
rest_remainder: bool,
contracts: Option<i32>,
target_cost_centi_cents: Option<i64>,
replace_existing: Option<bool>,
subtrader_id: Option<String>,
) -> Result<CreateRfqResponse, KalshiError> {
let path = "/communications/rfqs";
let body = CreateRfqRequest {
market_ticker: market_ticker.to_string(),
rest_remainder,
contracts,
target_cost_centi_cents,
replace_existing,
subtrader_id,
};
self.signed_post(path, &body).await
}
/// Retrieves a specific RFQ by ID.
///
/// This method fetches detailed information about a specific RFQ.
///
/// # Arguments
///
/// * `rfq_id` - The RFQ ID to retrieve.
///
/// # Returns
///
/// - `Ok(Rfq)`: The RFQ details on successful retrieval.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let rfq = kalshi_instance.get_rfq("rfq-123").await.unwrap();
/// ```
///
pub async fn get_rfq(&self, rfq_id: &str) -> Result<Rfq, KalshiError> {
let path = format!("/communications/rfqs/{}", rfq_id);
let res: RfqResponse = self.signed_get(&path).await?;
Ok(res.rfq)
}
/// Deletes an RFQ.
///
/// This method cancels and removes an RFQ. Only the creator can delete an RFQ.
///
/// # Arguments
///
/// * `rfq_id` - The RFQ ID to delete.
///
/// # Returns
///
/// - `Ok(())`: Success confirmation.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// kalshi_instance.delete_rfq("rfq-123").await.unwrap();
/// ```
///
pub async fn delete_rfq(&self, rfq_id: &str) -> Result<(), KalshiError> {
let path = format!("/communications/rfqs/{}", rfq_id);
let _res: DeleteRfqResponse = self.signed_delete(&path).await?;
Ok(())
}
// ========== Task 3.3: get_quotes() with pagination ==========
/// Retrieves quotes with optional filtering and pagination.
///
/// This method lists quotes that the user has created or received, with support
/// for filtering by various parameters and pagination.
///
/// # Arguments
///
/// * `cursor` - Pagination cursor from previous request.
/// * `event_ticker` - Filter by event ticker.
/// * `market_ticker` - Filter by market ticker.
/// * `limit` - Number of results per page (default 500, max 500).
/// * `status` - Filter by quote status.
/// * `quote_creator_user_id` - Filter by quote creator user ID.
/// * `rfq_creator_user_id` - Filter by RFQ creator user ID.
/// * `rfq_id` - Filter by RFQ ID.
///
/// # Returns
///
/// - `Ok((Option<String>, Vec<Quote>))`: A tuple containing the next cursor (if any) and a vector of quotes.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let (cursor, quotes) = kalshi_instance.get_quotes(
/// None, // cursor
/// None, // event_ticker
/// None, // market_ticker
/// Some(10), // limit
/// None, // status
/// None, // quote_creator_user_id
/// None, // rfq_creator_user_id
/// None, // rfq_id
/// ).await.unwrap();
/// ```
///
#[allow(clippy::too_many_arguments)]
pub async fn get_quotes(
&self,
cursor: Option<String>,
event_ticker: Option<String>,
market_ticker: Option<String>,
limit: Option<i32>,
status: Option<String>,
quote_creator_user_id: Option<String>,
rfq_creator_user_id: Option<String>,
rfq_id: Option<String>,
) -> Result<(Option<String>, Vec<Quote>), KalshiError> {
let mut params: Vec<(&str, String)> = Vec::with_capacity(8);
add_param!(params, "cursor", cursor);
add_param!(params, "event_ticker", event_ticker);
add_param!(params, "market_ticker", market_ticker);
add_param!(params, "limit", limit);
add_param!(params, "status", status);
add_param!(params, "quote_creator_user_id", quote_creator_user_id);
add_param!(params, "rfq_creator_user_id", rfq_creator_user_id);
add_param!(params, "rfq_id", rfq_id);
let path = if params.is_empty() {
"/communications/quotes".to_string()
} else {
let qs = params
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join("&");
format!("/communications/quotes?{}", qs)
};
let res: QuotesResponse = self.signed_get(&path).await?;
Ok((res.cursor, res.quotes))
}
// ========== Task 3.2: create_quote() ==========
/// Creates a new quote in response to an RFQ.
///
/// This method submits a quote offer to an RFQ requestor.
///
/// # Arguments
///
/// * `rfq_id` - The RFQ ID this quote responds to.
/// * `yes_bid` - Bid price for YES contracts in dollars ("0.5600" format).
/// * `no_bid` - Bid price for NO contracts in dollars ("0.5600" format).
/// * `rest_remainder` - Whether to rest the remainder after execution.
///
/// # Returns
///
/// - `Ok(CreateQuoteResponse)`: The created quote response containing the new quote ID.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let response = kalshi_instance.create_quote(
/// "rfq-123",
/// "0.5000",
/// "0.5000",
/// false,
/// ).await.unwrap();
/// println!("Created quote with ID: {}", response.id);
/// ```
///
pub async fn create_quote(
&self,
rfq_id: &str,
yes_bid: &str,
no_bid: &str,
rest_remainder: bool,
) -> Result<CreateQuoteResponse, KalshiError> {
let path = "/communications/quotes";
let body = CreateQuoteRequest {
rfq_id: rfq_id.to_string(),
yes_bid: yes_bid.to_string(),
no_bid: no_bid.to_string(),
rest_remainder,
};
self.signed_post(path, &body).await
}
/// Retrieves a specific quote by ID.
///
/// This method fetches detailed information about a specific quote.
///
/// # Arguments
///
/// * `quote_id` - The quote ID to retrieve.
///
/// # Returns
///
/// - `Ok(Quote)`: The quote details on successful retrieval.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let quote = kalshi_instance.get_quote("quote-123").await.unwrap();
/// ```
///
pub async fn get_quote(&self, quote_id: &str) -> Result<Quote, KalshiError> {
let path = format!("/communications/quotes/{}", quote_id);
let res: QuoteResponse = self.signed_get(&path).await?;
Ok(res.quote)
}
/// Deletes a quote.
///
/// This method cancels and removes a quote. Only the creator can delete a quote.
///
/// # Arguments
///
/// * `quote_id` - The quote ID to delete.
///
/// # Returns
///
/// - `Ok(())`: Success confirmation.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// kalshi_instance.delete_quote("quote-123").await.unwrap();
/// ```
///
pub async fn delete_quote(&self, quote_id: &str) -> Result<(), KalshiError> {
let path = format!("/communications/quotes/{}", quote_id);
let _res: DeleteQuoteResponse = self.signed_delete(&path).await?;
Ok(())
}
// ========== Task 3.4: accept_quote() with accepted_side ==========
/// Accepts a quote.
///
/// This method accepts a quote offer, which will execute the trade.
///
/// # Arguments
///
/// * `quote_id` - The quote ID to accept.
/// * `accepted_side` - Which side to accept (Yes or No).
///
/// # Returns
///
/// - `Ok(())`: Success confirmation (API returns 204 No Content).
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// use kalshi::Side;
///
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// kalshi_instance.accept_quote("quote-123", Side::Yes).await.unwrap();
/// ```
///
pub async fn accept_quote(
&self,
quote_id: &str,
accepted_side: Side,
) -> Result<(), KalshiError> {
let path = format!("/communications/quotes/{}/accept", quote_id);
let body = AcceptQuoteRequest { accepted_side };
let _: serde_json::Value = self.signed_put(&path, Some(&body)).await?;
Ok(())
}
/// Confirms a quote.
///
/// This method confirms a quote after acceptance, finalizing the transaction.
///
/// # Arguments
///
/// * `quote_id` - The quote ID to confirm.
///
/// # Returns
///
/// - `Ok(QuoteConfirmed)`: The confirmed quote details on successful confirmation.
/// - `Err(KalshiError)`: An error if there is an issue with the request.
///
/// # Example
///
/// ```
/// // Assuming `kalshi_instance` is an instance of `Kalshi`
/// let result = kalshi_instance.confirm_quote("quote-123").await.unwrap();
/// ```
///
pub async fn confirm_quote(&self, quote_id: &str) -> Result<QuoteConfirmed, KalshiError> {
let path = format!("/communications/quotes/{}/confirm", quote_id);
self.signed_put(&path, None::<&()>).await
}
}
// -------- Request bodies --------
#[derive(Debug, Deserialize)]
struct CommunicationsIdResponse {
communications_id: String,
}
#[derive(Debug, Serialize)]
struct CreateRfqRequest {
market_ticker: String,
rest_remainder: bool,
#[serde(skip_serializing_if = "Option::is_none")]
contracts: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
target_cost_centi_cents: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
replace_existing: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
subtrader_id: Option<String>,
}
#[derive(Debug, Serialize)]
struct CreateQuoteRequest {
rfq_id: String,
yes_bid: String,
no_bid: String,
rest_remainder: bool,
}
#[derive(Debug, Serialize)]
struct AcceptQuoteRequest {
accepted_side: Side,
}
// -------- Response wrappers --------
#[derive(Debug, Deserialize)]
struct RfqsResponse {
rfqs: Vec<Rfq>,
cursor: Option<String>,
}
#[derive(Debug, Deserialize)]
struct RfqResponse {
rfq: Rfq,
}
#[derive(Debug, Deserialize)]
struct DeleteRfqResponse {}
#[derive(Debug, Deserialize)]
struct QuotesResponse {
quotes: Vec<Quote>,
cursor: Option<String>,
}
#[derive(Debug, Deserialize)]
struct QuoteResponse {
quote: Quote,
}
#[derive(Debug, Deserialize)]
struct DeleteQuoteResponse {}
// -------- Public models --------
/// Response from creating a new RFQ.
#[derive(Debug, Deserialize, Serialize)]
pub struct CreateRfqResponse {
/// The ID of the newly created RFQ.
pub id: String,
}
/// Response from creating a new quote.
#[derive(Debug, Deserialize, Serialize)]
pub struct CreateQuoteResponse {
/// The ID of the newly created quote.
pub id: String,
}
/// Represents a communication message or thread.
#[derive(Debug, Deserialize, Serialize)]
pub struct Communication {
/// The communication ID.
pub id: String,
/// The communication type.
#[serde(rename = "type")]
pub comm_type: String,
/// The message content.
pub message: Option<String>,
/// Timestamp when created.
pub created_time: String,
/// Additional fields.
#[serde(flatten)]
pub details: std::collections::HashMap<String, serde_json::Value>,
}
/// Represents an RFQ (Request for Quote).
#[derive(Debug, Deserialize, Serialize)]
pub struct Rfq {
/// The RFQ ID.
pub id: String,
/// The market ticker requested.
#[serde(alias = "ticker")]
pub market_ticker: Option<String>,
/// The desired quantity.
#[serde(default)]
pub contracts: Option<i32>,
/// The side of the trade ("yes" or "no").
pub side: Option<String>,
/// Optional message with the RFQ.
pub message: Option<String>,
/// The status of the RFQ.
pub status: Option<String>,
/// Timestamp when created.
pub created_time: Option<String>,
/// Timestamp when expires.
pub expires_time: Option<String>,
/// Whether to rest the remainder.
pub rest_remainder: Option<bool>,
/// Target cost in centi-cents.
pub target_cost_centi_cents: Option<i64>,
/// Creator user ID.
pub creator_user_id: Option<String>,
/// Additional fields that may be returned by the API.
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
}
/// Represents a quote offer.
#[derive(Debug, Deserialize, Serialize)]
pub struct Quote {
/// The quote ID.
pub id: String,
/// The RFQ this quote responds to.
pub rfq_id: Option<String>,
/// The quoted yes bid price in dollars.
pub yes_bid: Option<String>,
/// The quoted no bid price in dollars.
pub no_bid: Option<String>,
/// The quoted price in cents (legacy).
pub price: Option<i32>,
/// The quoted quantity (legacy).
pub quantity: Option<i32>,
/// The status of the quote.
pub status: Option<String>,
/// Timestamp when created.
pub created_time: Option<String>,
/// Timestamp when expires.
pub expires_time: Option<String>,
/// Whether to rest the remainder.
pub rest_remainder: Option<bool>,
/// Quote creator user ID.
pub quote_creator_user_id: Option<String>,
/// RFQ creator user ID.
pub rfq_creator_user_id: Option<String>,
/// Additional fields that may be returned by the API.
#[serde(flatten)]
pub extra: std::collections::HashMap<String, serde_json::Value>,
}
/// Represents a confirmed quote.
#[derive(Debug, Deserialize, Serialize)]
pub struct QuoteConfirmed {
/// The quote ID.
pub quote_id: String,
/// The status after confirmation.
pub status: String,
/// The fill ID if trade was completed.
pub fill_id: Option<String>,
}