dfns_sdk_rs/api/staking/
client.rs

1// @dfns-sdk-rs/src/api/staking/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 StakingClient {
17    api_options: DfnsApiClientOptions,
18}
19
20impl StakingClient {
21    pub fn new(api_options: DfnsApiClientOptions) -> Self {
22        Self { api_options }
23    }
24
25    pub async fn create_stake(
26        &self,
27        request: CreateStakeRequest,
28    ) -> Result<CreateStakeResponse, crate::error::DfnsError> {
29        let path = build_path_and_query(
30            "/staking/stakes",
31            &PathAndQueryParams {
32                path: HashMap::new(),
33                query: HashMap::new(),
34            },
35        );
36
37        user_action_fetch(
38            &path,
39            crate::utils::fetch::FetchOptions {
40                method: crate::utils::fetch::HttpMethod::POST,
41                headers: None,
42                body: Some(json!(request.body)),
43                api_options: self.api_options.clone(),
44            },
45        )
46        .await
47    }
48
49    pub async fn create_stake_action(
50        &self,
51        request: CreateStakeActionRequest,
52    ) -> Result<CreateStakeActionResponse, crate::error::DfnsError> {
53        let mut path_params = HashMap::new();
54        path_params.insert("stakeId".to_string(), request.stake_id.clone());
55
56        let path = build_path_and_query(
57            "/staking/stakes/:stakeId/actions",
58            &PathAndQueryParams {
59                path: path_params,
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 get_stake_rewards(
77        &self,
78        request: GetStakeRewardsRequest,
79    ) -> Result<GetStakeRewardsResponse, crate::error::DfnsError> {
80        let mut path_params = HashMap::new();
81        path_params.insert("stakeId".to_string(), request.stake_id.clone());
82
83        let path = build_path_and_query(
84            "/staking/stakes/:stakeId/rewards",
85            &PathAndQueryParams {
86                path: path_params,
87                query: HashMap::new(),
88            },
89        );
90
91        simple_fetch(
92            &path,
93            crate::utils::fetch::FetchOptions {
94                method: crate::utils::fetch::HttpMethod::GET,
95                headers: None,
96                body: None,
97                api_options: self.api_options.base.clone(),
98            },
99        )
100        .await
101    }
102
103    pub async fn list_stake_actions(
104        &self,
105        request: Option<ListStakeActionsRequest>,
106    ) -> Result<ListStakeActionsResponse, crate::error::DfnsError> {
107        let mut query_params = HashMap::new();
108        if let Some(req) = request {
109            if let Some(query) = req.query {
110                if let Some(limit) = query.limit {
111                    query_params.insert("limit".to_string(), limit.to_string());
112                }
113                if let Some(token) = query.pagination_token {
114                    query_params.insert("paginationToken".to_string(), token);
115                }
116            }
117        }
118
119        let path = build_path_and_query(
120            "/staking/stakes/:stakeId/actions",
121            &PathAndQueryParams {
122                path: HashMap::new(),
123                query: query_params,
124            },
125        );
126
127        simple_fetch(
128            &path,
129            crate::utils::fetch::FetchOptions {
130                method: crate::utils::fetch::HttpMethod::GET,
131                headers: None,
132                body: None,
133                api_options: self.api_options.base.clone(),
134            },
135        )
136        .await
137    }
138
139    pub async fn list_stakes(
140        &self,
141        request: Option<ListStakesRequest>,
142    ) -> Result<ListStakesResponse, crate::error::DfnsError> {
143        let mut query_params = HashMap::new();
144        if let Some(req) = request {
145            if let Some(query) = req.query {
146                if let Some(limit) = query.limit {
147                    query_params.insert("limit".to_string(), limit.to_string());
148                }
149                if let Some(token) = query.pagination_token {
150                    query_params.insert("paginationToken".to_string(), token);
151                }
152            }
153        }
154
155        let path = build_path_and_query(
156            "/staking/stakes",
157            &PathAndQueryParams {
158                path: HashMap::new(),
159                query: query_params,
160            },
161        );
162
163        simple_fetch(
164            &path,
165            crate::utils::fetch::FetchOptions {
166                method: crate::utils::fetch::HttpMethod::GET,
167                headers: None,
168                body: None,
169                api_options: self.api_options.base.clone(),
170            },
171        )
172        .await
173    }
174}