kalshi 0.9.0

An HTTPS and Websocket wrapper that allows users to write trading bots for the Kalshi events trading platform.
Documentation
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
use super::Kalshi;
use crate::kalshi_error::*;
use std::fmt;
use uuid::Uuid;

use serde::{Deserialize, Serialize};

impl<'a> Kalshi<'a> {
    /// Retrieves the current balance of the authenticated user from the Kalshi exchange.
    ///
    /// This method fetches the user's balance, requiring a valid authentication token. 
    /// If the user is not logged in or the token is missing, it returns an error.
    ///
    /// # Returns
    ///
    /// - `Ok(i64)`: The user's current balance on successful retrieval.
    /// - `Err(KalshiError)`: An error if the user is not authenticated or if there is an issue with the request.
    ///
    /// # Example
    ///
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let balance = kalshi_instance.get_balance().await.unwrap();
    /// ```
    ///
    pub async fn get_balance(&self) -> Result<i64, KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }

        let balance_url: &str = &format!("{}/portfolio/balance", self.base_url.to_string());

        let result: BalanceResponse = self
            .client
            .get(balance_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .send()
            .await?
            .json()
            .await?;

        Ok(result.balance)
    }

    /// Retrieves a list of orders from the Kalshi exchange based on specified criteria.
    ///
    /// This method fetches multiple orders, allowing for filtering by ticker, event ticker, time range, 
    /// status, and pagination. A valid authentication token is required to access this information.
    /// If the user is not logged in or the token is missing, it returns an error.
    ///
    /// # Arguments
    ///
    /// * `ticker` - An optional string to filter orders by market ticker.
    /// * `event_ticker` - An optional string to filter orders by event ticker.
    /// * `min_ts` - An optional minimum timestamp for order creation time.
    /// * `max_ts` - An optional maximum timestamp for order creation time.
    /// * `status` - An optional string to filter orders by their status.
    /// * `limit` - An optional integer to limit the number of orders returned.
    /// * `cursor` - An optional string for pagination cursor.
    ///
    /// # Returns
    ///
    /// - `Ok((Option<String>, Vec<Order>))`: A tuple containing an optional pagination cursor 
    ///   and a vector of `Order` objects on successful retrieval.
    /// - `Err(KalshiError)`: An error if the user is not authenticated or if there is an issue with the request.
    ///
    /// # Example
    /// Retrieves all possible orders (Will crash, need to limit for a successful request).
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let orders = kalshi_instance.get_multiple_orders(
    ///     Some("ticker_name"), None, None, None, None, None, None
    /// ).await.unwrap();
    /// ```
    ///
    pub async fn get_multiple_orders(
        &self,
        ticker: Option<String>,
        event_ticker: Option<String>,
        min_ts: Option<i64>,
        max_ts: Option<i64>,
        status: Option<String>,
        limit: Option<i32>,
        cursor: Option<String>,
    ) -> Result<(Option<String>, Vec<Order>), KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let user_orders_url: &str = &format!("{}/portfolio/orders", self.base_url.to_string());

        let mut params: Vec<(&str, String)> = Vec::with_capacity(7);

        add_param!(params, "ticker", ticker);
        add_param!(params, "limit", limit);
        add_param!(params, "cursor", cursor);
        add_param!(params, "min_ts", min_ts);
        add_param!(params, "max_ts", max_ts);
        add_param!(params, "event_ticker", event_ticker);
        add_param!(params, "status", status);

        let user_orders_url = reqwest::Url::parse_with_params(user_orders_url, &params)
            .unwrap_or_else(|err| {
                eprintln!("{:?}", err);
                panic!("Internal Parse Error, please contact developer!");
            });

        let result: MultipleOrderResponse = self
            .client
            .get(user_orders_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .send()
            .await?
            .json()
            .await?;

        return Ok((result.cursor, result.orders));
    }

    /// Retrieves detailed information about a specific order from the Kalshi exchange.
    ///
    /// This method fetches data for a single order identified by its order ID. A valid authentication token 
    /// is required to access this information. If the user is not logged in or the token is missing, it returns an error.
    ///
    /// # Arguments
    ///
    /// * `order_id` - A reference to a string representing the order's unique identifier.
    ///
    /// # Returns
    ///
    /// - `Ok(Order)`: Detailed information about the specified order on successful retrieval.
    /// - `Err(KalshiError)`: An error if the user is not authenticated or if there is an issue with the request.
    ///
    /// # Example
    ///
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let order_id = "some_order_id";
    /// let order = kalshi_instance.get_single_order(&order_id).await.unwrap();
    /// ```
    ///
    pub async fn get_single_order(&self, order_id: &String) -> Result<Order, KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let user_order_url: &str = &format!(
            "{}/portfolio/orders/{}",
            self.base_url.to_string(),
            order_id
        );

        let result: SingleOrderResponse = self
            .client
            .get(user_order_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .send()
            .await?
            .json()
            .await?;

        return Ok(result.order);
    }

    /// Cancels an existing order on the Kalshi exchange.
    ///
    /// This method cancels an order specified by its ID. A valid authentication token is 
    /// required to perform this action. If the user is not logged in or the token is missing, 
    /// it returns an error.
    ///
    /// # Arguments
    ///
    /// * `order_id` - A string slice referencing the ID of the order to be canceled.
    ///
    /// # Returns
    ///
    /// - `Ok((Order, i32))`: A tuple containing the updated `Order` object after cancellation 
    ///   and an integer indicating the amount by which the order was reduced on successful cancellation.
    /// - `Err(KalshiError)`: An error if the user is not authenticated or if there is an issue with the request.
    ///
    /// # Example
    ///
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let order_id = "some_order_id";
    /// let (order, reduced_by) = kalshi_instance.cancel_order(order_id).await.unwrap();
    /// ```
    ///
    pub async fn cancel_order(&self, order_id: &str) -> Result<(Order, i32), KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let cancel_order_url: &str = &format!(
            "{}/portfolio/orders/{}",
            self.base_url.to_string(),
            order_id
        );

        let result: DeleteOrderResponse = self
            .client
            .delete(cancel_order_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .send()
            .await?
            .json()
            .await?;

        Ok((result.order, result.reduced_by))
    }
    /// Decreases the size of an existing order on the Kalshi exchange.
    ///
    /// This method allows reducing the size of an order either by specifying the amount to reduce 
    /// (`reduce_by`) or setting a new target size (`reduce_to`). A valid authentication token is 
    /// required for this operation. It's important to provide either `reduce_by` or `reduce_to`, 
    /// but not both at the same time.
    ///
    /// # Arguments
    ///
    /// * `order_id` - A string slice referencing the ID of the order to be decreased.
    /// * `reduce_by` - An optional integer specifying how much to reduce the order by.
    /// * `reduce_to` - An optional integer specifying the new size of the order.
    ///
    /// # Returns
    ///
    /// - `Ok(Order)`: The updated `Order` object after decreasing the size.
    /// - `Err(KalshiError)`: An error if the user is not authenticated, if both `reduce_by` and `reduce_to` are provided, 
    ///   or if there is an issue with the request.
    ///
    /// # Example
    ///
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let order_id = "some_order_id";
    /// let decrease_result = kalshi_instance.decrease_order(order_id, Some(5), None).await.unwrap();
    /// ```
    ///
    pub async fn decrease_order(
        &self,
        order_id: &str,
        reduce_by: Option<i32>,
        reduce_to: Option<i32>,
    ) -> Result<Order, KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let decrease_order_url: &str = &format!(
            "{}/portfolio/orders/{}",
            self.base_url.to_string(),
            order_id
        );

        match (reduce_by, reduce_to) {
            (Some(_), Some(_)) => {
                return Err(KalshiError::UserInputError(
                    "Can only provide reduce_by strict exclusive or reduce_to, can't provide both"
                        .to_string(),
                ));
            }
            (None, None) => {
                return Err(KalshiError::UserInputError(
                    "Must provide either reduce_by exclusive or reduce_to, can't provide neither"
                        .to_string(),
                ));
            }
            _ => {}
        }

        let decrease_payload = DecreaseOrderPayload {
            reduce_by: reduce_by,
            reduce_to: reduce_to,
        };

        let result: SingleOrderResponse = self
            .client
            .post(decrease_order_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .header("content-type", "application/json".to_string())
            .json(&decrease_payload)
            .send()
            .await?
            .json()
            .await?;

        Ok(result.order)
    }

    /// Retrieves a list of fills from the Kalshi exchange based on specified criteria.
    ///
    /// This method fetches multiple fills, allowing for filtering by ticker, order ID, time range,
    /// and pagination. A valid authentication token is required to access this information.
    /// If the user is not logged in or the token is missing, it returns an error.
    ///
    /// # Arguments
    ///
    /// * `ticker` - An optional string to filter fills by market ticker.
    /// * `order_id` - An optional string to filter fills by order ID.
    /// * `min_ts` - An optional minimum timestamp for fill creation time.
    /// * `max_ts` - An optional maximum timestamp for fill creation time.
    /// * `limit` - An optional integer to limit the number of fills returned.
    /// * `cursor` - An optional string for pagination cursor.
    ///
    /// # Returns
    ///
    /// - `Ok((Option<String>, Vec<Fill>))`: A tuple containing an optional pagination cursor 
    ///   and a vector of `Fill` objects on successful retrieval.
    /// - `Err(KalshiError)`: An error if the user is not authenticated or if there is an issue with the request.
    ///
    /// # Example
    /// Retrieves all filled orders
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let fills = kalshi_instance.get_multiple_fills(
    ///     Some("ticker_name"), None, None, None, None, None
    /// ).await.unwrap();
    /// ```
    ///
    pub async fn get_multiple_fills(
        &self,
        ticker: Option<String>,
        order_id: Option<String>,
        min_ts: Option<i64>,
        max_ts: Option<i64>,
        limit: Option<i32>,
        cursor: Option<String>,
    ) -> Result<(Option<String>, Vec<Fill>), KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let user_fills_url: &str = &format!("{}/portfolio/fills", self.base_url.to_string());

        let mut params: Vec<(&str, String)> = Vec::with_capacity(7);

        add_param!(params, "ticker", ticker);
        add_param!(params, "limit", limit);
        add_param!(params, "cursor", cursor);
        add_param!(params, "min_ts", min_ts);
        add_param!(params, "max_ts", max_ts);
        add_param!(params, "order_id", order_id);

        let user_fills_url = reqwest::Url::parse_with_params(user_fills_url, &params)
            .unwrap_or_else(|err| {
                eprintln!("{:?}", err);
                panic!("Internal Parse Error, please contact developer!");
            });

        let result: MultipleFillsResponse = self
            .client
            .get(user_fills_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .send()
            .await?
            .json()
            .await?;

        return Ok((result.cursor, result.fills));
    }

    /// Retrieves a list of portfolio settlements from the Kalshi exchange.
    ///
    /// This method fetches settlements in the user's portfolio, with options for pagination using limit and cursor.
    /// A valid authentication token is required to access this information.
    /// If the user is not logged in or the token is missing, it returns an error.
    ///
    /// # Arguments
    ///
    /// * `limit` - An optional integer to limit the number of settlements returned.
    /// * `cursor` - An optional string for pagination cursor.
    ///
    /// # Returns
    ///
    /// - `Ok((Option<String>, Vec<Settlement>))`: A tuple containing an optional pagination cursor 
    ///   and a vector of `Settlement` objects on successful retrieval.
    /// - `Err(KalshiError)`: An error if the user is not authenticated or if there is an issue with the request.
    ///
    /// # Example
    ///
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let settlements = kalshi_instance.get_portfolio_settlements(None, None).await.unwrap();
    /// ```
    ///
    pub async fn get_portfolio_settlements(
        &self,
        limit: Option<i64>,
        cursor: Option<String>,
    ) -> Result<(Option<String>, Vec<Settlement>), KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let settlements_url: &str = &format!("{}/portfolio/settlements", self.base_url.to_string());

        let mut params: Vec<(&str, String)> = Vec::with_capacity(6);

        add_param!(params, "limit", limit);
        add_param!(params, "cursor", cursor);

        let settlements_url = reqwest::Url::parse_with_params(settlements_url, &params)
            .unwrap_or_else(|err| {
                eprintln!("{:?}", err);
                panic!("Internal Parse Error, please contact developer!");
            });

        let result: PortfolioSettlementResponse = self
            .client
            .get(settlements_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .send()
            .await?
            .json()
            .await?;

        Ok((result.cursor, result.settlements))
    }

    /// Retrieves the user's positions in events and markets from the Kalshi exchange.
    ///
    /// This method fetches the user's positions, providing options for filtering by settlement status, 
    /// specific ticker, and event ticker, as well as pagination using limit and cursor. A valid 
    /// authentication token is required to access this information. If the user is not logged in 
    /// or the token is missing, it returns an error.
    ///
    /// # Arguments
    ///
    /// * `limit` - An optional integer to limit the number of positions returned.
    /// * `cursor` - An optional string for pagination cursor.
    /// * `settlement_status` - An optional string to filter positions by their settlement status.
    /// * `ticker` - An optional string to filter positions by market ticker.
    /// * `event_ticker` - An optional string to filter positions by event ticker.
    ///
    /// # Returns
    ///
    /// - `Ok((Option<String>, Vec<EventPosition>, Vec<MarketPosition>))`: A tuple containing an optional pagination cursor, 
    ///   a vector of `EventPosition` objects, and a vector of `MarketPosition` objects on successful retrieval.
    /// - `Err(KalshiError)`: An error if the user is not authenticated or if there is an issue with the request.
    ///
    /// # Example
    ///
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let user_positions = kalshi_instance.get_user_positions(None, None, None, None, None).await.unwrap();
    /// ```
    ///
    pub async fn get_user_positions(
        &self,
        limit: Option<i64>,
        cursor: Option<String>,
        settlement_status: Option<String>,
        ticker: Option<String>,
        event_ticker: Option<String>,
    ) -> Result<(Option<String>, Vec<EventPosition>, Vec<MarketPosition>), KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let positions_url: &str = &format!("{}/portfolio/positions", self.base_url.to_string());

        let mut params: Vec<(&str, String)> = Vec::with_capacity(6);

        add_param!(params, "limit", limit);
        add_param!(params, "cursor", cursor);
        add_param!(params, "settlement_status", settlement_status);
        add_param!(params, "ticker", ticker);
        add_param!(params, "event_ticker", event_ticker);

        let positions_url =
            reqwest::Url::parse_with_params(positions_url, &params).unwrap_or_else(|err| {
                eprintln!("{:?}", err);
                panic!("Internal Parse Error, please contact developer!");
            });

        let result: GetPositionsResponse = self
            .client
            .get(positions_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .send()
            .await?
            .json()
            .await?;

        Ok((
            result.cursor,
            result.event_positions,
            result.market_positions,
        ))
    }

    /// Submits an order to the Kalshi exchange.
    ///
    /// This method allows placing an order in the market, requiring details such as action, count, side,
    /// ticker, order type, and other optional parameters. A valid authentication token is 
    /// required for this operation. Note that for limit orders, either `no_price` or `yes_price` must be provided, 
    /// but not both.
    ///
    /// # Arguments
    ///
    /// * `action` - The action (buy/sell) of the order.
    /// * `client_order_id` - An optional client-side identifier for the order.
    /// * `count` - The number of shares or contracts to trade.
    /// * `side` - The side (Yes/No) of the order.
    /// * `ticker` - The market ticker the order is placed in.
    /// * `input_type` - The type of the order (e.g., market, limit).
    /// * `buy_max_cost` - The maximum cost for a buy order. Optional.
    /// * `expiration_ts` - The expiration timestamp for the order. Optional.
    /// * `no_price` - The price for the 'No' option in a limit order. Optional.
    /// * `sell_position_floor` - The minimum position size to maintain after selling. Optional.
    /// * `yes_price` - The price for the 'Yes' option in a limit order. Optional.
    ///
    /// # Returns
    ///
    /// - `Ok(Order)`: The created `Order` object on successful placement.
    /// - `Err(KalshiError)`: An error if the user is not authenticated, if both `no_price` and `yes_price` are provided for limit orders, 
    ///   or if there is an issue with the request.
    ///
    /// # Example
    ///
    /// ```
    /// // Assuming `kalshi_instance` is an already authenticated instance of `Kalshi`
    /// let action = Action::Buy;
    /// let side = Side::Yes;
    /// let order = kalshi_instance.create_order(
    ///     action,
    ///     None,
    ///     10,
    ///     side,
    ///     "example_ticker",
    ///     OrderType::Limit,
    ///     None,
    ///     None,
    ///     None,
    ///     None,
    ///     Some(100)
    /// ).await.unwrap();
    /// ```
    ///
    pub async fn create_order(
        &self,
        action: Action,
        client_order_id: Option<String>,
        count: i32,
        side: Side,
        ticker: String,
        input_type: OrderType,
        buy_max_cost: Option<i64>,
        expiration_ts: Option<i64>,
        no_price: Option<i64>,
        sell_position_floor: Option<i32>,
        yes_price: Option<i64>,
    ) -> Result<Order, KalshiError> {
        if self.curr_token == None {
            return Err(KalshiError::UserInputError(
                "Not logged in, a valid token is required for requests that require authentication"
                    .to_string(),
            ));
        }
        let order_url: &str = &format!("{}/portfolio/orders", self.base_url.to_string());

        match input_type {
            OrderType::Limit => match (no_price, yes_price) {
                (Some(_), Some(_)) => {
                    return Err(KalshiError::UserInputError(
                        "Can only provide no_price exclusive or yes_price, can't provide both"
                            .to_string(),
                    ));
                }
                (None, None) => {
                    return Err(KalshiError::UserInputError(
                            "Must provide either no_price exclusive or yes_price, can't provide neither"
                                .to_string(),
                        ));
                }
                _ => {}
            },
            _ => {}
        }

        let unwrapped_id = match client_order_id {
            Some(id) => id,
            _ => String::from(Uuid::new_v4()),
        };

        let order_payload = CreateOrderPayload {
            action: action,
            client_order_id: unwrapped_id,
            count: count,
            side: side,
            ticker: ticker,
            r#type: input_type,
            buy_max_cost: buy_max_cost,
            expiration_ts: expiration_ts,
            no_price: no_price,
            sell_position_floor: sell_position_floor,
            yes_price: yes_price,
        };

        let response: SingleOrderResponse = self
            .client
            .post(order_url)
            .header("Authorization", self.curr_token.clone().unwrap())
            .header("content-type", "application/json".to_string())
            .json(&order_payload)
            .send()
            .await?
            .json()
            .await?;

        Ok(response.order)
    }
}

// PRIVATE STRUCTS
// used in getbalance method
#[derive(Debug, Serialize, Deserialize)]
struct BalanceResponse {
    balance: i64,
}

#[derive(Debug, Deserialize, Serialize)]
struct SingleOrderResponse {
    order: Order,
}

#[derive(Debug, Deserialize, Serialize)]
struct MultipleOrderResponse {
    orders: Vec<Order>,
    cursor: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
struct DeleteOrderResponse {
    order: Order,
    reduced_by: i32,
}

#[derive(Debug, Deserialize, Serialize)]
struct DecreaseOrderResponse {
    order: Order,
}

#[derive(Debug, Deserialize, Serialize)]
struct DecreaseOrderPayload {
    reduce_by: Option<i32>,
    reduce_to: Option<i32>,
}

#[derive(Debug, Deserialize, Serialize)]
struct MultipleFillsResponse {
    fills: Vec<Fill>,
    cursor: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
struct PortfolioSettlementResponse {
    cursor: Option<String>,
    settlements: Vec<Settlement>,
}

#[derive(Debug, Deserialize, Serialize)]
struct GetPositionsResponse {
    cursor: Option<String>,
    event_positions: Vec<EventPosition>,
    market_positions: Vec<MarketPosition>,
}

#[derive(Debug, Deserialize, Serialize)]
struct CreateOrderPayload {
    action: Action,
    client_order_id: String,
    count: i32,
    side: Side,
    ticker: String,
    r#type: OrderType,
    buy_max_cost: Option<i64>,
    expiration_ts: Option<i64>,
    no_price: Option<i64>,
    sell_position_floor: Option<i32>,
    yes_price: Option<i64>,
}

// PUBLIC STRUCTS
// -------------------------

/// Represents an order in the Kalshi exchange.
///
/// This struct details an individual order, including its identification, status, prices, and various metrics related to its lifecycle.
///
#[derive(Debug, Deserialize, Serialize)]
pub struct Order {
    /// Unique identifier for the order.
    pub order_id: String,
    /// Identifier of the user who placed the order. Optional.
    pub user_id: Option<String>,
    /// Ticker of the market associated with the order.
    pub ticker: String,
    /// Current status of the order (e.g., resting, executed).
    pub status: OrderStatus,
    /// Price of the 'Yes' option in the order.
    pub yes_price: i32,
    /// Price of the 'No' option in the order.
    pub no_price: i32,
    /// Timestamp when the order was created. Optional.
    pub created_time: Option<String>,
    /// Count of fills where the order acted as a taker. Optional.
    pub taker_fill_count: Option<i32>,
    /// Total cost of taker fills. Optional.
    pub taker_fill_cost: Option<i32>,
    /// Count of order placements. Optional.
    pub place_count: Option<i32>,
    /// Count of order decreases. Optional.
    pub decrease_count: Option<i32>,
    /// Count of fills where the order acted as a maker. Optional.
    pub maker_fill_count: Option<i32>,
    /// Count of FCC (Financial Crime Compliance) cancellations. Optional.
    pub fcc_cancel_count: Option<i32>,
    /// Count of cancellations at market close. Optional.
    pub close_cancel_count: Option<i32>,
    /// Remaining count of the order. Optional.
    pub remaining_count: Option<i32>,
    /// Position of the order in the queue. Optional.
    pub queue_position: Option<i32>,
    /// Expiration time of the order. Optional.
    pub expiration_time: Option<String>,
    /// Fees incurred as a taker. Optional.
    pub taker_fees: Option<i32>,
    /// The action (buy/sell) of the order.
    pub action: Action,
    /// The side (Yes/No) of the order.
    pub side: Side,
    /// Type of the order (e.g., market, limit).
    pub r#type: String,
    /// Last update time of the order. Optional.
    pub last_update_time: Option<String>,
    /// Client-side identifier for the order.
    pub client_order_id: String,
    /// Group identifier for the order.
    pub order_group_id: String,
}


/// A completed transaction (a 'fill') in the Kalshi exchange.
///
/// This struct details a single fill instance, including the action taken, the quantity,
/// the involved prices, and the identifiers of the order and trade.
///
#[derive(Debug, Deserialize, Serialize)]
pub struct Fill {
    /// The action (buy/sell) of the fill.
    pub action: Action,
    /// The number of contracts or shares involved in the fill.
    pub count: i32,
    /// The timestamp when the fill was created.
    pub created_time: String,
    /// Indicates if the fill was made by a taker.
    pub is_taker: bool,
    /// The price of the 'No' option in the fill.
    pub no_price: i64,
    /// The identifier of the associated order.
    pub order_id: String,
    /// The side (Yes/No) of the fill.
    pub side: Side,
    /// The ticker of the market in which the fill occurred.
    pub ticker: String,
    /// The unique identifier of the trade.
    pub trade_id: String,
    /// The price of the 'Yes' option in the fill.
    pub yes_price: i64,
}


/// A settlement of a market position in the Kalshi exchange.
///
/// This struct provides details of a market settlement, including the result, quantities,
/// costs involved, and the timestamp of settlement.
///
#[derive(Debug, Deserialize, Serialize)]
pub struct Settlement {
    /// The result of the market settlement.
    pub market_result: String,
    /// The quantity involved in the 'No' position.
    pub no_count: i64,
    /// The total cost associated with the 'No' position.
    pub no_total_cost: i64,
    /// The revenue generated from the settlement, in cents.
    pub revenue: i64,
    /// The timestamp when the settlement occurred.
    pub settled_time: String,
    /// The ticker of the market that was settled.
    pub ticker: String,
    /// The quantity involved in the 'Yes' position.
    pub yes_count: i64,
    /// The total cost associated with the 'Yes' position, in cents.
    pub yes_total_cost: i64,
}

/// A user's position in a specific event on the Kalshi exchange.
///
/// Details the user's exposure, costs, profits, and the number of resting orders related to a particular event.
///
#[derive(Debug, Deserialize, Serialize)]
pub struct EventPosition {
    /// The total exposure amount in the event.
    pub event_exposure: i64,
    /// The ticker of the event.
    pub event_ticker: String,
    /// The total fees paid in the event in cents.
    pub fees_paid: i64,
    /// The realized profit or loss in the event in cents.
    pub realized_pnl: i64,
    /// The count of resting (active but unfilled) orders in the event.
    pub resting_order_count: i32,
    /// The total cost incurred in the event in cents.
    pub total_cost: i64,
}


/// A user's position in a specific market on the Kalshi exchange.
///
/// This struct includes details about the user's market position, including exposure, fees,
/// profits, and the number of resting orders.
///
#[derive(Debug, Deserialize, Serialize)]
pub struct MarketPosition {
    /// The total fees paid in the market in cents.
    pub fees_paid: i64,
    /// The total exposure amount in the market.
    pub market_exposure: i64,
    /// The current position of the user in the market.
    pub position: i32,
    /// The realized profit or loss in the market in cents.
    pub realized_pnl: i64,
    /// The count of resting orders in the market.
    pub resting_orders_count: i32,
    /// The ticker of the market.
    pub ticker: String,
    /// The total traded amount in the market.
    pub total_traded: i64,
}


/// The side of a market position in the Kalshi exchange.
///
/// This enum is used to indicate whether a market position, order, or trade is associated with the 'Yes' or 'No' outcome of a market event.
///
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Side {
    /// Represents a position, order, or trade associated with the 'Yes' outcome of a market event.
    Yes,
    /// Represents a position, order, or trade associated with the 'No' outcome of a market event.
    No,
}

/// This enum is used to specify the type of action a user wants to take in an order, either buying or selling.
///
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Action {
    /// Represents a buy action.
    Buy,
    /// Represents a sell action.
    Sell,
}

impl fmt::Display for Action {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Action::Buy => write!(f, "buy"),
            Action::Sell => write!(f, "sell"),
        }
    }
}

/// The status of an order in the Kalshi exchange.
///
/// This enum categorizes an order's lifecycle state, from creation to completion or cancellation.
///
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OrderStatus {
    /// The order is active but not yet filled or partially filled and still in the order book.
    Resting,
    /// The order has been canceled and is no longer active.
    Canceled,
    /// The order has been fully executed.
    Executed,
    /// The order has been created and is awaiting further processing.
    Pending,
}

impl fmt::Display for OrderStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OrderStatus::Resting => write!(f, "resting"),
            OrderStatus::Canceled => write!(f, "cancelled"),
            OrderStatus::Executed => write!(f, "executed"),
            OrderStatus::Pending => write!(f, "pending"),
        }
    }
}

/// Defines the type of an order in the Kalshi exchange.
///
/// This enum is used to specify the nature of the order, particularly how it interacts with the market.
///
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OrderType {
    /// A market order is executed immediately at the current market price.
    Market,
    /// A limit order is set to be executed at a specific price or better.
    Limit,
}