dfns_sdk_rs/api/wallets/
client.rs

1// @dfns-sdk-rs/src/api/wallets/client.rs
2
3use super::types::*;
4use crate::{
5    models::generic::DfnsApiClientOptions,
6    utils::{
7        fetch::simple_fetch,
8        url::{build_path_and_query, PathAndQueryParams},
9        user_action_fetch::user_action_fetch,
10    },
11};
12use serde_json::json;
13use std::collections::HashMap;
14
15#[derive(Debug, Clone, PartialEq)]
16pub struct WalletsClient {
17    api_options: DfnsApiClientOptions,
18}
19
20impl WalletsClient {
21    pub fn new(api_options: DfnsApiClientOptions) -> Self {
22        Self { api_options }
23    }
24
25    pub async fn broadcast_transaction(
26        &self,
27        request: BroadcastTransactionRequest,
28    ) -> Result<BroadcastTransactionResponse, crate::error::DfnsError> {
29        let mut path_params = HashMap::new();
30        path_params.insert("walletId".to_string(), request.wallet_id.clone());
31
32        let path = build_path_and_query(
33            "/wallets/:walletId/transactions",
34            &PathAndQueryParams {
35                path: path_params,
36                query: HashMap::new(),
37            },
38        );
39
40        user_action_fetch(
41            &path,
42            crate::utils::fetch::FetchOptions {
43                method: crate::utils::fetch::HttpMethod::POST,
44                headers: None,
45                body: Some(json!(request.body)),
46                api_options: self.api_options.clone(),
47            },
48        )
49        .await
50    }
51
52    pub async fn create_wallet(
53        &self,
54        request: CreateWalletRequest,
55    ) -> Result<CreateWalletResponse, crate::error::DfnsError> {
56        let path = build_path_and_query(
57            "/wallets",
58            &PathAndQueryParams {
59                path: HashMap::new(),
60                query: HashMap::new(),
61            },
62        );
63
64        user_action_fetch(
65            &path,
66            crate::utils::fetch::FetchOptions {
67                method: crate::utils::fetch::HttpMethod::POST,
68                headers: None,
69                body: Some(json!(request.body)),
70                api_options: self.api_options.clone(),
71            },
72        )
73        .await
74    }
75
76    pub async fn delegate_wallet(
77        &self,
78        request: DelegateWalletRequest,
79    ) -> Result<DelegateWalletResponse, crate::error::DfnsError> {
80        let mut path_params = HashMap::new();
81        path_params.insert("walletId".to_string(), request.wallet_id.clone());
82
83        let path = build_path_and_query(
84            "/wallets/:walletId/delegate",
85            &PathAndQueryParams {
86                path: path_params,
87                query: HashMap::new(),
88            },
89        );
90
91        user_action_fetch(
92            &path,
93            crate::utils::fetch::FetchOptions {
94                method: crate::utils::fetch::HttpMethod::POST,
95                headers: None,
96                body: Some(json!(request.body)),
97                api_options: self.api_options.clone(),
98            },
99        )
100        .await
101    }
102
103    pub async fn export_wallet(
104        &self,
105        request: ExportWalletRequest,
106    ) -> Result<ExportWalletResponse, crate::error::DfnsError> {
107        let mut path_params = HashMap::new();
108        path_params.insert("walletId".to_string(), request.wallet_id.clone());
109
110        let path = build_path_and_query(
111            "/wallets/:walletId/export",
112            &PathAndQueryParams {
113                path: path_params,
114                query: HashMap::new(),
115            },
116        );
117
118        user_action_fetch(
119            &path,
120            crate::utils::fetch::FetchOptions {
121                method: crate::utils::fetch::HttpMethod::POST,
122                headers: None,
123                body: Some(json!(request.body)),
124                api_options: self.api_options.clone(),
125            },
126        )
127        .await
128    }
129
130    pub async fn generate_signature(
131        &self,
132        request: GenerateSignatureRequest,
133    ) -> Result<GenerateSignatureResponse, crate::error::DfnsError> {
134        let mut path_params = HashMap::new();
135        path_params.insert("walletId".to_string(), request.wallet_id.clone());
136
137        let path = build_path_and_query(
138            "/wallets/:walletId/signatures",
139            &PathAndQueryParams {
140                path: path_params,
141                query: HashMap::new(),
142            },
143        );
144
145        user_action_fetch(
146            &path,
147            crate::utils::fetch::FetchOptions {
148                method: crate::utils::fetch::HttpMethod::POST,
149                headers: None,
150                body: Some(json!(request.body)),
151                api_options: self.api_options.clone(),
152            },
153        )
154        .await
155    }
156
157    pub async fn get_signature(
158        &self,
159        request: GetSignatureRequest,
160    ) -> Result<GetSignatureResponse, crate::error::DfnsError> {
161        let mut path_params = HashMap::new();
162        path_params.insert("walletId".to_string(), request.wallet_id.clone());
163        path_params.insert("signatureId".to_string(), request.signature_id.clone());
164
165        let path = build_path_and_query(
166            "/wallets/:walletId/signatures/:signatureId",
167            &PathAndQueryParams {
168                path: path_params,
169                query: HashMap::new(),
170            },
171        );
172
173        simple_fetch(
174            &path,
175            crate::utils::fetch::FetchOptions {
176                method: crate::utils::fetch::HttpMethod::GET,
177                headers: None,
178                body: None,
179                api_options: self.api_options.base.clone(),
180            },
181        )
182        .await
183    }
184
185    pub async fn get_transaction(
186        &self,
187        request: GetTransactionRequest,
188    ) -> Result<GetTransactionResponse, crate::error::DfnsError> {
189        let mut path_params = HashMap::new();
190        path_params.insert("walletId".to_string(), request.wallet_id.clone());
191        path_params.insert("transactionId".to_string(), request.transaction_id.clone());
192
193        let path = build_path_and_query(
194            "/wallets/:walletId/transactions/:transactionId",
195            &PathAndQueryParams {
196                path: path_params,
197                query: HashMap::new(),
198            },
199        );
200
201        simple_fetch(
202            &path,
203            crate::utils::fetch::FetchOptions {
204                method: crate::utils::fetch::HttpMethod::GET,
205                headers: None,
206                body: None,
207                api_options: self.api_options.base.clone(),
208            },
209        )
210        .await
211    }
212
213    pub async fn get_transfer(
214        &self,
215        request: GetTransferRequest,
216    ) -> Result<GetTransferResponse, crate::error::DfnsError> {
217        let mut path_params = HashMap::new();
218        path_params.insert("walletId".to_string(), request.wallet_id.clone());
219        path_params.insert("transferId".to_string(), request.transfer_id.clone());
220
221        let path = build_path_and_query(
222            "/wallets/:walletId/transfers/:transferId",
223            &PathAndQueryParams {
224                path: path_params,
225                query: HashMap::new(),
226            },
227        );
228
229        simple_fetch(
230            &path,
231            crate::utils::fetch::FetchOptions {
232                method: crate::utils::fetch::HttpMethod::GET,
233                headers: None,
234                body: None,
235                api_options: self.api_options.base.clone(),
236            },
237        )
238        .await
239    }
240
241    pub async fn get_wallet(
242        &self,
243        request: GetWalletRequest,
244    ) -> Result<GetWalletResponse, crate::error::DfnsError> {
245        let mut path_params = HashMap::new();
246        path_params.insert("walletId".to_string(), request.wallet_id.clone());
247
248        let path = build_path_and_query(
249            "/wallets/:walletId",
250            &PathAndQueryParams {
251                path: path_params,
252                query: HashMap::new(),
253            },
254        );
255
256        simple_fetch(
257            &path,
258            crate::utils::fetch::FetchOptions {
259                method: crate::utils::fetch::HttpMethod::GET,
260                headers: None,
261                body: None,
262                api_options: self.api_options.base.clone(),
263            },
264        )
265        .await
266    }
267
268    pub async fn get_wallet_assets(
269        &self,
270        request: GetWalletAssetsRequest,
271    ) -> Result<GetWalletAssetsResponse, crate::error::DfnsError> {
272        let mut path_params = HashMap::new();
273        path_params.insert("walletId".to_string(), request.wallet_id.clone());
274
275        let mut query_params = HashMap::new();
276        if let Some(query) = request.query {
277            if let Some(net_worth) = query.net_worth {
278                query_params.insert("netWorth".to_string(), net_worth.to_string());
279            }
280        }
281
282        let path = build_path_and_query(
283            "/wallets/:walletId/assets",
284            &PathAndQueryParams {
285                path: path_params,
286                query: query_params,
287            },
288        );
289
290        simple_fetch(
291            &path,
292            crate::utils::fetch::FetchOptions {
293                method: crate::utils::fetch::HttpMethod::GET,
294                headers: None,
295                body: None,
296                api_options: self.api_options.base.clone(),
297            },
298        )
299        .await
300    }
301
302    pub async fn get_wallet_history(
303        &self,
304        request: GetWalletHistoryRequest,
305    ) -> Result<GetWalletHistoryResponse, crate::error::DfnsError> {
306        let mut path_params = HashMap::new();
307        path_params.insert("walletId".to_string(), request.wallet_id.clone());
308
309        let mut query_params = HashMap::new();
310        if let Some(query) = request.query {
311            if let Some(contract) = query.contract {
312                query_params.insert("contract".to_string(), contract);
313            }
314            if let Some(direction) = query.direction {
315                query_params.insert("direction".to_string(), direction.to_string());
316            }
317            if let Some(kind) = query.kind {
318                query_params.insert("kind".to_string(), kind.to_string());
319            }
320            if let Some(limit) = query.limit {
321                query_params.insert("limit".to_string(), limit);
322            }
323            if let Some(token) = query.pagination_token {
324                query_params.insert("paginationToken".to_string(), token);
325            }
326        }
327
328        let path = build_path_and_query(
329            "/wallets/:walletId/history",
330            &PathAndQueryParams {
331                path: path_params,
332                query: query_params,
333            },
334        );
335
336        simple_fetch(
337            &path,
338            crate::utils::fetch::FetchOptions {
339                method: crate::utils::fetch::HttpMethod::GET,
340                headers: None,
341                body: None,
342                api_options: self.api_options.base.clone(),
343            },
344        )
345        .await
346    }
347
348    pub async fn get_wallet_nfts(
349        &self,
350        request: GetWalletNftsRequest,
351    ) -> Result<GetWalletNftsResponse, crate::error::DfnsError> {
352        let mut path_params = HashMap::new();
353        path_params.insert("walletId".to_string(), request.wallet_id.clone());
354
355        let path = build_path_and_query(
356            "/wallets/:walletId/nfts",
357            &PathAndQueryParams {
358                path: path_params,
359                query: HashMap::new(),
360            },
361        );
362
363        simple_fetch(
364            &path,
365            crate::utils::fetch::FetchOptions {
366                method: crate::utils::fetch::HttpMethod::GET,
367                headers: None,
368                body: None,
369                api_options: self.api_options.base.clone(),
370            },
371        )
372        .await
373    }
374
375    pub async fn import_wallet(
376        &self,
377        request: ImportWalletRequest,
378    ) -> Result<ImportWalletResponse, crate::error::DfnsError> {
379        let path = build_path_and_query(
380            "/wallets/import",
381            &PathAndQueryParams {
382                path: HashMap::new(),
383                query: HashMap::new(),
384            },
385        );
386
387        user_action_fetch(
388            &path,
389            crate::utils::fetch::FetchOptions {
390                method: crate::utils::fetch::HttpMethod::POST,
391                headers: None,
392                body: Some(json!(request.body)),
393                api_options: self.api_options.clone(),
394            },
395        )
396        .await
397    }
398
399    pub async fn list_signatures(
400        &self,
401        request: ListSignaturesRequest,
402    ) -> Result<ListSignaturesResponse, crate::error::DfnsError> {
403        let mut path_params = HashMap::new();
404        path_params.insert("walletId".to_string(), request.wallet_id.clone());
405
406        let mut query_params = HashMap::new();
407        if let Some(query) = request.query {
408            if let Some(limit) = query.limit {
409                query_params.insert("limit".to_string(), limit);
410            }
411            if let Some(token) = query.pagination_token {
412                query_params.insert("paginationToken".to_string(), token);
413            }
414        }
415
416        let path = build_path_and_query(
417            "/wallets/:walletId/signatures",
418            &PathAndQueryParams {
419                path: path_params,
420                query: query_params,
421            },
422        );
423
424        simple_fetch(
425            &path,
426            crate::utils::fetch::FetchOptions {
427                method: crate::utils::fetch::HttpMethod::GET,
428                headers: None,
429                body: None,
430                api_options: self.api_options.base.clone(),
431            },
432        )
433        .await
434    }
435
436    pub async fn list_transactions(
437        &self,
438        request: ListTransactionsRequest,
439    ) -> Result<ListTransactionsResponse, crate::error::DfnsError> {
440        let mut path_params = HashMap::new();
441        path_params.insert("walletId".to_string(), request.wallet_id.clone());
442
443        let mut query_params = HashMap::new();
444        if let Some(query) = request.query {
445            if let Some(limit) = query.limit {
446                query_params.insert("limit".to_string(), limit);
447            }
448            if let Some(token) = query.pagination_token {
449                query_params.insert("paginationToken".to_string(), token);
450            }
451        }
452
453        let path = build_path_and_query(
454            "/wallets/:walletId/transactions",
455            &PathAndQueryParams {
456                path: path_params,
457                query: query_params,
458            },
459        );
460
461        simple_fetch(
462            &path,
463            crate::utils::fetch::FetchOptions {
464                method: crate::utils::fetch::HttpMethod::GET,
465                headers: None,
466                body: None,
467                api_options: self.api_options.base.clone(),
468            },
469        )
470        .await
471    }
472
473    pub async fn list_transfers(
474        &self,
475        request: ListTransfersRequest,
476    ) -> Result<ListTransfersResponse, crate::error::DfnsError> {
477        let mut path_params = HashMap::new();
478        path_params.insert("walletId".to_string(), request.wallet_id.clone());
479
480        let mut query_params = HashMap::new();
481        if let Some(query) = request.query {
482            if let Some(limit) = query.limit {
483                query_params.insert("limit".to_string(), limit);
484            }
485            if let Some(token) = query.pagination_token {
486                query_params.insert("paginationToken".to_string(), token);
487            }
488        }
489
490        let path = build_path_and_query(
491            "/wallets/:walletId/transfers",
492            &PathAndQueryParams {
493                path: path_params,
494                query: query_params,
495            },
496        );
497
498        simple_fetch(
499            &path,
500            crate::utils::fetch::FetchOptions {
501                method: crate::utils::fetch::HttpMethod::GET,
502                headers: None,
503                body: None,
504                api_options: self.api_options.base.clone(),
505            },
506        )
507        .await
508    }
509
510    pub async fn list_wallets(
511        &self,
512        request: Option<ListWalletsRequest>,
513    ) -> Result<ListWalletsResponse, crate::error::DfnsError> {
514        let mut query_params = HashMap::new();
515        if let Some(req) = request {
516            if let Some(query) = req.query {
517                if let Some(limit) = query.limit {
518                    query_params.insert("limit".to_string(), limit);
519                }
520                if let Some(owner_id) = query.owner_id {
521                    query_params.insert("ownerId".to_string(), owner_id);
522                }
523                if let Some(owner_username) = query.owner_username {
524                    query_params.insert("ownerUsername".to_string(), owner_username);
525                }
526                if let Some(token) = query.pagination_token {
527                    query_params.insert("paginationToken".to_string(), token);
528                }
529            }
530        }
531
532        let path = build_path_and_query(
533            "/wallets",
534            &PathAndQueryParams {
535                path: HashMap::new(),
536                query: query_params,
537            },
538        );
539
540        simple_fetch(
541            &path,
542            crate::utils::fetch::FetchOptions {
543                method: crate::utils::fetch::HttpMethod::GET,
544                headers: None,
545                body: None,
546                api_options: self.api_options.base.clone(),
547            },
548        )
549        .await
550    }
551
552    pub async fn tag_wallet(
553        &self,
554        request: TagWalletRequest,
555    ) -> Result<TagWalletResponse, crate::error::DfnsError> {
556        let mut path_params = HashMap::new();
557        path_params.insert("walletId".to_string(), request.wallet_id.clone());
558
559        let path = build_path_and_query(
560            "/wallets/:walletId/tags",
561            &PathAndQueryParams {
562                path: path_params,
563                query: HashMap::new(),
564            },
565        );
566
567        user_action_fetch(
568            &path,
569            crate::utils::fetch::FetchOptions {
570                method: crate::utils::fetch::HttpMethod::PUT,
571                headers: None,
572                body: Some(json!(request.body)),
573                api_options: self.api_options.clone(),
574            },
575        )
576        .await
577    }
578
579    pub async fn transfer_asset(
580        &self,
581        request: TransferAssetRequest,
582    ) -> Result<TransferAssetResponse, crate::error::DfnsError> {
583        let mut path_params = HashMap::new();
584        path_params.insert("walletId".to_string(), request.wallet_id.clone());
585
586        let path = build_path_and_query(
587            "/wallets/:walletId/transfers",
588            &PathAndQueryParams {
589                path: path_params,
590                query: HashMap::new(),
591            },
592        );
593
594        user_action_fetch(
595            &path,
596            crate::utils::fetch::FetchOptions {
597                method: crate::utils::fetch::HttpMethod::POST,
598                headers: None,
599                body: Some(json!(request.body)),
600                api_options: self.api_options.clone(),
601            },
602        )
603        .await
604    }
605
606    pub async fn untag_wallet(
607        &self,
608        request: UntagWalletRequest,
609    ) -> Result<UntagWalletResponse, crate::error::DfnsError> {
610        let mut path_params = HashMap::new();
611        path_params.insert("walletId".to_string(), request.wallet_id.clone());
612
613        let path = build_path_and_query(
614            "/wallets/:walletId/tags",
615            &PathAndQueryParams {
616                path: path_params,
617                query: HashMap::new(),
618            },
619        );
620
621        user_action_fetch(
622            &path,
623            crate::utils::fetch::FetchOptions {
624                method: crate::utils::fetch::HttpMethod::DELETE,
625                headers: None,
626                body: Some(json!(request.body)),
627                api_options: self.api_options.clone(),
628            },
629        )
630        .await
631    }
632
633    pub async fn update_wallet(
634        &self,
635        request: UpdateWalletRequest,
636    ) -> Result<UpdateWalletResponse, crate::error::DfnsError> {
637        let mut path_params = HashMap::new();
638        path_params.insert("walletId".to_string(), request.wallet_id.clone());
639
640        let path = build_path_and_query(
641            "/wallets/:walletId",
642            &PathAndQueryParams {
643                path: path_params,
644                query: HashMap::new(),
645            },
646        );
647
648        user_action_fetch(
649            &path,
650            crate::utils::fetch::FetchOptions {
651                method: crate::utils::fetch::HttpMethod::PUT,
652                headers: None,
653                body: Some(json!(request.body)),
654                api_options: self.api_options.clone(),
655            },
656        )
657        .await
658    }
659}