maxt 0.2.1

One Rust API for Upbit, Bithumb, Binance, and Hyperliquid market data, accounts, and orders.
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
//! Bithumb spot adapter.

mod parse;
mod private;
mod rest;
mod stream;
mod wallet;

use crate::adapter::{Adapter, BoxFuture};
use crate::error::{Error, Result};
use crate::feature::Feature;
use crate::request::{
    CancelOrdersRequest, CandleRequest, DepositAddressRequest, OrderHistoryRequest,
    OrderLookupRequest, OrderRequest, TransferHistoryRequest, TransferLookupRequest,
    WithdrawRequest,
};
use crate::stream::{AccountStream, MarketStream};
use crate::transport::HttpTransport;
use crate::types::{
    AssetNetwork, Balance, CancelOrdersResult, Candle, Cursor, Deposit, DepositAddress,
    DepositAddressEntry, Exchange, Market, MarketInfo, MarketKind, Network, Order, OrderBook,
    OrderRules, OrderType, Page, Side, StreamConfig, Subscription, Ticker, Timestamp, Trade,
    Withdrawal, WithdrawalFee, WithdrawalQuote,
};

/// Query parameters for Bithumb's KRW withdrawal list.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BithumbKrwWithdrawalsRequest {
    /// Optional provider withdrawal state, under Bithumb's spelling.
    pub state: Option<String>,
    /// Optional provider withdrawal IDs.
    pub uuids: Vec<String>,
    /// Optional provider transaction IDs.
    pub txids: Vec<String>,
    /// Page number; Bithumb defaults to 1.
    pub page: Option<u32>,
    /// Number of rows; Bithumb defaults to 100 and caps this at 100.
    pub limit: Option<u32>,
    /// Result order; Bithumb defaults to newest first.
    pub order_by: Option<BithumbOrderDirection>,
}

impl BithumbKrwWithdrawalsRequest {
    /// Starts an unfiltered request using Bithumb's defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters by provider state.
    #[must_use]
    pub fn state(mut self, state: impl Into<String>) -> Self {
        self.state = Some(state.into());
        self
    }

    /// Filters by provider withdrawal IDs.
    #[must_use]
    pub fn uuids(mut self, uuids: impl Into<Vec<String>>) -> Self {
        self.uuids = uuids.into();
        self
    }

    /// Filters by provider transaction IDs.
    #[must_use]
    pub fn txids(mut self, txids: impl Into<Vec<String>>) -> Self {
        self.txids = txids.into();
        self
    }

    /// Selects the page number.
    #[must_use]
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }

    /// Selects the page size.
    #[must_use]
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Selects the result order.
    #[must_use]
    pub fn order_by(mut self, order_by: BithumbOrderDirection) -> Self {
        self.order_by = Some(order_by);
        self
    }
}

/// Query parameters for Bithumb's KRW deposit list.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BithumbKrwDepositsRequest {
    /// Optional provider deposit state, under Bithumb's spelling.
    pub state: Option<String>,
    /// Optional provider deposit IDs.
    pub uuids: Vec<String>,
    /// Optional provider transaction IDs.
    pub txids: Vec<String>,
    /// Page number; Bithumb defaults to 1.
    pub page: Option<u32>,
    /// Number of rows; Bithumb defaults to 100 and caps this at 100.
    pub limit: Option<u32>,
    /// Result order; Bithumb defaults to newest first.
    pub order_by: Option<BithumbOrderDirection>,
}

impl BithumbKrwDepositsRequest {
    /// Starts an unfiltered request using Bithumb's defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters by provider state.
    #[must_use]
    pub fn state(mut self, state: impl Into<String>) -> Self {
        self.state = Some(state.into());
        self
    }

    /// Filters by provider deposit IDs.
    #[must_use]
    pub fn uuids(mut self, uuids: impl Into<Vec<String>>) -> Self {
        self.uuids = uuids.into();
        self
    }

    /// Filters by provider transaction IDs.
    #[must_use]
    pub fn txids(mut self, txids: impl Into<Vec<String>>) -> Self {
        self.txids = txids.into();
        self
    }

    /// Selects the page number.
    #[must_use]
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }

    /// Selects the page size.
    #[must_use]
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Selects the result order.
    #[must_use]
    pub fn order_by(mut self, order_by: BithumbOrderDirection) -> Self {
        self.order_by = Some(order_by);
        self
    }
}

/// Request body for a Bithumb KRW deposit or withdrawal.
///
/// Bithumb requires a registered bank account for KRW withdrawals and Kakao
/// second-factor authorization for both KRW transfer writes. The account is a
/// provider-side eligibility condition; Kakao is the only documented request
/// value and is supplied by the adapter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbKrwTransferRequest {
    /// KRW amount submitted to Bithumb.
    pub amount: rust_decimal::Decimal,
}

impl BithumbKrwTransferRequest {
    /// Builds a request using Bithumb's only documented second-factor method.
    pub fn new(amount: rust_decimal::Decimal) -> Self {
        Self { amount }
    }
}

/// One KRW withdrawal returned by Bithumb.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbKrwWithdrawal {
    /// Bithumb's transfer type (`withdraw`).
    pub transfer_type: String,
    /// Provider withdrawal identifier.
    pub uuid: String,
    /// Currency returned by Bithumb (normally `KRW`).
    pub currency: String,
    /// Provider network, when Bithumb returns one.
    pub net_type: Option<String>,
    /// Provider transaction identifier, when assigned.
    pub txid: Option<String>,
    /// Provider withdrawal state, under Bithumb's spelling.
    pub state: String,
    /// Creation time.
    pub created_at: Option<Timestamp>,
    /// Completion time, when complete.
    pub done_at: Option<Timestamp>,
    /// Withdrawal amount.
    pub amount: rust_decimal::Decimal,
    /// Withdrawal fee.
    pub fee: rust_decimal::Decimal,
    /// Provider transaction type, normally `default`.
    pub transaction_type: Option<String>,
}

/// One KRW deposit returned by Bithumb.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbKrwDeposit {
    /// Bithumb's transfer type (`deposit`).
    pub transfer_type: String,
    /// Provider deposit identifier.
    pub uuid: String,
    /// Currency returned by Bithumb (normally `KRW`).
    pub currency: String,
    /// Provider network, when Bithumb returns one.
    pub net_type: Option<String>,
    /// Provider transaction identifier, when assigned.
    pub txid: Option<String>,
    /// Provider deposit state, under Bithumb's spelling.
    pub state: String,
    /// Creation time.
    pub created_at: Option<Timestamp>,
    /// Completion time, when complete.
    pub done_at: Option<Timestamp>,
    /// Deposit amount.
    pub amount: rust_decimal::Decimal,
    /// Deposit fee.
    pub fee: rust_decimal::Decimal,
    /// Provider transaction type, normally `default`.
    pub transaction_type: Option<String>,
}

pub(crate) const REST_BASE_URL: &str = "https://api.bithumb.com";
pub(crate) const WEBSOCKET_URL: &str = "wss://ws-api.bithumb.com/websocket/v1";
/// Private account events use a separate v2 WebSocket endpoint.
pub(crate) const PRIVATE_WEBSOCKET_URL: &str = "wss://ws-api.bithumb.com/websocket/v2/private";

pub(crate) fn network_from_provider(raw: &str) -> Network {
    match raw.trim().to_ascii_uppercase().as_str() {
        "BTC" | "BITCOIN" => Network::Bitcoin,
        "ETH" | "ETHEREUM" => Network::Ethereum,
        "ARB" | "ARBITRUM" | "ARBITRUM ONE" => Network::Arbitrum,
        "BSC" | "BEP20" | "BNB SMART CHAIN" => Network::BnbSmartChain,
        "TRX" | "TRON" => Network::Tron,
        "SOL" | "SOLANA" => Network::Solana,
        "MATIC" | "POLYGON" | "POLYGON POS" => Network::Polygon,
        "BASE" => Network::Base,
        "OP" | "OPTIMISM" => Network::Optimism,
        "AVAXC" | "AVAX-C" | "AVALANCHE C-CHAIN" => Network::AvalancheC,
        "XRP" | "XRP LEDGER" => Network::XrpLedger,
        "XLM" | "STELLAR" => Network::Stellar,
        "ATOM" | "COSMOS" | "COSMOS HUB" => Network::Cosmos,
        "APT" | "APTOS" => Network::Aptos,
        "SUI" => Network::Sui,
        "TON" => Network::Ton,
        "NEAR" => Network::Near,
        "DOT" | "POLKADOT" => Network::Polkadot,
        _ => Network::Other(raw.trim().to_owned()),
    }
}

/// Severity of a Bithumb market alert (경보제), ordered from least to most severe.
///
/// This is separate from the `CAUTION` investment-warning flag returned by
/// [`BithumbAdapter::market_warnings`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum BithumbAlertStep {
    /// Caution (주의), the lowest alert level.
    Caution,
    /// Warning (경고), the middle alert level.
    Warning,
    /// Danger (위험), the highest documented alert level.
    Danger,
    /// An unrecognized level, ordered above [`Self::Danger`] so thresholds surface it.
    Unknown,
}

/// An active Bithumb market alert for one market and criterion.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct BithumbMarketAlert {
    /// Bithumb's criterion code, preserved verbatim for forward compatibility.
    pub kind: String,
    /// Alert severity.
    pub step: BithumbAlertStep,
    /// Alert expiry, converted from Bithumb's KST wall-clock value to UTC.
    pub ends_at: Timestamp,
}

/// One Bithumb exchange notice.
///
/// Bithumb publishes `published_at` and `modified_at` as Korea Standard Time
/// wall-clock values. They are converted to UTC timestamps at the boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbNotice {
    /// Bithumb's notice categories, in the order supplied by the provider.
    pub categories: Vec<String>,
    /// Notice title.
    pub title: String,
    /// Provider-hosted notice URL.
    pub url: String,
    /// Publication time converted from KST to UTC.
    pub published_at: Timestamp,
    /// Most recent modification time converted from KST to UTC.
    pub modified_at: Timestamp,
}

/// One API key registered on a Bithumb account.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbApiKey {
    /// Bithumb's API access-key identifier.
    pub access_key: String,
    /// Key expiration time, converted from Bithumb's offset timestamp.
    pub expires_at: Timestamp,
}

/// A state accepted by Bithumb's pending-order endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BithumbPendingOrderState {
    /// An order resting in the order book.
    Wait,
    /// A reserved order waiting for its trigger price.
    Watch,
}

/// A sort direction accepted by Bithumb's pending-order endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BithumbOrderDirection {
    /// Oldest orders first.
    Ascending,
    /// Newest orders first.
    Descending,
}

/// Filters for Bithumb's paginated pending-order endpoint.
///
/// Leave `state` and `order_by` unset to use Bithumb's `wait` and `desc`
/// defaults. Leave `limit` unset to use Bithumb's default page size of 100.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BithumbPendingOrdersRequest {
    /// Optional market filter.
    pub market: Option<Market>,
    /// Resting (`wait`) or trigger-waiting (`watch`) orders.
    pub state: Option<BithumbPendingOrderState>,
    /// Page size from 1 through 100.
    pub limit: Option<u32>,
    /// Optional result order; Bithumb defaults to newest first.
    pub order_by: Option<BithumbOrderDirection>,
    /// Cursor returned by the preceding page.
    pub cursor: Option<Cursor>,
}

/// One order in a Bithumb batch-order request.
///
/// The common [`OrderRequest`] is used directly so the validation and amount
/// semantics stay identical to single-order placement. Bithumb still exposes
/// this endpoint as a provider-specific operation because its response is
/// non-atomic: every item can be accepted or rejected independently.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbBatchOrdersRequest {
    /// Orders to submit. Bithumb accepts between 1 and 20 items.
    pub orders: Vec<OrderRequest>,
}

impl BithumbBatchOrdersRequest {
    /// Creates a batch request from the supplied order list.
    pub fn new(orders: impl Into<Vec<OrderRequest>>) -> Self {
        Self {
            orders: orders.into(),
        }
    }
}

impl From<Vec<OrderRequest>> for BithumbBatchOrdersRequest {
    fn from(orders: Vec<OrderRequest>) -> Self {
        Self::new(orders)
    }
}

/// One accepted order returned by Bithumb's batch-order endpoint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbBatchOrder {
    /// Exchange-assigned order identifier.
    pub order_id: String,
    /// Caller-assigned identifier, when supplied.
    pub client_order_id: Option<String>,
    /// Market returned by Bithumb.
    pub market: Market,
    /// Buy or sell.
    pub side: Side,
    /// Provider order type.
    pub order_type: OrderType,
    /// Provider time-in-force value, when the request specified one.
    ///
    /// This is retained as Bithumb's raw value so a newly introduced provider
    /// value does not make an otherwise successful batch response undecodable.
    pub time_in_force: Option<String>,
    /// Provider self-trade-prevention value, when returned.
    ///
    /// Bithumb may add values independently of the common order model, so the
    /// raw value is retained rather than narrowing it to a closed enum.
    pub stp_type: Option<String>,
    /// Acceptance time, when returned.
    pub created_at: Option<Timestamp>,
}

/// One rejected item returned by Bithumb's batch-order endpoint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbBatchOrderFailure {
    /// Caller-assigned identifier, when supplied.
    pub client_order_id: Option<String>,
    /// Provider time-in-force value, when returned.
    pub time_in_force: Option<String>,
    /// Provider error code (`name`).
    pub code: String,
    /// Provider error message.
    pub message: String,
}

/// The non-atomic result of a Bithumb batch-order request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BithumbBatchOrderOutcome {
    /// This item was accepted for placement.
    Accepted(BithumbBatchOrder),
    /// This item was rejected while other items may have succeeded.
    Rejected(BithumbBatchOrderFailure),
}

/// All per-item outcomes returned by Bithumb.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbBatchOrdersResult {
    /// Results retain Bithumb's response order.
    pub outcomes: Vec<BithumbBatchOrderOutcome>,
}

/// State filter accepted by Bithumb's TWAP history endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BithumbTwapState {
    /// The strategy is still submitting child orders.
    Progress,
    /// All child orders have completed.
    Done,
    /// The strategy was cancelled before completion.
    Cancel,
}

/// Sort direction accepted by Bithumb's TWAP history endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BithumbTwapOrderDirection {
    /// Oldest TWAP orders first.
    Ascending,
    /// Newest TWAP orders first.
    Descending,
}

/// A page request for Bithumb TWAP orders.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BithumbTwapOrdersRequest {
    /// Optional market filter.
    pub market: Option<Market>,
    /// Optional TWAP identifiers. Bithumb accepts at most the server limit.
    pub uuids: Vec<String>,
    /// Optional lifecycle filter; Bithumb defaults to `progress`.
    pub state: Option<BithumbTwapState>,
    /// Cursor returned by the preceding page.
    pub cursor: Option<Cursor>,
    /// Page size from 1 through 100.
    pub limit: Option<u32>,
    /// Optional result order; Bithumb defaults to newest first.
    pub order_by: Option<BithumbTwapOrderDirection>,
}

impl BithumbTwapOrdersRequest {
    /// Starts an unfiltered request using Bithumb's defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters by market.
    #[must_use]
    pub fn market(mut self, market: Market) -> Self {
        self.market = Some(market);
        self
    }

    /// Filters by one or more TWAP identifiers.
    #[must_use]
    pub fn uuids(mut self, uuids: impl Into<Vec<String>>) -> Self {
        self.uuids = uuids.into();
        self
    }

    /// Filters by lifecycle state.
    #[must_use]
    pub fn state(mut self, state: BithumbTwapState) -> Self {
        self.state = Some(state);
        self
    }

    /// Resumes from a cursor returned by the preceding page.
    #[must_use]
    pub fn cursor(mut self, cursor: Cursor) -> Self {
        self.cursor = Some(cursor);
        self
    }

    /// Sets the page size.
    #[must_use]
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Selects the result order.
    #[must_use]
    pub fn order_by(mut self, order_by: BithumbTwapOrderDirection) -> Self {
        self.order_by = Some(order_by);
        self
    }
}

/// Parameters for creating one Bithumb TWAP order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbTwapOrderRequest {
    /// Bithumb spot market.
    pub market: Market,
    /// Buy (`bid`) or sell (`ask`).
    pub side: Side,
    /// Total base quantity for a sell request.
    pub volume: Option<rust_decimal::Decimal>,
    /// Total quote amount for a buy request.
    pub price: Option<rust_decimal::Decimal>,
    /// Total execution duration in seconds, from 300 through 43,200.
    pub duration: u32,
    /// Child-order interval in seconds: 15, 20, 30, 60, or 120.
    pub frequency: u32,
}

/// One TWAP order returned by Bithumb.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbTwapOrder {
    /// TWAP identifier (`uuid`, also called `algo_order_id`).
    pub id: String,
    /// Buy or sell side.
    pub side: Side,
    /// Price captured when the strategy was created.
    pub price: rust_decimal::Decimal,
    /// Provider lifecycle state.
    pub state: BithumbTwapState,
    /// Bithumb spot market.
    pub market: Market,
    /// Creation time.
    pub created_at: Timestamp,
    /// User-supplied total quantity or amount.
    pub volume: rust_decimal::Decimal,
    /// Completion time, when Bithumb reports one.
    pub finished_at: Option<Timestamp>,
    /// Number of child orders planned.
    pub total_order_count: u32,
    /// Number of child orders that traded.
    pub total_trades_count: u32,
    /// Number of child orders submitted so far.
    pub progress_count: u32,
    /// Total quote amount filled.
    pub total_executed_amount: rust_decimal::Decimal,
    /// Total base quantity filled.
    pub total_executed_volume: rust_decimal::Decimal,
    /// Average fill price.
    pub avg_trade_price: rust_decimal::Decimal,
    /// Bithumb wallet identifier, when Bithumb reports one.
    pub wallet_id: Option<String>,
    /// Cancellation time, when the order was cancelled.
    pub canceled_at: Option<Timestamp>,
    /// Cancellation reason, preserved when Bithumb reports one.
    pub cancel_type: Option<String>,
}

impl BithumbPendingOrdersRequest {
    /// Starts an unfiltered request using Bithumb's defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters by market.
    #[must_use]
    pub fn market(mut self, market: Market) -> Self {
        self.market = Some(market);
        self
    }

    /// Selects the pending-order state.
    #[must_use]
    pub fn state(mut self, state: BithumbPendingOrderState) -> Self {
        self.state = Some(state);
        self
    }

    /// Sets the page size.
    #[must_use]
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Selects the result order.
    #[must_use]
    pub fn order_by(mut self, order_by: BithumbOrderDirection) -> Self {
        self.order_by = Some(order_by);
        self
    }

    /// Resumes from a cursor returned by the preceding page.
    #[must_use]
    pub fn cursor(mut self, cursor: Cursor) -> Self {
        self.cursor = Some(cursor);
        self
    }
}

/// One Bithumb asset's public transfer-fee catalog entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbAssetFee {
    /// Bithumb's display name for the asset.
    pub display_name: String,
    /// Asset symbol, uppercase.
    pub asset: String,
    /// Fee rules for every network Bithumb returned for this asset.
    pub networks: Vec<BithumbNetworkFee>,
}

/// One Bithumb network's public transfer-fee rules.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BithumbNetworkFee {
    /// Canonical network when maxt recognizes Bithumb's network name.
    pub network: Network,
    /// Bithumb's exact `net_name` value.
    pub provider_name: String,
    /// Deposit fee in the transferred asset.
    pub deposit_fee: rust_decimal::Decimal,
    /// Minimum deposit amount.
    pub minimum_deposit: rust_decimal::Decimal,
    /// Fixed or rate-based withdrawal fee rule.
    pub withdrawal_fee: WithdrawalFee,
    /// Minimum withdrawal amount.
    pub minimum_withdrawal: rust_decimal::Decimal,
}

/// Adapter for Bithumb spot markets.
///
/// Public REST supports markets, trades, order books, tickers, and candles.
/// Public WebSocket supports trades, order books, and tickers. Derivatives and
/// candle streams return [`Error::Unsupported`](crate::Error::Unsupported).
#[derive(Debug, Clone)]
pub struct BithumbAdapter {
    credentials: Option<BithumbCredentials>,
    // Stored as a result because `new` is infallible.
    http: Result<HttpTransport>,
}

#[derive(Debug, Clone)]
pub(crate) struct BithumbCredentials {
    pub(crate) access_key: String,
    pub(crate) secret_key: String,
}

impl BithumbAdapter {
    /// Creates an adapter for public market data.
    pub fn new() -> Self {
        Self {
            credentials: None,
            http: HttpTransport::new(REST_BASE_URL),
        }
    }

    /// Adds the access key and secret key required by private APIs.
    #[must_use]
    pub fn with_credentials(
        mut self,
        access_key: impl Into<String>,
        secret_key: impl Into<String>,
    ) -> Self {
        self.credentials = Some(BithumbCredentials {
            access_key: access_key.into(),
            secret_key: secret_key.into(),
        });
        self
    }

    /// Returns every listed market with its raw investment-warning flag.
    ///
    /// The value is `NONE` or `CAUTION` (유의 종목). A warned market remains
    /// tradable and maps to [`MarketStatus::Unknown`](crate::MarketStatus::Unknown).
    /// This flag is separate from [`Self::market_alerts`].
    pub async fn market_warnings(&self) -> Result<Vec<(Market, String)>> {
        rest::market_warnings(self.http()?).await
    }

    /// Returns active alert-system rows, one per market and criterion.
    ///
    /// Markets without alerts are omitted. Alerts do not change the common
    /// [`MarketStatus`](crate::MarketStatus).
    pub async fn market_alerts(&self) -> Result<Vec<(Market, BithumbMarketAlert)>> {
        rest::market_alerts(self.http()?).await
    }

    /// Returns the newest Bithumb exchange notices first.
    ///
    /// `count` must be from 1 through 20. `None` uses Bithumb's documented
    /// five-notice default.
    pub async fn notices(&self, count: Option<u32>) -> Result<Vec<BithumbNotice>> {
        rest::notices(self.http()?, count).await
    }

    /// Returns Bithumb's public transfer-fee catalog for one asset or `ALL`.
    ///
    /// The returned values are fee rules, not account-specific withdrawal
    /// availability. Use [`Adapter::asset_networks`] for an authenticated
    /// account's current transfer status and limits.
    pub async fn transfer_fees(&self, currency: &str) -> Result<Vec<BithumbAssetFee>> {
        rest::transfer_fees(self.http()?, currency).await
    }

    /// Returns Bithumb's KRW withdrawal list.
    pub async fn krw_withdrawals(
        &self,
        request: &BithumbKrwWithdrawalsRequest,
    ) -> Result<Vec<BithumbKrwWithdrawal>> {
        wallet::krw_withdrawals(self.http()?, self.credentials()?, request).await
    }

    /// Requests a KRW withdrawal to the bank account registered with Bithumb.
    ///
    /// The account must be eligible on Bithumb and the API key must permit
    /// KRW withdrawals. Bithumb performs the Kakao second-factor step; this
    /// method does not accept or store local bank-account credentials.
    pub async fn withdraw_krw(
        &self,
        request: &BithumbKrwTransferRequest,
    ) -> Result<BithumbKrwWithdrawal> {
        wallet::withdraw_krw(self.http()?, self.credentials()?, request).await
    }

    /// Returns Bithumb's KRW deposit list.
    pub async fn krw_deposits(
        &self,
        request: &BithumbKrwDepositsRequest,
    ) -> Result<Vec<BithumbKrwDeposit>> {
        wallet::krw_deposits(self.http()?, self.credentials()?, request).await
    }

    /// Requests a KRW deposit using Bithumb's Kakao second-factor method.
    ///
    /// The source bank account and second-factor approval are provider-side
    /// eligibility conditions, not request fields accepted by this API.
    pub async fn deposit_krw(
        &self,
        request: &BithumbKrwTransferRequest,
    ) -> Result<BithumbKrwDeposit> {
        wallet::deposit_krw(self.http()?, self.credentials()?, request).await
    }

    /// Returns the API keys registered on this Bithumb account.
    pub async fn api_keys(&self) -> Result<Vec<BithumbApiKey>> {
        private::api_keys(self.http()?, self.credentials()?).await
    }

    /// Returns one page of Bithumb `wait` or `watch` orders.
    pub async fn pending_orders(
        &self,
        request: &BithumbPendingOrdersRequest,
    ) -> Result<Page<Order>> {
        private::pending_orders(self.http()?, self.credentials()?, request).await
    }

    /// Returns one page of Bithumb TWAP orders.
    pub async fn twap_orders(
        &self,
        request: &BithumbTwapOrdersRequest,
    ) -> Result<Page<BithumbTwapOrder>> {
        private::twap_orders(self.http()?, self.credentials()?, request).await
    }

    /// Creates a TWAP order. This submits a financial request to Bithumb.
    pub async fn create_twap_order(&self, request: &BithumbTwapOrderRequest) -> Result<String> {
        private::create_twap_order(self.http()?, self.credentials()?, request).await
    }

    /// Cancels a TWAP order and returns the cancelled identifier.
    pub async fn cancel_twap_order(&self, algo_order_id: &str) -> Result<String> {
        private::cancel_twap_order(self.http()?, self.credentials()?, algo_order_id).await
    }

    /// Submits up to 20 orders and preserves each accepted/rejected outcome.
    ///
    /// This is a financial write. Bithumb processes items independently and
    /// may return HTTP 200 even when one or more items fail.
    pub async fn batch_orders(
        &self,
        request: &BithumbBatchOrdersRequest,
    ) -> Result<BithumbBatchOrdersResult> {
        private::batch_orders(self.http()?, self.credentials()?, request).await
    }

    pub(crate) fn is_authenticated(&self) -> bool {
        self.credentials.is_some()
    }

    /// Returns credentials or an authentication error before network I/O.
    fn credentials(&self) -> Result<&BithumbCredentials> {
        self.credentials
            .as_ref()
            .ok_or_else(|| Error::auth("bithumb needs both an access key and a secret key"))
    }

    pub(crate) fn http(&self) -> Result<&HttpTransport> {
        self.http.as_ref().map_err(Clone::clone)
    }
}

impl Default for BithumbAdapter {
    fn default() -> Self {
        Self::new()
    }
}

impl Adapter for BithumbAdapter {
    fn exchange(&self) -> Exchange {
        Exchange::Bithumb
    }

    fn supports(&self, feature: Feature) -> bool {
        if matches!(feature, Feature::TravelRule) {
            return false;
        }
        if feature.is_derivatives_only() {
            return false;
        }
        // Bithumb does not publish a public candle stream.
        if matches!(feature, Feature::CandleStream) {
            return false;
        }
        if feature.needs_credentials() {
            return self.is_authenticated();
        }
        true
    }

    fn markets(&self, kind: MarketKind) -> BoxFuture<'_, Result<Vec<MarketInfo>>> {
        Box::pin(async move { rest::markets(self.http()?, kind).await })
    }

    fn trades(&self, market: &Market, limit: Option<u32>) -> BoxFuture<'_, Result<Vec<Trade>>> {
        let market = market.clone();
        Box::pin(async move { rest::trades(self.http()?, &market, limit).await })
    }

    fn order_book(&self, market: &Market, depth: Option<u32>) -> BoxFuture<'_, Result<OrderBook>> {
        let market = market.clone();
        Box::pin(async move { rest::order_book(self.http()?, &market, depth).await })
    }

    fn ticker(&self, market: &Market) -> BoxFuture<'_, Result<Ticker>> {
        let market = market.clone();
        Box::pin(async move { rest::ticker(self.http()?, &market).await })
    }

    fn candles(&self, request: &CandleRequest) -> BoxFuture<'_, Result<Vec<Candle>>> {
        let request = request.clone();
        Box::pin(async move { rest::candles(self.http()?, &request).await })
    }

    fn subscribe(
        &self,
        subscription: &Subscription,
        config: &StreamConfig,
    ) -> BoxFuture<'_, Result<MarketStream>> {
        let subscription = subscription.clone();
        let config = config.clone();
        Box::pin(async move { stream::subscribe(&subscription, &config).await })
    }

    fn subscribe_account(&self, config: &StreamConfig) -> BoxFuture<'_, Result<AccountStream>> {
        let config = config.clone();
        Box::pin(async move { stream::subscribe_account(self.credentials()?, &config).await })
    }

    fn balances(&self) -> BoxFuture<'_, Result<Vec<Balance>>> {
        Box::pin(async move { private::balances(self.http()?, self.credentials()?).await })
    }

    fn order_rules(&self, market: &Market) -> BoxFuture<'_, Result<OrderRules>> {
        let market = market.clone();
        Box::pin(
            async move { private::order_rules(self.http()?, self.credentials()?, &market).await },
        )
    }

    fn asset_networks(&self, asset: &str) -> BoxFuture<'_, Result<Vec<AssetNetwork>>> {
        let asset = asset.to_string();
        Box::pin(
            async move { wallet::asset_networks(self.http()?, self.credentials()?, &asset).await },
        )
    }

    fn deposit_addresses(&self) -> BoxFuture<'_, Result<Vec<DepositAddressEntry>>> {
        Box::pin(async move { wallet::deposit_addresses(self.http()?, self.credentials()?).await })
    }

    fn deposit_address(
        &self,
        request: &DepositAddressRequest,
    ) -> BoxFuture<'_, Result<DepositAddress>> {
        let request = request.clone();
        Box::pin(async move {
            wallet::deposit_address(self.http()?, self.credentials()?, &request).await
        })
    }

    fn create_deposit_address(
        &self,
        request: &DepositAddressRequest,
    ) -> BoxFuture<'_, Result<DepositAddress>> {
        let request = request.clone();
        Box::pin(async move {
            wallet::create_deposit_address(self.http()?, self.credentials()?, &request).await
        })
    }

    fn prepare_withdrawal(
        &self,
        request: &WithdrawRequest,
    ) -> BoxFuture<'_, Result<WithdrawalQuote>> {
        let request = request.clone();
        Box::pin(async move {
            wallet::prepare_withdrawal(self.http()?, self.credentials()?, &request).await
        })
    }

    fn withdraw(&self, request: &WithdrawRequest) -> BoxFuture<'_, Result<Withdrawal>> {
        let request = request.clone();
        Box::pin(async move { wallet::withdraw(self.http()?, self.credentials()?, &request).await })
    }

    fn deposit(&self, request: &TransferLookupRequest) -> BoxFuture<'_, Result<Deposit>> {
        let request = request.clone();
        Box::pin(async move { wallet::deposit(self.http()?, self.credentials()?, &request).await })
    }

    fn withdrawal(&self, request: &TransferLookupRequest) -> BoxFuture<'_, Result<Withdrawal>> {
        let request = request.clone();
        Box::pin(
            async move { wallet::withdrawal(self.http()?, self.credentials()?, &request).await },
        )
    }

    fn cancel_withdrawal(&self, withdrawal_id: &str) -> BoxFuture<'_, Result<()>> {
        let withdrawal_id = withdrawal_id.to_owned();
        Box::pin(async move {
            wallet::cancel_withdrawal(self.http()?, self.credentials()?, &withdrawal_id).await
        })
    }

    fn deposits(&self, request: &TransferHistoryRequest) -> BoxFuture<'_, Result<Page<Deposit>>> {
        let request = request.clone();
        Box::pin(async move { wallet::deposits(self.http()?, self.credentials()?, &request).await })
    }

    fn withdrawals(
        &self,
        request: &TransferHistoryRequest,
    ) -> BoxFuture<'_, Result<Page<Withdrawal>>> {
        let request = request.clone();
        Box::pin(
            async move { wallet::withdrawals(self.http()?, self.credentials()?, &request).await },
        )
    }

    fn open_orders(&self, market: Option<&Market>) -> BoxFuture<'_, Result<Vec<Order>>> {
        let market = market.cloned();
        Box::pin(async move {
            private::open_orders(self.http()?, self.credentials()?, market.as_ref()).await
        })
    }

    fn order(&self, market: &Market, order_id: &str) -> BoxFuture<'_, Result<Order>> {
        let market = market.clone();
        let order_id = order_id.to_string();
        Box::pin(async move {
            private::order(self.http()?, self.credentials()?, &market, &order_id).await
        })
    }

    fn order_by_client_id(&self, market: &Market, client_id: &str) -> BoxFuture<'_, Result<Order>> {
        let market = market.clone();
        let client_id = client_id.to_string();
        Box::pin(async move {
            private::order_by_client_id(self.http()?, self.credentials()?, &market, &client_id)
                .await
        })
    }

    fn orders_by_ids(&self, request: &OrderLookupRequest) -> BoxFuture<'_, Result<Vec<Order>>> {
        let request = request.clone();
        Box::pin(async move {
            private::orders_by_ids(self.http()?, self.credentials()?, &request).await
        })
    }

    fn order_history(&self, request: &OrderHistoryRequest) -> BoxFuture<'_, Result<Page<Order>>> {
        let request = request.clone();
        Box::pin(async move {
            private::order_history(self.http()?, self.credentials()?, &request).await
        })
    }

    fn place_order(&self, request: &OrderRequest) -> BoxFuture<'_, Result<Order>> {
        let request = request.clone();
        Box::pin(
            async move { private::place_order(self.http()?, self.credentials()?, &request).await },
        )
    }

    fn cancel_order(&self, market: &Market, order_id: &str) -> BoxFuture<'_, Result<()>> {
        let market = market.clone();
        let order_id = order_id.to_string();
        Box::pin(async move {
            private::cancel_order(self.http()?, self.credentials()?, &market, &order_id).await
        })
    }

    fn cancel_order_by_client_id(
        &self,
        market: &Market,
        client_id: &str,
    ) -> BoxFuture<'_, Result<()>> {
        let market = market.clone();
        let client_id = client_id.to_string();
        Box::pin(async move {
            private::cancel_order_by_client_id(
                self.http()?,
                self.credentials()?,
                &market,
                &client_id,
            )
            .await
        })
    }

    fn cancel_orders(
        &self,
        request: &CancelOrdersRequest,
    ) -> BoxFuture<'_, Result<CancelOrdersResult>> {
        let request = request.clone();
        Box::pin(async move {
            private::cancel_orders(self.http()?, self.credentials()?, &request).await
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn candles_are_available_over_rest_but_not_as_a_stream() {
        let adapter = BithumbAdapter::new();

        assert!(adapter.supports(Feature::Candles));
        assert!(!adapter.supports(Feature::CandleStream));
    }

    #[test]
    fn every_other_public_stream_is_available() {
        let adapter = BithumbAdapter::new();

        for feature in [
            Feature::TradeStream,
            Feature::OrderBookStream,
            Feature::TickerStream,
        ] {
            assert!(adapter.supports(feature), "{feature:?}");
        }
    }

    #[test]
    fn a_spot_exchange_never_claims_derivatives_features() {
        let adapter = BithumbAdapter::new().with_credentials("access", "secret");

        for feature in [Feature::Positions, Feature::Margin, Feature::FundingRates] {
            assert!(!adapter.supports(feature), "{feature:?}");
        }
    }

    #[tokio::test]
    async fn subscribing_to_candles_is_refused_before_a_socket_is_opened() {
        use crate::types::{Feed, Interval, Market, StreamConfig, Subscription};

        let subscription = Subscription::new()
            .market(Market::spot(Exchange::Bithumb, "BTC", "KRW"))
            .feed(Feed::Candles(Interval::Min1));

        let error = BithumbAdapter::new()
            .subscribe(&subscription, &StreamConfig::default())
            .await
            .expect_err("bithumb publishes no candle stream");

        assert!(matches!(
            error,
            Error::Unsupported {
                feature: Feature::CandleStream,
                exchange: "bithumb",
                ..
            }
        ));
    }

    #[tokio::test]
    async fn a_private_call_without_credentials_is_an_auth_failure_not_a_missing_feature() {
        let error = BithumbAdapter::new()
            .balances()
            .await
            .expect_err("no credentials were supplied");

        // The feature exists, but the request lacks credentials.
        assert!(
            matches!(error, Error::Auth { .. }),
            "expected an auth failure, got {error:?}"
        );
    }

    #[tokio::test]
    async fn pending_orders_without_credentials_are_rejected_before_network_io() {
        let error = BithumbAdapter::new()
            .pending_orders(&BithumbPendingOrdersRequest::new())
            .await
            .expect_err("no credentials were supplied");

        assert!(matches!(error, Error::Auth { .. }));
    }

    #[tokio::test]
    async fn batch_order_writes_without_credentials_are_rejected_before_network_io() {
        let request = BithumbBatchOrdersRequest::new(vec![OrderRequest::market(
            Market::spot(Exchange::Bithumb, "BTC", "KRW"),
            Side::Buy,
            crate::types::Size::Quote(crate::Decimal::ONE),
        )]);
        let error = BithumbAdapter::new()
            .batch_orders(&request)
            .await
            .expect_err("no credentials were supplied");

        assert!(matches!(error, Error::Auth { .. }));
    }

    #[test]
    fn credentials_are_what_unlock_the_private_half() {
        let public = BithumbAdapter::new();
        let private = BithumbAdapter::new().with_credentials("access", "secret");

        assert!(!private.supports(Feature::TravelRule));

        for feature in [
            Feature::Balances,
            Feature::AssetNetworks,
            Feature::DepositAddresses,
            Feature::DepositHistory,
            Feature::DepositLookup,
            Feature::WithdrawalQuotes,
            Feature::Withdrawals,
            Feature::WithdrawalHistory,
            Feature::WithdrawalLookup,
            Feature::WithdrawalCancellation,
            Feature::Trading,
            Feature::AccountStream,
        ] {
            assert!(!public.supports(feature), "{feature:?}");
            assert!(private.supports(feature), "{feature:?}");
        }
    }
}