rust-okx 0.2.0

Async Rust client for the OKX v5 REST 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
//! Authenticated funding-account and asset endpoints (`/api/v5/asset/*`).

use serde::{Deserialize, Serialize};

use crate::client::OkxClient;
use crate::error::Error;
use crate::model::NumberString;
use crate::transport::Transport;

const NON_TRADABLE_ASSETS: &str = "/api/v5/asset/non-tradable-assets";
const DEPOSIT_ADDRESS: &str = "/api/v5/asset/deposit-address";
const BALANCES: &str = "/api/v5/asset/balances";
const TRANSFER: &str = "/api/v5/asset/transfer";
const TRANSFER_STATE: &str = "/api/v5/asset/transfer-state";
const WITHDRAWAL: &str = "/api/v5/asset/withdrawal";
const DEPOSIT_HISTORY: &str = "/api/v5/asset/deposit-history";
const CURRENCIES: &str = "/api/v5/asset/currencies";
const PURCHASE_REDEMPT: &str = "/api/v5/asset/purchase_redempt";
const BILLS: &str = "/api/v5/asset/bills";
const DEPOSIT_LIGHTNING: &str = "/api/v5/asset/deposit-lightning";
const WITHDRAWAL_LIGHTNING: &str = "/api/v5/asset/withdrawal-lightning";
const CANCEL_WITHDRAWAL: &str = "/api/v5/asset/cancel-withdrawal";
const CONVERT_DUST_ASSETS: &str = "/api/v5/asset/convert-dust-assets";
const ASSET_VALUATION: &str = "/api/v5/asset/asset-valuation";
const WITHDRAWAL_HISTORY: &str = "/api/v5/asset/withdrawal-history";
const DEPOSIT_WITHDRAW_STATUS: &str = "/api/v5/asset/deposit-withdraw-status";

/// Accessor for authenticated funding-account and asset endpoints.
///
/// Obtain one via [`OkxClient::funding`](crate::OkxClient::funding). All methods
/// require credentials. Mutating endpoints operate on the funding account, not
/// the trading account.
pub struct Funding<'a, T> {
    client: &'a OkxClient<T>,
}

impl<'a, T: Transport> Funding<'a, T> {
    pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
        Self { client }
    }

    /// Retrieve currency metadata and chain settings.
    ///
    /// `GET /api/v5/asset/currencies`. Authenticated.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Configuration`] without credentials, [`Error::Api`] on a
    /// non-zero OKX code, or transport/decode errors.
    pub async fn get_currencies(&self, ccy: Option<&str>) -> Result<Vec<Currency>, Error> {
        let query = CcyQuery { ccy };
        self.client.get(CURRENCIES, &query, true).await
    }

    /// Retrieve funding-account balances.
    ///
    /// `GET /api/v5/asset/balances`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_balances(&self, ccy: Option<&str>) -> Result<Vec<FundingBalance>, Error> {
        let query = CcyQuery { ccy };
        self.client.get(BALANCES, &query, true).await
    }

    /// Retrieve non-tradable assets.
    ///
    /// `GET /api/v5/asset/non-tradable-assets`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_non_tradable_assets(
        &self,
        ccy: Option<&str>,
    ) -> Result<Vec<NonTradableAsset>, Error> {
        let query = CcyQuery { ccy };
        self.client.get(NON_TRADABLE_ASSETS, &query, true).await
    }

    /// Retrieve deposit addresses for a currency.
    ///
    /// `GET /api/v5/asset/deposit-address`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_deposit_address(&self, ccy: &str) -> Result<Vec<DepositAddress>, Error> {
        let query = RequiredCcyQuery { ccy };
        self.client.get(DEPOSIT_ADDRESS, &query, true).await
    }

    /// Transfer funds between OKX account types.
    ///
    /// `POST /api/v5/asset/transfer`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn funds_transfer(
        &self,
        request: &FundsTransferRequest,
    ) -> Result<Vec<TransferResult>, Error> {
        self.client.post(TRANSFER, request, true).await
    }

    /// Retrieve the state of a funds transfer.
    ///
    /// `GET /api/v5/asset/transfer-state`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn transfer_state(
        &self,
        trans_id: &str,
        transfer_type: Option<&str>,
    ) -> Result<Vec<TransferState>, Error> {
        let query = TransferStateQuery {
            trans_id,
            transfer_type,
        };
        self.client.get(TRANSFER_STATE, &query, true).await
    }

    /// Withdraw funds from OKX.
    ///
    /// `POST /api/v5/asset/withdrawal`. Authenticated. This is a real asset
    /// movement endpoint; build requests deliberately with
    /// [`WithdrawalRequest::new`].
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn withdrawal(
        &self,
        request: &WithdrawalRequest,
    ) -> Result<Vec<WithdrawalResult>, Error> {
        self.client.post(WITHDRAWAL, request, true).await
    }

    /// Retrieve deposit history.
    ///
    /// `GET /api/v5/asset/deposit-history`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_deposit_history(
        &self,
        request: &DepositHistoryRequest,
    ) -> Result<Vec<DepositRecord>, Error> {
        self.client.get(DEPOSIT_HISTORY, request, true).await
    }

    /// Subscribe or redeem savings through the legacy purchase/redemption
    /// endpoint.
    ///
    /// `POST /api/v5/asset/purchase_redempt`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn purchase_redempt(
        &self,
        ccy: &str,
        amt: &str,
        side: &str,
        rate: &str,
    ) -> Result<Vec<PurchaseRedemptResult>, Error> {
        let body = PurchaseRedemptBody {
            ccy,
            amt,
            side,
            rate,
        };
        self.client.post(PURCHASE_REDEMPT, &body, true).await
    }

    /// Retrieve funding-account bills.
    ///
    /// `GET /api/v5/asset/bills`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_bills(
        &self,
        request: &FundingBillsRequest,
    ) -> Result<Vec<FundingBill>, Error> {
        self.client.get(BILLS, request, true).await
    }

    /// Create or retrieve a Lightning Network deposit invoice.
    ///
    /// `GET /api/v5/asset/deposit-lightning`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_deposit_lightning(
        &self,
        request: &DepositLightningRequest,
    ) -> Result<Vec<DepositLightning>, Error> {
        self.client.get(DEPOSIT_LIGHTNING, request, true).await
    }

    /// Withdraw through the Lightning Network.
    ///
    /// `POST /api/v5/asset/withdrawal-lightning`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn withdrawal_lightning(
        &self,
        request: &WithdrawalLightningRequest,
    ) -> Result<Vec<WithdrawalLightning>, Error> {
        self.client.post(WITHDRAWAL_LIGHTNING, request, true).await
    }

    /// Cancel a withdrawal.
    ///
    /// `POST /api/v5/asset/cancel-withdrawal`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn cancel_withdrawal(&self, wd_id: &str) -> Result<Vec<WithdrawalResult>, Error> {
        let body = WithdrawalIdBody { wd_id };
        self.client.post(CANCEL_WITHDRAWAL, &body, true).await
    }

    /// Convert small balances into another asset.
    ///
    /// `POST /api/v5/asset/convert-dust-assets`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn convert_dust_assets(
        &self,
        ccy: &[&str],
    ) -> Result<Vec<ConvertDustAssetsResult>, Error> {
        let body = ConvertDustAssetsBody { ccy };
        self.client.post(CONVERT_DUST_ASSETS, &body, true).await
    }

    /// Retrieve total asset valuation.
    ///
    /// `GET /api/v5/asset/asset-valuation`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_asset_valuation(
        &self,
        ccy: Option<&str>,
    ) -> Result<Vec<AssetValuation>, Error> {
        let query = CcyQuery { ccy };
        self.client.get(ASSET_VALUATION, &query, true).await
    }

    /// Retrieve deposit/withdrawal status.
    ///
    /// `GET /api/v5/asset/deposit-withdraw-status`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_deposit_withdraw_status(
        &self,
        request: &DepositWithdrawStatusRequest,
    ) -> Result<Vec<DepositWithdrawStatus>, Error> {
        self.client
            .get(DEPOSIT_WITHDRAW_STATUS, request, true)
            .await
    }

    /// Retrieve withdrawal history.
    ///
    /// `GET /api/v5/asset/withdrawal-history`. Authenticated.
    ///
    /// # Errors
    ///
    /// See [`get_currencies`](Self::get_currencies).
    pub async fn get_withdrawal_history(
        &self,
        request: &WithdrawalHistoryRequest,
    ) -> Result<Vec<WithdrawalRecord>, Error> {
        self.client.get(WITHDRAWAL_HISTORY, request, true).await
    }
}

#[derive(Debug, Serialize)]
struct CcyQuery<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    ccy: Option<&'a str>,
}

#[derive(Debug, Serialize)]
struct RequiredCcyQuery<'a> {
    ccy: &'a str,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct TransferStateQuery<'a> {
    #[serde(rename = "transId")]
    trans_id: &'a str,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    transfer_type: Option<&'a str>,
}

#[derive(Debug, Serialize)]
struct PurchaseRedemptBody<'a> {
    ccy: &'a str,
    amt: &'a str,
    side: &'a str,
    rate: &'a str,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct WithdrawalIdBody<'a> {
    #[serde(rename = "wdId")]
    wd_id: &'a str,
}

#[derive(Debug, Serialize)]
struct ConvertDustAssetsBody<'a> {
    ccy: &'a [&'a str],
}

/// Request body for [`Funding::funds_transfer`].
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FundsTransferRequest {
    ccy: String,
    amt: String,
    #[serde(rename = "from")]
    from_account: String,
    to: String,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    transfer_type: Option<String>,
    #[serde(rename = "subAcct", skip_serializing_if = "Option::is_none")]
    sub_account: Option<String>,
    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
    inst_id: Option<String>,
    #[serde(rename = "toInstId", skip_serializing_if = "Option::is_none")]
    to_inst_id: Option<String>,
    #[serde(rename = "loanTrans", skip_serializing_if = "Option::is_none")]
    loan_transfer: Option<String>,
}

impl FundsTransferRequest {
    /// Create a funds-transfer request.
    pub fn new(
        ccy: impl Into<String>,
        amt: impl Into<String>,
        from_account: impl Into<String>,
        to: impl Into<String>,
    ) -> Self {
        Self {
            ccy: ccy.into(),
            amt: amt.into(),
            from_account: from_account.into(),
            to: to.into(),
            transfer_type: None,
            sub_account: None,
            inst_id: None,
            to_inst_id: None,
            loan_transfer: None,
        }
    }

    /// Set transfer type, e.g. `0` for transfer within account.
    pub fn transfer_type(mut self, transfer_type: impl Into<String>) -> Self {
        self.transfer_type = Some(transfer_type.into());
        self
    }

    /// Set sub-account name.
    pub fn sub_account(mut self, sub_account: impl Into<String>) -> Self {
        self.sub_account = Some(sub_account.into());
        self
    }

    /// Set source instrument ID.
    pub fn inst_id(mut self, inst_id: impl Into<String>) -> Self {
        self.inst_id = Some(inst_id.into());
        self
    }

    /// Set destination instrument ID.
    pub fn to_inst_id(mut self, to_inst_id: impl Into<String>) -> Self {
        self.to_inst_id = Some(to_inst_id.into());
        self
    }

    /// Set whether this is a loan transfer.
    pub fn loan_transfer(mut self, loan_transfer: impl Into<String>) -> Self {
        self.loan_transfer = Some(loan_transfer.into());
        self
    }
}

/// Request body for [`Funding::withdrawal`].
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WithdrawalRequest {
    ccy: String,
    amt: String,
    dest: String,
    #[serde(rename = "toAddr")]
    to_addr: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    chain: Option<String>,
    #[serde(rename = "areaCode", skip_serializing_if = "Option::is_none")]
    area_code: Option<String>,
    #[serde(rename = "clientId", skip_serializing_if = "Option::is_none")]
    client_id: Option<String>,
    #[serde(rename = "toAddrType", skip_serializing_if = "Option::is_none")]
    to_addr_type: Option<String>,
}

impl WithdrawalRequest {
    /// Create a withdrawal request.
    pub fn new(
        ccy: impl Into<String>,
        amt: impl Into<String>,
        dest: impl Into<String>,
        to_addr: impl Into<String>,
    ) -> Self {
        Self {
            ccy: ccy.into(),
            amt: amt.into(),
            dest: dest.into(),
            to_addr: to_addr.into(),
            chain: None,
            area_code: None,
            client_id: None,
            to_addr_type: None,
        }
    }

    /// Set withdrawal chain.
    pub fn chain(mut self, chain: impl Into<String>) -> Self {
        self.chain = Some(chain.into());
        self
    }

    /// Set phone area code for internal withdrawals.
    pub fn area_code(mut self, area_code: impl Into<String>) -> Self {
        self.area_code = Some(area_code.into());
        self
    }

    /// Set client withdrawal ID.
    pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
        self.client_id = Some(client_id.into());
        self
    }

    /// Set address type.
    pub fn to_addr_type(mut self, to_addr_type: impl Into<String>) -> Self {
        self.to_addr_type = Some(to_addr_type.into());
        self
    }
}

/// Query parameters for [`Funding::get_deposit_history`].
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DepositHistoryRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    ccy: Option<String>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    deposit_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    state: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    after: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    before: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    limit: Option<u32>,
    #[serde(rename = "txId", skip_serializing_if = "Option::is_none")]
    tx_id: Option<String>,
    #[serde(rename = "depId", skip_serializing_if = "Option::is_none")]
    dep_id: Option<String>,
    #[serde(rename = "fromWdId", skip_serializing_if = "Option::is_none")]
    from_wd_id: Option<String>,
}

impl DepositHistoryRequest {
    /// Create an empty deposit-history query.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by currency.
    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
        self.ccy = Some(ccy.into());
        self
    }

    /// Filter by deposit type.
    pub fn deposit_type(mut self, deposit_type: impl Into<String>) -> Self {
        self.deposit_type = Some(deposit_type.into());
        self
    }

    /// Filter by state.
    pub fn state(mut self, state: impl Into<String>) -> Self {
        self.state = Some(state.into());
        self
    }

    /// Page after the given ID.
    pub fn after(mut self, after: impl Into<String>) -> Self {
        self.after = Some(after.into());
        self
    }

    /// Page before the given ID.
    pub fn before(mut self, before: impl Into<String>) -> Self {
        self.before = Some(before.into());
        self
    }

    /// Set result limit.
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Filter by transaction ID.
    pub fn tx_id(mut self, tx_id: impl Into<String>) -> Self {
        self.tx_id = Some(tx_id.into());
        self
    }

    /// Filter by deposit ID.
    pub fn dep_id(mut self, dep_id: impl Into<String>) -> Self {
        self.dep_id = Some(dep_id.into());
        self
    }

    /// Filter by source withdrawal ID.
    pub fn from_wd_id(mut self, from_wd_id: impl Into<String>) -> Self {
        self.from_wd_id = Some(from_wd_id.into());
        self
    }
}

/// Query parameters for [`Funding::get_withdrawal_history`].
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WithdrawalHistoryRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    ccy: Option<String>,
    #[serde(rename = "wdId", skip_serializing_if = "Option::is_none")]
    wd_id: Option<String>,
    #[serde(rename = "clientId", skip_serializing_if = "Option::is_none")]
    client_id: Option<String>,
    #[serde(rename = "txId", skip_serializing_if = "Option::is_none")]
    tx_id: Option<String>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    withdrawal_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    state: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    after: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    before: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    limit: Option<u32>,
    #[serde(rename = "toAddrType", skip_serializing_if = "Option::is_none")]
    to_addr_type: Option<String>,
}

impl WithdrawalHistoryRequest {
    /// Create an empty withdrawal-history query.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by currency.
    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
        self.ccy = Some(ccy.into());
        self
    }

    /// Filter by withdrawal ID.
    pub fn withdrawal_id(mut self, wd_id: impl Into<String>) -> Self {
        self.wd_id = Some(wd_id.into());
        self
    }

    /// Filter by client ID.
    pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
        self.client_id = Some(client_id.into());
        self
    }

    /// Filter by transaction ID.
    pub fn tx_id(mut self, tx_id: impl Into<String>) -> Self {
        self.tx_id = Some(tx_id.into());
        self
    }

    /// Filter by withdrawal type.
    pub fn withdrawal_type(mut self, withdrawal_type: impl Into<String>) -> Self {
        self.withdrawal_type = Some(withdrawal_type.into());
        self
    }

    /// Filter by state.
    pub fn state(mut self, state: impl Into<String>) -> Self {
        self.state = Some(state.into());
        self
    }

    /// Page after the given ID.
    pub fn after(mut self, after: impl Into<String>) -> Self {
        self.after = Some(after.into());
        self
    }

    /// Page before the given ID.
    pub fn before(mut self, before: impl Into<String>) -> Self {
        self.before = Some(before.into());
        self
    }

    /// Set result limit.
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Filter by destination address type.
    pub fn to_addr_type(mut self, to_addr_type: impl Into<String>) -> Self {
        self.to_addr_type = Some(to_addr_type.into());
        self
    }
}

/// Query parameters for [`Funding::get_bills`].
#[derive(Debug, Clone, Default, Serialize)]
pub struct FundingBillsRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    ccy: Option<String>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    bill_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    after: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    before: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    limit: Option<u32>,
}

impl FundingBillsRequest {
    /// Create an empty funding-bills query.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by currency.
    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
        self.ccy = Some(ccy.into());
        self
    }

    /// Filter by bill type.
    pub fn bill_type(mut self, bill_type: impl Into<String>) -> Self {
        self.bill_type = Some(bill_type.into());
        self
    }

    /// Page after the given bill ID.
    pub fn after(mut self, after: impl Into<String>) -> Self {
        self.after = Some(after.into());
        self
    }

    /// Page before the given bill ID.
    pub fn before(mut self, before: impl Into<String>) -> Self {
        self.before = Some(before.into());
        self
    }

    /// Set result limit.
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }
}

/// Query parameters for [`Funding::get_deposit_lightning`].
#[derive(Debug, Clone, Serialize)]
pub struct DepositLightningRequest {
    ccy: String,
    amt: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    to: Option<String>,
}

impl DepositLightningRequest {
    /// Create a Lightning deposit request.
    pub fn new(ccy: impl Into<String>, amt: impl Into<String>) -> Self {
        Self {
            ccy: ccy.into(),
            amt: amt.into(),
            to: None,
        }
    }

    /// Set recipient.
    pub fn to(mut self, to: impl Into<String>) -> Self {
        self.to = Some(to.into());
        self
    }
}

/// Request body for [`Funding::withdrawal_lightning`].
#[derive(Debug, Clone, Serialize)]
pub struct WithdrawalLightningRequest {
    ccy: String,
    invoice: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    memo: Option<String>,
}

impl WithdrawalLightningRequest {
    /// Create a Lightning withdrawal request.
    pub fn new(ccy: impl Into<String>, invoice: impl Into<String>) -> Self {
        Self {
            ccy: ccy.into(),
            invoice: invoice.into(),
            memo: None,
        }
    }

    /// Set withdrawal memo.
    pub fn memo(mut self, memo: impl Into<String>) -> Self {
        self.memo = Some(memo.into());
        self
    }
}

/// Query parameters for [`Funding::get_deposit_withdraw_status`].
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DepositWithdrawStatusRequest {
    #[serde(rename = "wdId", skip_serializing_if = "Option::is_none")]
    wd_id: Option<String>,
    #[serde(rename = "txId", skip_serializing_if = "Option::is_none")]
    tx_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    ccy: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    to: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    chain: Option<String>,
}

impl DepositWithdrawStatusRequest {
    /// Create an empty deposit/withdrawal-status query.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by withdrawal ID.
    pub fn withdrawal_id(mut self, wd_id: impl Into<String>) -> Self {
        self.wd_id = Some(wd_id.into());
        self
    }

    /// Filter by transaction ID.
    pub fn tx_id(mut self, tx_id: impl Into<String>) -> Self {
        self.tx_id = Some(tx_id.into());
        self
    }

    /// Filter by currency.
    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
        self.ccy = Some(ccy.into());
        self
    }

    /// Filter by destination address.
    pub fn to(mut self, to: impl Into<String>) -> Self {
        self.to = Some(to.into());
        self
    }

    /// Filter by chain.
    pub fn chain(mut self, chain: impl Into<String>) -> Self {
        self.chain = Some(chain.into());
        self
    }
}

/// Currency metadata and chain settings.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Currency {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Display name.
    #[serde(default)]
    pub name: String,
    /// Chain name.
    #[serde(default)]
    pub chain: String,
    /// Minimum withdrawal amount.
    #[serde(default, rename = "minWd")]
    pub min_wd: NumberString,
    /// Minimum deposit amount.
    #[serde(default, rename = "minDep")]
    pub min_dep: NumberString,
    /// Withdrawal fee.
    #[serde(default, rename = "minFee")]
    pub min_fee: NumberString,
    /// Whether deposit is enabled.
    #[serde(default, rename = "canDep")]
    pub can_dep: bool,
    /// Whether withdrawal is enabled.
    #[serde(default, rename = "canWd")]
    pub can_wd: bool,
    /// Whether internal transfer is enabled.
    #[serde(default, rename = "canInternal")]
    pub can_internal: bool,
}

/// Funding-account balance for one currency.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FundingBalance {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Total balance.
    #[serde(default)]
    pub bal: NumberString,
    /// Frozen balance.
    #[serde(default)]
    pub frozen_bal: NumberString,
    /// Available balance.
    #[serde(default)]
    pub avail_bal: NumberString,
}

/// Non-tradable asset row.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct NonTradableAsset {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Asset amount.
    #[serde(default)]
    pub amt: NumberString,
    /// Asset type.
    #[serde(default, rename = "type")]
    pub asset_type: String,
}

/// Deposit address for one currency/chain.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DepositAddress {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Chain name.
    #[serde(default)]
    pub chain: String,
    /// Deposit address.
    #[serde(default)]
    pub addr: String,
    /// Address tag, memo, or payment ID when required by the chain.
    #[serde(default)]
    pub tag: String,
    /// Selected account.
    #[serde(default)]
    pub selected: bool,
}

/// Funds-transfer result.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransferResult {
    /// Transfer ID.
    #[serde(default, rename = "transId")]
    pub trans_id: String,
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Transfer amount.
    #[serde(default)]
    pub amt: NumberString,
}

/// Funds-transfer state.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransferState {
    /// Transfer ID.
    #[serde(default, rename = "transId")]
    pub trans_id: String,
    /// Transfer state.
    #[serde(default)]
    pub state: String,
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Transfer amount.
    #[serde(default)]
    pub amt: NumberString,
    /// Source account.
    #[serde(default, rename = "from")]
    pub from_account: String,
    /// Destination account.
    #[serde(default)]
    pub to: String,
}

/// Withdrawal result.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WithdrawalResult {
    /// Withdrawal ID.
    #[serde(default, rename = "wdId")]
    pub wd_id: String,
    /// Client withdrawal ID.
    #[serde(default, rename = "clientId")]
    pub client_id: String,
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Withdrawal amount.
    #[serde(default)]
    pub amt: NumberString,
}

/// Deposit history row.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DepositRecord {
    /// Deposit ID.
    #[serde(default, rename = "depId")]
    pub dep_id: String,
    /// Transaction ID.
    #[serde(default, rename = "txId")]
    pub tx_id: String,
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Chain name.
    #[serde(default)]
    pub chain: String,
    /// Deposit amount.
    #[serde(default)]
    pub amt: NumberString,
    /// Deposit state.
    #[serde(default)]
    pub state: String,
    /// Timestamp.
    #[serde(default)]
    pub ts: NumberString,
}

/// Withdrawal history row.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WithdrawalRecord {
    /// Withdrawal ID.
    #[serde(default, rename = "wdId")]
    pub wd_id: String,
    /// Client withdrawal ID.
    #[serde(default, rename = "clientId")]
    pub client_id: String,
    /// Transaction ID.
    #[serde(default, rename = "txId")]
    pub tx_id: String,
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Chain name.
    #[serde(default)]
    pub chain: String,
    /// Withdrawal amount.
    #[serde(default)]
    pub amt: NumberString,
    /// Fee.
    #[serde(default)]
    pub fee: NumberString,
    /// Withdrawal state.
    #[serde(default)]
    pub state: String,
    /// Timestamp.
    #[serde(default)]
    pub ts: NumberString,
}

/// Funding-account bill row.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FundingBill {
    /// Bill ID.
    #[serde(default, rename = "billId")]
    pub bill_id: String,
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Balance change.
    #[serde(default)]
    pub bal_chg: NumberString,
    /// Balance after change.
    #[serde(default)]
    pub bal: NumberString,
    /// Bill type.
    #[serde(default, rename = "type")]
    pub bill_type: String,
    /// Timestamp.
    #[serde(default)]
    pub ts: NumberString,
}

/// Lightning deposit invoice.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DepositLightning {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Invoice amount.
    #[serde(default)]
    pub amt: NumberString,
    /// Lightning invoice.
    #[serde(default)]
    pub invoice: String,
    /// Recipient.
    #[serde(default)]
    pub to: String,
}

/// Lightning withdrawal result.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct WithdrawalLightning {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Withdrawal amount.
    #[serde(default)]
    pub amt: NumberString,
    /// Withdrawal ID.
    #[serde(default, rename = "wdId")]
    pub wd_id: String,
}

/// Total asset valuation.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AssetValuation {
    /// Valuation details by account area.
    #[serde(default)]
    pub details: AssetValuationDetails,
    /// Total balance in the requested valuation currency.
    #[serde(default, rename = "totalBal")]
    pub total_bal: NumberString,
}

/// Asset valuation details by account area.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AssetValuationDetails {
    /// Funding-account valuation.
    #[serde(default)]
    pub funding: NumberString,
    /// Trading-account valuation.
    #[serde(default)]
    pub trading: NumberString,
    /// Earn-account valuation.
    #[serde(default)]
    pub earn: NumberString,
    /// Classic-account valuation.
    #[serde(default)]
    pub classic: NumberString,
}

/// Deposit/withdrawal status row.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DepositWithdrawStatus {
    /// Withdrawal ID.
    #[serde(default, rename = "wdId")]
    pub wd_id: String,
    /// Transaction ID.
    #[serde(default, rename = "txId")]
    pub tx_id: String,
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Chain name.
    #[serde(default)]
    pub chain: String,
    /// Destination address.
    #[serde(default)]
    pub to: String,
    /// State.
    #[serde(default)]
    pub state: String,
}

/// Dust-conversion result.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ConvertDustAssetsResult {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Converted amount.
    #[serde(default)]
    pub amt: NumberString,
}

/// Purchase/redemption result.
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct PurchaseRedemptResult {
    /// Currency code.
    #[serde(default)]
    pub ccy: String,
    /// Amount.
    #[serde(default)]
    pub amt: NumberString,
    /// Operation side.
    #[serde(default)]
    pub side: String,
}

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

    #[test]
    fn funds_transfer_request_omits_unset_optional_fields() {
        let value =
            serde_json::to_value(FundsTransferRequest::new("USDT", "1", "6", "18")).unwrap();

        assert_eq!(value["ccy"], "USDT");
        assert_eq!(value["amt"], "1");
        assert_eq!(value["from"], "6");
        assert_eq!(value["to"], "18");
        assert!(value.get("subAcct").is_none());
        assert!(value.get("instId").is_none());
        assert!(value.get("loanTrans").is_none());
    }

    #[test]
    fn withdrawal_request_omits_unset_optional_fields() {
        let value =
            serde_json::to_value(WithdrawalRequest::new("USDT", "1", "3", "example")).unwrap();

        assert_eq!(value["ccy"], "USDT");
        assert_eq!(value["amt"], "1");
        assert_eq!(value["dest"], "3");
        assert_eq!(value["toAddr"], "example");
        assert!(value.get("chain").is_none());
        assert!(value.get("areaCode").is_none());
        assert!(value.get("toAddrType").is_none());
    }

    #[test]
    fn history_requests_omit_unset_optional_fields() {
        let deposit = serde_urlencoded::to_string(DepositHistoryRequest::new().limit(5)).unwrap();
        assert_eq!(deposit, "limit=5");

        let withdrawal =
            serde_urlencoded::to_string(WithdrawalHistoryRequest::new().to_addr_type("1")).unwrap();
        assert_eq!(withdrawal, "toAddrType=1");
    }

    #[test]
    fn deposit_withdraw_status_request_omits_unset_optional_fields() {
        let query = serde_urlencoded::to_string(
            DepositWithdrawStatusRequest::new()
                .currency("USDT")
                .chain("USDT-TRC20"),
        )
        .unwrap();

        assert_eq!(query, "ccy=USDT&chain=USDT-TRC20");
    }
}