patisson-bybit-sdk 0.3.0

Unofficial Rust SDK for the Bybit exchange API
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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
use rust_decimal::{Decimal, serde::str_option::deserialize as option_decimal};
use serde::{Deserialize, Serialize};
use serde_aux::prelude::{
    deserialize_number_from_string as number,
    deserialize_option_number_from_string as option_number,
};

use crate::{
    ContractType, CopyTrading, CurAuctionPhase, OptionType, Side, Status, Timestamp,
    enums::{Category, Interval, IntervalTime},
    serde::{empty_string_as_none, int_to_bool, string_to_bool},
};

/// Query parameters of `GET /v5/market/kline` ([`Client::get_index_price_kline`](crate::http::Client::get_index_price_kline), [`Client::get_kline`](crate::http::Client::get_kline), [`Client::get_mark_price_kline`](crate::http::Client::get_mark_price_kline), [`Client::get_premium_index_price_kline`](crate::http::Client::get_premium_index_price_kline)).
#[derive(Debug, Serialize, Clone)]
pub struct GetKLinesParams {
    /// Product type. linear, inverse When category is not passed, use linear by default.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: String,
    /// Kline interval. 1, 3, 5, 15, 30, 60, 120, 240, 360, 720, D, W, M.
    pub interval: Interval,
    /// The start timestamp (ms).
    pub start: Option<Timestamp>,
    /// The end timestamp (ms).
    pub end: Option<Timestamp>,
    /// Limit for data size per page. \[ 1, 1000 \]. Default: 200.
    pub limit: Option<u64>,
}

impl GetKLinesParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category, symbol: impl Into<String>, interval: Interval) -> Self {
        Self {
            category,
            symbol: symbol.into(),
            interval,
            start: None,
            end: None,
            limit: None,
        }
    }

    /// Set `start`: the start timestamp (ms).
    pub fn with_start(mut self, v: Timestamp) -> Self {
        self.start = Some(v);
        self
    }

    /// Set `end`: the end timestamp (ms).
    pub fn with_end(mut self, v: Timestamp) -> Self {
        self.end = Some(v);
        self
    }

    /// Set `limit`: limit for data size per page. \[ 1, 1000 \]. Default: 200.
    pub fn with_limit(mut self, v: u64) -> Self {
        self.limit = Some(v);
        self
    }
}

/// Result of `GET /v5/market/kline` ([`Client::get_kline`](crate::http::Client::get_kline)).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "category")]
pub enum KLine {
    /// Inverse contracts.
    #[serde(rename = "inverse")]
    Inverse {
        /// Symbol name.
        symbol: String,
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<KLineRow>,
    },
    /// Linear (USDT/USDC) contracts.
    #[serde(rename = "linear")]
    Linear {
        /// Symbol name.
        symbol: String,
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<KLineRow>,
    },
    /// Options.
    #[serde(rename = "option")]
    Option {
        /// Symbol name.
        symbol: String,
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<KLineRow>,
    },
    /// Spot.
    #[serde(rename = "spot")]
    Spot {
        /// Symbol name.
        symbol: String,
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<KLineRow>,
    },
}

/// Result of the mark, index and premium index price kline endpoints: rows
/// carry prices only (no volume or turnover).
#[derive(Debug, Deserialize, PartialEq)]
pub struct PriceKLine {
    /// Product type.
    pub category: Category,
    /// Symbol name.
    pub symbol: String,
    /// Klines, newest first.
    pub list: Vec<PriceKLineRow>,
}

/// A price-only kline row: `[startTime, open, high, low, close]`.
#[derive(Debug, Deserialize, PartialEq)]
pub struct PriceKLineRow {
    /// Start time of the candle, milliseconds.
    #[serde(deserialize_with = "number")]
    pub start_time: Timestamp,
    /// Open price.
    pub open_price: Decimal,
    /// Highest price.
    pub high_price: Decimal,
    /// Lowest price.
    pub low_price: Decimal,
    /// Close price; the latest price while the candle is open.
    pub close_price: Decimal,
}

/// Part of the response of `/v5/market/kline`.
#[derive(Debug, Deserialize, PartialEq)]
pub struct KLineRow {
    /// Start time of the candle (ms)
    #[serde(rename = "startTime", deserialize_with = "number")]
    pub start_time: Timestamp,
    /// Open price
    #[serde(rename = "openPrice")]
    pub open_price: Decimal,
    /// Highest price
    #[serde(rename = "highPrice")]
    pub high_price: Decimal,
    /// Lowest price
    #[serde(rename = "lowPrice")]
    pub low_price: Decimal,
    /// Close price. Is the last traded price when the candle is not closed
    #[serde(rename = "closePrice")]
    pub close_price: Decimal,
    /// Trade volume. Unit of contract: pieces of contract. Unit of spot: quantity of coins
    #[serde(rename = "volume")]
    pub volume: Decimal,
    /// Turnover. Unit of figure: quantity of quota coin
    #[serde(rename = "turnover")]
    pub turnover: Decimal,
}

/// Query parameters of `GET /v5/market/tickers` ([`Client::get_tickers`](crate::http::Client::get_tickers)).
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GetTickersParams {
    /// Product type. spot, linear, inverse, option.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: Option<String>,
    /// Base coin, uppercase only. Apply to option only.
    pub base_coin: Option<String>,
    /// Expiry date. e.g., 25DEC22. Apply to option only.
    pub exp_date: Option<String>,
}

impl GetTickersParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category) -> Self {
        Self {
            category,
            symbol: None,
            base_coin: None,
            exp_date: None,
        }
    }

    /// Set `symbol`: symbol name, like BTCUSDT, uppercase only.
    pub fn with_symbol(mut self, v: impl Into<String>) -> Self {
        self.symbol = Some(v.into());
        self
    }

    /// Set `base_coin`: base coin, uppercase only. Apply to option only.
    pub fn with_base_coin(mut self, v: impl Into<String>) -> Self {
        self.base_coin = Some(v.into());
        self
    }

    /// Set `exp_date`: expiry date. e.g., 25DEC22. Apply to option only.
    pub fn with_exp_date(mut self, v: impl Into<String>) -> Self {
        self.exp_date = Some(v.into());
        self
    }
}

/// Result of `GET /v5/market/tickers` ([`Client::get_tickers`](crate::http::Client::get_tickers)).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "category")]
pub enum Ticker {
    /// Inverse contracts.
    #[serde(rename = "inverse")]
    Inverse {
        /// Items.
        list: Vec<LinearInverseTicker>,
    },
    /// Linear (USDT/USDC) contracts.
    #[serde(rename = "linear")]
    Linear {
        /// Items.
        list: Vec<LinearInverseTicker>,
    },
    /// Options.
    #[serde(rename = "option")]
    Option {
        /// Items.
        list: Vec<OptionTicker>,
    },
    /// Spot.
    #[serde(rename = "spot")]
    Spot {
        /// Items.
        list: Vec<SpotTicker>,
    },
}

/// Part of the response of `/v5/market/tickers`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LinearInverseTicker {
    /// Symbol name
    pub symbol: String,
    /// Last price
    pub last_price: Decimal,
    /// Mark price
    pub mark_price: Decimal,
    /// Index price
    pub index_price: Decimal,
    /// Market price 24 hours ago
    pub prev_price24h: Decimal,
    /// Percentage change of market price in the last 24 hours
    pub price24h_pcnt: Decimal,
    /// The highest price in the last 24 hours
    pub high_price24h: Decimal,
    /// The lowest price in the last 24 hours
    pub low_price24h: Decimal,
    /// Market price an hour ago
    pub prev_price1h: Decimal,
    /// Open interest size
    pub open_interest: Decimal,
    /// Open interest value
    pub open_interest_value: Decimal,
    /// Turnover for 24h
    pub turnover24h: Decimal,
    /// Volume for 24h
    pub volume24h: Decimal,
    /// Funding rate
    #[serde(default, deserialize_with = "option_decimal")]
    pub funding_rate: Option<Decimal>,
    /// Next funding timestamp (ms)
    #[serde(deserialize_with = "number")]
    pub next_funding_time: Timestamp,
    /// Predicated delivery price. It has value when 30 min before delivery
    #[serde(default, deserialize_with = "option_decimal")]
    pub predicted_delivery_price: Option<Decimal>,
    /// Basis rate. Unique field for inverse futures & USDC futures
    #[serde(default, deserialize_with = "option_decimal")]
    pub basis_rate: Option<Decimal>,
    /// Basis. Unique field for inverse futures & USDC futures
    #[serde(default, deserialize_with = "option_decimal")]
    pub basis: Option<Decimal>,
    /// Delivery fee rate. Unique field for inverse futures & USDC futures
    #[serde(default, deserialize_with = "option_decimal")]
    pub delivery_fee_rate: Option<Decimal>,
    /// Delivery date time (UTC+0). Unique field for inverse futures & USDC futures
    #[serde(deserialize_with = "option_number")]
    pub delivery_time: Option<Timestamp>,
    /// Best bid price
    pub bid1_price: Decimal,
    /// Best bid size
    pub bid1_size: Decimal,
    /// Best ask price
    pub ask1_price: Decimal,
    /// Best ask size
    pub ask1_size: Decimal,
    /// Estimated pre-market contract open price. The value is meaningless when entering continuous trading phase.
    #[serde(default, deserialize_with = "option_decimal")]
    pub pre_open_price: Option<Decimal>,
    /// Estimated pre-market contract open qty. The value is meaningless when entering continuous trading phase.
    #[serde(default, deserialize_with = "option_decimal")]
    pub pre_qty: Option<Decimal>,
    /// Enum: NotStarted, Finished, CallAuction, CallAuctionNoCancel, CrossMatching, ContinuousTrading.
    #[serde(default, deserialize_with = "empty_string_as_none")]
    pub cur_pre_listing_phase: Option<CurAuctionPhase>,
}

/// Part of the response of `/v5/market/tickers`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OptionTicker {
    /// Symbol name
    pub symbol: String,
    /// Best bid price
    pub bid1_price: Decimal,
    /// Best bid size
    pub bid1_size: Decimal,
    /// Best bid iv
    pub bid1_iv: Decimal,
    /// Best ask price
    pub ask1_price: Decimal,
    /// Best ask size
    pub ask1_size: Decimal,
    /// Best ask iv
    pub ask1_iv: Decimal,
    /// Last price
    pub last_price: Decimal,
    /// The highest price in the last 24 hours
    pub high_price24h: Decimal,
    /// The lowest price in the last 24 hours
    pub low_price24h: Decimal,
    /// Mark price
    pub mark_price: Decimal,
    /// Index price
    pub index_price: Decimal,
    /// Mark price iv
    pub mark_iv: Decimal,
    /// Underlying price
    pub underlying_price: Decimal,
    /// Open interest size
    pub open_interest: Decimal,
    /// Turnover for 24h
    pub turnover24h: Decimal,
    /// Volume for 24h
    pub volume24h: Decimal,
    /// Total volume
    pub total_volume: Decimal,
    /// Total turnover
    pub total_turnover: Decimal,
    /// Delta
    pub delta: Decimal,
    /// Gamma
    pub gamma: Decimal,
    /// Vega
    pub vega: Decimal,
    /// Theta
    pub theta: Decimal,
    /// Predicated delivery price. It has value when 30 min before delivery
    pub predicted_delivery_price: Decimal,
    /// The change in the last 24 hours
    pub change24h: Decimal,
}

/// Part of the response of `/v5/market/tickers`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SpotTicker {
    /// Symbol name
    pub symbol: String,
    /// Best bid price
    pub bid1_price: Decimal,
    /// Best bid size
    pub bid1_size: Decimal,
    /// Best ask price
    pub ask1_price: Decimal,
    /// Best ask size
    pub ask1_size: Decimal,
    /// Last price
    pub last_price: Decimal,
    /// Market price 24 hours ago
    pub prev_price24h: Decimal,
    /// Percentage change of market price in the last 24 hours
    pub price24h_pcnt: Decimal,
    /// The highest price in the last 24 hours
    pub high_price24h: Decimal,
    /// The lowest price in the last 24 hours
    pub low_price24h: Decimal,
    /// Turnover for 24h
    pub turnover24h: Decimal,
    /// Volume for 24h
    pub volume24h: Decimal,
    /// USD index price
    /// - used to calculate USD value of the assets in Unified account
    /// - non-collateral margin coin returns ""
    /// - Only those trading pairs like "XXX/USDT" or "XXX/USDC" have the value
    #[serde(default, deserialize_with = "option_decimal")]
    pub usd_index_price: Option<Decimal>,
}

/// Query parameters of `GET /v5/market/orderbook` ([`Client::get_orderbook`](crate::http::Client::get_orderbook)).
#[derive(Debug, Serialize, Clone)]
pub struct GetOrderbookParams {
    /// Product type. spot, linear, inverse, option.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: String,
    /// Limit size for each bid and ask
    /// - spot: [1, 1000]. Default: 1.
    /// - linear&inverse: [1, 1000]. Default: 25.
    /// - option: [1, 25]. Default: 1.
    pub limit: Option<u64>,
}

impl GetOrderbookParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category, symbol: impl Into<String>) -> Self {
        Self {
            category,
            symbol: symbol.into(),
            limit: None,
        }
    }

    /// Set `limit`: limit size for each bid and ask - spot: [1, 1000]. Default: 1. - linear&inverse: [1, 1000]. Default: 25. - option: [1, 25]. Default: 1.
    pub fn with_limit(mut self, v: u64) -> Self {
        self.limit = Some(v);
        self
    }
}

/// Result of `GET /v5/market/orderbook` ([`Client::get_orderbook`](crate::http::Client::get_orderbook)).
#[derive(Debug, Deserialize, PartialEq)]
pub struct Orderbook {
    /// Symbol name
    #[serde(rename = "s")]
    pub symbol: String,
    /// Bid, buy side. Sorted by price in descending order
    #[serde(rename = "b")]
    pub bids: Vec<OrderbookLevel>,
    /// Ask, sell side. Sorted by price in ascending order
    #[serde(rename = "a")]
    pub asks: Vec<OrderbookLevel>,
    /// The timestamp (ms) that the system generates the data
    pub ts: Timestamp,
    /// Update ID, is a sequence. Occasionally, you'll receive "u"=1, which is a snapshot
    /// data due to the restart of the service. So please overwrite your local orderbook
    #[serde(rename = "u")]
    pub update_id: i64,
    /// Cross sequence. You can use this field to compare different levels orderbook data,
    /// and for the smaller seq, then it means the data is generated earlier
    pub seq: i64,
    /// Cross timestamp (ms). Spot only
    #[serde(default)]
    pub cts: Option<Timestamp>,
}

/// Part of the response of `/v5/market/orderbook`.
#[derive(Debug, Deserialize, PartialEq)]
pub struct OrderbookLevel {
    /// Price
    pub price: Decimal,
    /// Size
    pub size: Decimal,
}

/// Query parameters of `GET /v5/market/recent-trade` ([`Client::get_public_recent_trading_history`](crate::http::Client::get_public_recent_trading_history)).
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GetTradesParams {
    /// Product type. spot, linear, inverse, option.
    pub category: Category,
    /// required for spot/linear/inverse
    /// optional for option
    pub symbol: Option<String>,
    /// Apply to option only
    /// If the field is not passed, return BTC data by default
    pub base_coin: Option<String>,
    /// Option type. Apply to option only
    pub option_type: Option<OptionType>,
    /// spot: `[1, 60]`, default: 60
    /// others: `[1, 1000]`, default: 500
    pub limit: Option<u64>,
}

impl GetTradesParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category) -> Self {
        Self {
            category,
            symbol: None,
            base_coin: None,
            option_type: None,
            limit: None,
        }
    }

    /// Set `symbol`: required for spot/linear/inverse optional for option.
    pub fn with_symbol(mut self, v: impl Into<String>) -> Self {
        self.symbol = Some(v.into());
        self
    }

    /// Set `base_coin`: apply to option only If the field is not passed, return BTC data by default.
    pub fn with_base_coin(mut self, v: impl Into<String>) -> Self {
        self.base_coin = Some(v.into());
        self
    }

    /// Set `option_type`: option type. Apply to option only.
    pub fn with_option_type(mut self, v: OptionType) -> Self {
        self.option_type = Some(v);
        self
    }

    /// Set `limit`: spot: `[1, 60]`, default: 60 others: `[1, 1000]`, default: 500.
    pub fn with_limit(mut self, v: u64) -> Self {
        self.limit = Some(v);
        self
    }
}

/// Result of `GET /v5/market/recent-trade` ([`Client::get_public_recent_trading_history`](crate::http::Client::get_public_recent_trading_history)).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "category")]
pub enum Trade {
    /// Inverse contracts.
    #[serde(rename = "inverse")]
    Inverse {
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<InverseLinearSpotTrade>,
    },
    /// Linear (USDT/USDC) contracts.
    #[serde(rename = "linear")]
    Linear {
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<InverseLinearSpotTrade>,
    },
    /// Options.
    #[serde(rename = "option")]
    Option {
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<OptionTrade>,
    },
    /// Spot.
    #[serde(rename = "spot")]
    Spot {
        /// An string array of individual candle Sort in reverse by startTime.
        list: Vec<InverseLinearSpotTrade>,
    },
}

/// Part of the response of `/v5/account/transaction-log`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct InverseLinearSpotTrade {
    /// Execution ID
    pub exec_id: String,
    /// Symbol name
    pub symbol: String,
    /// Trade price
    pub price: Decimal,
    /// Trade size
    pub size: Decimal,
    /// Side of taker Buy, Sell
    pub side: Side,
    /// Trade time (ms)
    #[serde(deserialize_with = "number")]
    pub time: Timestamp,
    /// boolean Whether the trade is block trade
    pub is_block_trade: bool,
    /// Whether the trade is RPI trade
    #[serde(rename = "isRPITrade")]
    pub is_rpi_trade: bool,
}

/// Part of the response of `/v5/account/transaction-log`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OptionTrade {
    /// Execution ID
    pub exec_id: String,
    /// Symbol name
    pub symbol: String,
    /// Trade price
    pub price: Decimal,
    /// Trade size
    pub size: Decimal,
    /// Side of taker Buy, Sell
    pub side: Side,
    /// Trade time (ms)
    #[serde(deserialize_with = "number")]
    pub time: Timestamp,
    /// boolean Whether the trade is block trade
    pub is_block_trade: bool,
    /// Whether the trade is an RPI trade (not sent for options: `false`).
    #[serde(rename = "isRPITrade", default)]
    pub is_rpi_trade: bool,
    /// Mark price
    #[serde(rename = "mP")]
    pub mark_price: Decimal,
    /// Index price
    #[serde(rename = "iP")]
    pub index_price: Decimal,
    /// Mark iv
    #[serde(rename = "mIv")]
    pub mark_iv: Decimal,
    /// iv
    #[serde(rename = "iv")]
    pub iv: Decimal,
}

/// Result of `GET /v5/market/time` ([`Client::get_server_time`](crate::http::Client::get_server_time)).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ServerTime {
    /// Bybit server timestamp (sec)
    #[serde(deserialize_with = "number")]
    pub time_second: u64,
    /// Bybit server timestamp (nano)
    #[serde(deserialize_with = "number")]
    pub time_nano: u64,
}

/// Query parameters of `GET /v5/market/instruments-info` ([`Client::get_instruments_info`](crate::http::Client::get_instruments_info)).
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GetInstrumentsInfoParams {
    /// Product type. spot, linear, inverse, option.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: Option<String>,
    /// Symbol status filter linear & inverse & spot By default returns Trading & PendingOpen symbols option By default returns PreLaunch, Trading, and Delivering Spot has Trading only linear & inverse: when status=PreLaunch, it returns Pre-Market ...
    pub status: Option<Status>,
    /// Base coin, uppercase only Applies to linear, inverse, option only option: returns BTC by default Pass All to return all option symbols. Only valid when category=option.
    pub base_coin: Option<String>,
    /// Limit for data size per page. \[ 1, 1000 \]. Default: 500.
    pub limit: Option<i64>,
    /// Cursor. Use the nextPageCursor token from the response to retrieve the next page of the result set.
    pub cursor: Option<String>,
}

impl GetInstrumentsInfoParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category) -> Self {
        Self {
            category,
            symbol: None,
            status: None,
            base_coin: None,
            limit: None,
            cursor: None,
        }
    }

    /// Set `symbol`: symbol name, like BTCUSDT, uppercase only.
    pub fn with_symbol(mut self, v: impl Into<String>) -> Self {
        self.symbol = Some(v.into());
        self
    }

    /// Set `status`: symbol status filter linear & inverse & spot By default returns Trading & PendingOpen symbols option By default returns PreLaunch, Trading, and Delivering Spot has Trading only linear & inverse: when status=PreLaunch, it returns Pre-Market ...
    pub fn with_status(mut self, v: Status) -> Self {
        self.status = Some(v);
        self
    }

    /// Set `base_coin`: base coin, uppercase only Applies to linear, inverse, option only option: returns BTC by default Pass All to return all option symbols. Only valid when category=option.
    pub fn with_base_coin(mut self, v: impl Into<String>) -> Self {
        self.base_coin = Some(v.into());
        self
    }

    /// Set `limit`: limit for data size per page. \[ 1, 1000 \]. Default: 500.
    pub fn with_limit(mut self, v: i64) -> Self {
        self.limit = Some(v);
        self
    }

    /// Set `cursor`: cursor. Use the nextPageCursor token from the response to retrieve the next page of the result set.
    pub fn with_cursor(mut self, v: impl Into<String>) -> Self {
        self.cursor = Some(v.into());
        self
    }
}

/// Result of `GET /v5/market/instruments-info` ([`Client::get_instruments_info`](crate::http::Client::get_instruments_info)).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "category")]
pub enum InstrumentsInfo {
    /// Inverse contracts.
    #[serde(rename = "inverse", rename_all = "camelCase")]
    Inverse {
        /// Cursor of the next page; empty on the last page.
        next_page_cursor: String,
        /// Items.
        list: Vec<InverseLinearInstrumentsInfo>,
    },
    /// Linear (USDT/USDC) contracts.
    #[serde(rename = "linear", rename_all = "camelCase")]
    Linear {
        /// Cursor of the next page; empty on the last page.
        next_page_cursor: String,
        /// Items.
        list: Vec<InverseLinearInstrumentsInfo>,
    },
    /// Options.
    #[serde(rename = "option", rename_all = "camelCase")]
    Option {
        /// Cursor of the next page; empty on the last page.
        next_page_cursor: String,
        /// Items.
        list: Vec<OptionInstrumentsInfo>,
    },
    /// Spot.
    #[serde(rename = "spot", rename_all = "camelCase")]
    Spot {
        /// Cursor of the next page; empty on the last page.
        #[serde(default, deserialize_with = "empty_string_as_none")]
        next_page_cursor: Option<String>,
        /// Items.
        list: Vec<SpotInstrumentsInfo>,
    },
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct InverseLinearInstrumentsInfo {
    /// Symbol name.
    pub symbol: String,
    /// Contract type.
    pub contract_type: ContractType,
    /// Instrument status.
    pub status: Status,
    /// Base coin.
    pub base_coin: String,
    /// Quote coin.
    pub quote_coin: String,
    /// Launch timestamp (ms).
    #[serde(deserialize_with = "number")]
    pub launch_time: Timestamp,
    /// Delivery timestamp (ms) Expired futures delivery time Perpetual delisting time.
    #[serde(deserialize_with = "number")]
    pub delivery_time: Timestamp,
    /// Delivery fee rate.
    #[serde(deserialize_with = "option_decimal")]
    pub delivery_fee_rate: Option<Decimal>,
    /// Price scale.
    #[serde(deserialize_with = "number")]
    pub price_scale: i64,
    /// Leverage attributes.
    pub leverage_filter: LeverageFilter,
    /// Price attributes.
    pub price_filter: PriceFilter,
    /// Size attributes.
    pub lot_size_filter: LotSizeFilter,
    /// Whether to support unified margin trade.
    pub unified_margin_trade: bool,
    /// Funding interval (minute).
    pub funding_interval: i64,
    /// Settle coin.
    pub settle_coin: String,
    /// Copy trade symbol or not.
    pub copy_trading: CopyTrading,
    /// Upper limit of funding date.
    pub upper_funding_rate: Decimal,
    /// Lower limit of funding date.
    pub lower_funding_rate: Decimal,
    /// Risk parameters for limit order price. Note that the formula changed in May 2026.
    pub risk_parameters: RiskParameters,
    /// Whether the contract is a pre-market contract When the pre-market contract is converted to official contract, it will be false.
    pub is_pre_listing: bool,
    /// If isPreListing=false, preListingInfo=null If isPreListing=true, preListingInfo is an object.
    pub pre_listing_info: Option<PreListingInfo>,
}

/// An option of `GET /v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OptionInstrumentsInfo {
    /// Symbol name, e.g. `ETH-25JUN27-7000-P-USDT`.
    pub symbol: String,
    /// Display name in the UI.
    #[serde(default)]
    pub display_name: Option<String>,
    /// Trading status.
    pub status: Status,
    /// Base coin.
    pub base_coin: String,
    /// Quote coin.
    pub quote_coin: String,
    /// Settle coin.
    pub settle_coin: String,
    /// Call or put.
    pub options_type: OptionType,
    /// Launch time, milliseconds.
    #[serde(deserialize_with = "number")]
    pub launch_time: Timestamp,
    /// Delivery time, milliseconds.
    #[serde(deserialize_with = "number")]
    pub delivery_time: Timestamp,
    /// Delivery fee rate.
    #[serde(default, deserialize_with = "option_decimal")]
    pub delivery_fee_rate: Option<Decimal>,
    /// Price limits and tick size.
    pub price_filter: PriceFilter,
    /// Order quantity limits.
    pub lot_size_filter: OptionLotSizeFilter,
}

/// Order quantity limits of an option.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OptionLotSizeFilter {
    /// Maximum order quantity.
    pub max_order_qty: Decimal,
    /// Minimum order quantity.
    pub min_order_qty: Decimal,
    /// Quantity step.
    pub qty_step: Decimal,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SpotInstrumentsInfo {
    /// Symbol name
    pub symbol: String,
    /// Base coin
    pub base_coin: String,
    /// Quote coin
    pub quote_coin: String,
    /// Whether or not this is an innovation zone token. 0: false, 1: true
    #[serde(deserialize_with = "string_to_bool")]
    pub innovation: bool,
    /// Instrument status
    pub status: Status,
    /// Margin trade symbol or not
    /// This is to identify if the symbol support margin trading under different account modes
    /// You may find some symbols not supporting margin buy or margin sell, so you need to go to Collateral Info (UTA) to check if that coin is borrowable
    pub margin_trading: String,
    /// Whether or not it has an special treatment label. 0: false, 1: true
    #[serde(deserialize_with = "string_to_bool")]
    pub st_tag: bool,
    /// Size attributes
    pub lot_size_filter: SpotLotSizeFilter,
    /// Price attributes
    pub price_filter: SpotPriceFilter,
    /// Risk parameters for limit order price, refer to announcement
    pub risk_parameters: RiskParameters,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LeverageFilter {
    /// Minimum leverage.
    pub min_leverage: Decimal,
    /// Maximum leverage.
    pub max_leverage: Decimal,
    /// The step to increase/reduce leverage.
    pub leverage_step: Decimal,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PriceFilter {
    /// Minimum order price.
    pub min_price: Decimal,
    /// Maximum order price.
    pub max_price: Decimal,
    /// The step to increase/reduce order price.
    pub tick_size: Decimal,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SpotPriceFilter {
    /// The step to increase/reduce order price
    pub tick_size: Decimal,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LotSizeFilter {
    /// Minimum notional value.
    pub min_notional_value: Decimal,
    /// Maximum quantity for Limit and PostOnly order.
    pub max_order_qty: Decimal,
    /// Maximum quantity for Market order.
    pub max_mkt_order_qty: Decimal,
    /// Minimum order quantity.
    pub min_order_qty: Decimal,
    /// The step to increase/reduce order quantity.
    pub qty_step: Decimal,
    /// Deprecated, please use maxOrderQty.
    pub post_only_max_order_qty: Decimal,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SpotLotSizeFilter {
    /// The precision of base coin
    pub base_precision: Decimal,
    /// The precision of quote coin
    pub quote_precision: Decimal,
    /// Minimum order quantity
    pub min_order_qty: Decimal,
    /// Maximum order quantity
    pub max_order_qty: Decimal,
    /// Minimum order amount
    pub min_order_amt: Decimal,
    /// Maximum order amount
    pub max_order_amt: Decimal,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RiskParameters {
    /// Ratio X.
    pub price_limit_ratio_x: Decimal,
    /// Ratio Y.
    pub price_limit_ratio_y: Decimal,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PreListingInfo {
    /// The current auction phase.
    pub cur_auction_phase: CurAuctionPhase,
    /// Each phase time info.
    pub phases: Vec<Phase>,
    /// Action fee info.
    pub auction_fee_info: AuctionFeeInfo,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Phase {
    /// Pre-market trading phase.
    pub phase: CurAuctionPhase,
    /// The start time of the phase, timestamp(ms).
    #[serde(deserialize_with = "option_number")]
    pub start_time: Option<Timestamp>,
    /// The end time of the phase, timestamp(ms).
    #[serde(deserialize_with = "option_number")]
    pub end_time: Option<Timestamp>,
}

/// Part of the response of `/v5/market/instruments-info`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AuctionFeeInfo {
    /// The trading fee rate during auction phase There is no trading fee until entering continues trading phase.
    pub auction_fee_rate: Decimal,
    /// The taker fee rate during continues trading phase.
    pub taker_fee_rate: Decimal,
    /// The maker fee rate during continues trading phase.
    pub maker_fee_rate: Decimal,
}

// --- Funding Rate History ---

/// Query params for [`Client::get_funding_rate_history`](crate::http::Client::get_funding_rate_history).
///
/// Covers: linear / inverse
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GetFundingRateHistoryParams {
    /// Product type. linear, inverse.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: String,
    /// The start timestamp (ms).
    pub start_time: Option<Timestamp>,
    /// The end timestamp (ms).
    pub end_time: Option<Timestamp>,
    /// Max 200. Default 200.
    pub limit: Option<u64>,
}

impl GetFundingRateHistoryParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category, symbol: impl Into<String>) -> Self {
        Self {
            category,
            symbol: symbol.into(),
            start_time: None,
            end_time: None,
            limit: None,
        }
    }

    /// Set `start_time`: the start timestamp (ms).
    pub fn with_start_time(mut self, v: Timestamp) -> Self {
        self.start_time = Some(v);
        self
    }

    /// Set `end_time`: the end timestamp (ms).
    pub fn with_end_time(mut self, v: Timestamp) -> Self {
        self.end_time = Some(v);
        self
    }

    /// Set `limit`: max 200. Default 200.
    pub fn with_limit(mut self, v: u64) -> Self {
        self.limit = Some(v);
        self
    }
}

/// Response for [`Client::get_funding_rate_history`](crate::http::Client::get_funding_rate_history).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "category")]
pub enum FundingRateHistory {
    /// Linear (USDT/USDC) contracts.
    #[serde(rename = "linear")]
    Linear {
        /// Items.
        list: Vec<FundingRateEntry>,
    },
    /// Inverse contracts.
    #[serde(rename = "inverse")]
    Inverse {
        /// Items.
        list: Vec<FundingRateEntry>,
    },
}

/// Part of the response of `/v5/market/funding/history`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FundingRateEntry {
    /// Symbol name.
    pub symbol: String,
    /// Funding rate.
    pub funding_rate: Decimal,
    /// Funding rate timestamp (ms).
    #[serde(deserialize_with = "number")]
    pub funding_rate_timestamp: Timestamp,
}

// --- Open Interest ---

/// Query params for [`Client::get_open_interest`](crate::http::Client::get_open_interest).
///
/// Covers: linear / inverse
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GetOpenInterestParams {
    /// Product type. linear, inverse.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: String,
    /// Interval time. 5min, 15min, 30min, 1h, 4h, 1d.
    pub interval_time: IntervalTime,
    /// The start timestamp (ms).
    pub start_time: Option<Timestamp>,
    /// The end timestamp (ms).
    pub end_time: Option<Timestamp>,
    /// Max 200. Default 50.
    pub limit: Option<u64>,
    /// Cursor. Used to paginate.
    pub cursor: Option<String>,
}

impl GetOpenInterestParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category, symbol: impl Into<String>, interval_time: IntervalTime) -> Self {
        Self {
            category,
            symbol: symbol.into(),
            interval_time,
            start_time: None,
            end_time: None,
            limit: None,
            cursor: None,
        }
    }

    /// Set `start_time`: the start timestamp (ms).
    pub fn with_start_time(mut self, v: Timestamp) -> Self {
        self.start_time = Some(v);
        self
    }

    /// Set `end_time`: the end timestamp (ms).
    pub fn with_end_time(mut self, v: Timestamp) -> Self {
        self.end_time = Some(v);
        self
    }

    /// Set `limit`: max 200. Default 50.
    pub fn with_limit(mut self, v: u64) -> Self {
        self.limit = Some(v);
        self
    }

    /// Set `cursor`: cursor. Used to paginate.
    pub fn with_cursor(mut self, v: impl Into<String>) -> Self {
        self.cursor = Some(v.into());
        self
    }
}

/// Response for [`Client::get_open_interest`](crate::http::Client::get_open_interest).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OpenInterest {
    /// Symbol name.
    pub symbol: String,
    /// Product type.
    pub category: String,
    /// Items.
    pub list: Vec<OpenInterestEntry>,
    /// Used to paginate.
    #[serde(default, deserialize_with = "empty_string_as_none")]
    pub next_page_cursor: Option<String>,
}

/// Part of the response of `/v5/market/open-interest`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OpenInterestEntry {
    /// Open interest. The value is the sum of both sides. The unit of value, e.g., BTCUSD(inverse) is USD, BTCUSDT(linear) is BTC.
    pub open_interest: Decimal,
    /// The timestamp (ms).
    #[serde(deserialize_with = "number")]
    pub timestamp: Timestamp,
}

// --- Historical Volatility ---

/// Query params for [`Client::get_historical_volatility`](crate::http::Client::get_historical_volatility).
///
/// Covers: option only
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GetHistoricalVolatilityParams {
    /// Product type. option.
    pub category: Category,
    /// Default: BTC.
    pub base_coin: Option<String>,
    /// Accepted values: 7, 14, 21, 30, 60, 90, 180, 270.
    pub period: Option<u16>,
    /// The start timestamp (ms).
    pub start_time: Option<Timestamp>,
    /// The end timestamp (ms).
    pub end_time: Option<Timestamp>,
}

impl GetHistoricalVolatilityParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category) -> Self {
        Self {
            category,
            base_coin: None,
            period: None,
            start_time: None,
            end_time: None,
        }
    }

    /// Set `base_coin`: default: BTC.
    pub fn with_base_coin(mut self, v: impl Into<String>) -> Self {
        self.base_coin = Some(v.into());
        self
    }

    /// Set `period`: accepted values: 7, 14, 21, 30, 60, 90, 180, 270.
    pub fn with_period(mut self, v: u16) -> Self {
        self.period = Some(v);
        self
    }

    /// Set `start_time`: the start timestamp (ms).
    pub fn with_start_time(mut self, v: Timestamp) -> Self {
        self.start_time = Some(v);
        self
    }

    /// Set `end_time`: the end timestamp (ms).
    pub fn with_end_time(mut self, v: Timestamp) -> Self {
        self.end_time = Some(v);
        self
    }
}

/// One data point from
/// [`Client::get_historical_volatility`](crate::http::Client::get_historical_volatility).
///
/// The endpoint returns `result` as a top-level JSON array, so the full
/// response type is `Response<Vec<HistoricalVolatilityEntry>>`.
#[derive(Debug, Deserialize, PartialEq)]
pub struct HistoricalVolatilityEntry {
    /// Period.
    pub period: u16,
    /// Volatility.
    pub value: Decimal,
    /// Timestamp (ms).
    #[serde(deserialize_with = "number")]
    pub time: Timestamp,
}

// --- Insurance ---

/// Query params for [`Client::get_insurance`](crate::http::Client::get_insurance).
#[derive(Debug, Serialize, Clone, Default)]
pub struct GetInsuranceParams {
    /// Coin, uppercase only. Default: return all insurance coins.
    pub coin: Option<String>,
}

impl GetInsuranceParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new() -> Self {
        Self { coin: None }
    }

    /// Set `coin`: coin, uppercase only. Default: return all insurance coins.
    pub fn with_coin(mut self, v: impl Into<String>) -> Self {
        self.coin = Some(v.into());
        self
    }
}

/// Response for [`Client::get_insurance`](crate::http::Client::get_insurance).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Insurance {
    /// Data updated time (ms).
    #[serde(deserialize_with = "number")]
    pub updated_time: Timestamp,
    /// Items.
    pub list: Vec<InsuranceEntry>,
}

/// Part of the response of `/v5/market/insurance`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct InsuranceEntry {
    /// Coin.
    pub coin: String,
    /// Balance.
    pub balance: Decimal,
    /// USD value.
    pub value: Decimal,
}

// --- Risk Limit ---

/// Query params for [`Client::get_risk_limit`](crate::http::Client::get_risk_limit).
///
/// Covers: linear / inverse
#[derive(Debug, Serialize, Clone)]
pub struct GetRiskLimitParams {
    /// Product type. linear, inverse.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: Option<String>,
    /// Cursor. Use the nextPageCursor token from the response to retrieve the next page of the data set.
    pub cursor: Option<String>,
}

impl GetRiskLimitParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category) -> Self {
        Self {
            category,
            symbol: None,
            cursor: None,
        }
    }

    /// Set `symbol`: symbol name, like BTCUSDT, uppercase only.
    pub fn with_symbol(mut self, v: impl Into<String>) -> Self {
        self.symbol = Some(v.into());
        self
    }

    /// Set `cursor`: cursor. Use the nextPageCursor token from the response to retrieve the next page of the data set.
    pub fn with_cursor(mut self, v: impl Into<String>) -> Self {
        self.cursor = Some(v.into());
        self
    }
}

/// Response for [`Client::get_risk_limit`](crate::http::Client::get_risk_limit).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RiskLimit {
    /// Product type.
    pub category: String,
    /// Items.
    pub list: Vec<RiskLimitEntry>,
}

/// Part of the response of `/v5/market/risk-limit`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RiskLimitEntry {
    /// Risk ID.
    pub id: u32,
    /// Symbol name.
    pub symbol: String,
    /// Position limit.
    pub risk_limit_value: Decimal,
    /// Maintain margin rate.
    pub maintenance_margin: Decimal,
    /// Initial margin rate.
    pub initial_margin: Decimal,
    /// 1: true, 0: false.
    #[serde(deserialize_with = "int_to_bool")]
    pub is_lowest_risk: bool,
    /// Allowed max leverage.
    pub max_leverage: Decimal,
}

// --- Delivery Price ---

/// Query params for [`Client::get_delivery_price`](crate::http::Client::get_delivery_price).
///
/// Covers: linear / inverse / option
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GetDeliveryPriceParams {
    /// Product type. linear, inverse, option.
    pub category: Category,
    /// Symbol name, like BTCUSDT, uppercase only.
    pub symbol: Option<String>,
    /// Base coin, uppercase only. Default: BTC. Valid for option only.
    pub base_coin: Option<String>,
    /// Max 200. Default 50.
    pub limit: Option<u64>,
    /// Cursor. Use the nextPageCursor token from the response to retrieve the next page of the result set.
    pub cursor: Option<String>,
}

impl GetDeliveryPriceParams {
    /// Create with the required fields; set the optional ones with the `with_*` methods.
    pub fn new(category: Category) -> Self {
        Self {
            category,
            symbol: None,
            base_coin: None,
            limit: None,
            cursor: None,
        }
    }

    /// Set `symbol`: symbol name, like BTCUSDT, uppercase only.
    pub fn with_symbol(mut self, v: impl Into<String>) -> Self {
        self.symbol = Some(v.into());
        self
    }

    /// Set `base_coin`: base coin, uppercase only. Default: BTC. Valid for option only.
    pub fn with_base_coin(mut self, v: impl Into<String>) -> Self {
        self.base_coin = Some(v.into());
        self
    }

    /// Set `limit`: max 200. Default 50.
    pub fn with_limit(mut self, v: u64) -> Self {
        self.limit = Some(v);
        self
    }

    /// Set `cursor`: cursor. Use the nextPageCursor token from the response to retrieve the next page of the result set.
    pub fn with_cursor(mut self, v: impl Into<String>) -> Self {
        self.cursor = Some(v.into());
        self
    }
}

/// Response for [`Client::get_delivery_price`](crate::http::Client::get_delivery_price).
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DeliveryPrice {
    /// Product type.
    pub category: String,
    /// Refer to the cursor request parameter.
    #[serde(default, deserialize_with = "empty_string_as_none")]
    pub next_page_cursor: Option<String>,
    /// Items.
    pub list: Vec<DeliveryPriceEntry>,
}

/// Part of the response of `/v5/market/delivery-price`.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DeliveryPriceEntry {
    /// Symbol name.
    pub symbol: String,
    /// Delivery price.
    pub delivery_price: Decimal,
    /// Delivery timestamp (ms).
    #[serde(deserialize_with = "number")]
    pub delivery_time: Timestamp,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Category, serde::serialize_query};

    #[test]
    fn trades_params_serialize_option_type_as_call_or_put() {
        let params = GetTradesParams {
            category: Category::Option,
            symbol: None,
            base_coin: Some(String::from("BTC")),
            option_type: Some(OptionType::Put),
            limit: None,
        };

        let query = serialize_query(&params).unwrap();

        assert_eq!(query, "category=option&baseCoin=BTC&optionType=Put");
    }

    #[test]
    fn base_coin_and_exp_date_are_sent_in_camel_case() {
        let tickers = GetTickersParams::new(Category::Option)
            .with_base_coin("ETH")
            .with_exp_date("25DEC26");
        let instruments = GetInstrumentsInfoParams::new(Category::Option).with_base_coin("ETH");

        assert_eq!(
            serialize_query(&tickers).unwrap(),
            "category=option&baseCoin=ETH&expDate=25DEC26"
        );
        assert_eq!(
            serialize_query(&instruments).unwrap(),
            "category=option&baseCoin=ETH"
        );
    }

    #[test]
    fn option_instruments_info_parses() {
        let json = r#"{
            "category": "option",
            "nextPageCursor": "",
            "list": [{
                "symbolId": 379919,
                "symbol": "ETH-25JUN27-7000-P-USDT",
                "status": "Trading",
                "baseCoin": "ETH",
                "quoteCoin": "USDT",
                "settleCoin": "USDT",
                "optionsType": "Put",
                "launchTime": "1787350800000",
                "deliveryTime": "1813910400000",
                "deliveryFeeRate": "0.00015",
                "priceFilter": {"minPrice": "0.1", "maxPrice": "41000", "tickSize": "0.1"},
                "lotSizeFilter": {"maxOrderQty": "5000", "minOrderQty": "0.1", "qtyStep": "0.1"},
                "displayName": "ETHUSDT-25JUN27-7000-P"
            }]
        }"#;

        let info: InstrumentsInfo = serde_json::from_str(json).unwrap();

        let InstrumentsInfo::Option { list, .. } = info else {
            panic!("not an option list");
        };
        assert_eq!(list[0].options_type, OptionType::Put);
        assert_eq!(list[0].lot_size_filter.qty_step, rust_decimal::dec!(0.1));
        assert_eq!(list[0].delivery_time, 1813910400000);
    }
}