koios_sdk/api/account.rs
1use crate::{
2 error::Result,
3 models::{
4 account::{
5 AccountAddresses, AccountAssets, AccountHistory, AccountInfo, AccountList,
6 AccountRewards, AccountUpdates,
7 },
8 address::AddressTransaction,
9 requests::{
10 StakeAddressesRequest, StakeAddressesWithEpochRequest,
11 StakeAddressesWithExtendedRequest, StakeAddressesWithFirstOnlyAndEmptyRequest,
12 },
13 UtxoInfo,
14 },
15 types::{AfterBlockHeight, StakeAddress},
16 Client,
17};
18
19impl Client {
20 /// Get a list of all stake addresses that have at least 1 transaction
21 ///
22 /// # Examples
23 ///
24 /// ```no_run
25 /// use koios_sdk::Client;
26 ///
27 /// #[tokio::main]
28 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
29 /// let client = Client::new()?;
30 /// let accounts = client.get_account_list().await?;
31 /// println!("Account list: {:?}", accounts);
32 /// Ok(())
33 /// }
34 /// ```
35 pub async fn get_account_list(&self) -> Result<Vec<AccountList>> {
36 self.get("/account_list").await
37 }
38
39 /// Get the account information for given stake addresses
40 ///
41 /// # Arguments
42 ///
43 /// * `stake_addresses` - List of stake addresses to query
44 ///
45 /// # Examples
46 ///
47 /// ```no_run
48 /// use koios_sdk::Client;
49 ///
50 /// #[tokio::main]
51 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
52 /// let client = Client::new()?;
53 /// let stake_addresses = vec![
54 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
55 /// ];
56 /// let account_info = client.get_account_info(&stake_addresses).await?;
57 /// println!("Account info: {:?}", account_info);
58 /// Ok(())
59 /// }
60 /// ```
61 pub async fn get_account_info(&self, stake_addresses: &[String]) -> Result<Vec<AccountInfo>> {
62 let request = StakeAddressesRequest::new(stake_addresses.to_vec());
63 self.post("/account_info", &request).await
64 }
65
66 /// Get the cached account information for given stake addresses
67 ///
68 /// # Arguments
69 ///
70 /// * `stake_addresses` - List of stake addresses to query
71 ///
72 /// # Examples
73 ///
74 /// ```no_run
75 /// use koios_sdk::Client;
76 ///
77 /// #[tokio::main]
78 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
79 /// let client = Client::new()?;
80 /// let stake_addresses = vec![
81 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
82 /// ];
83 /// let account_info = client.get_account_info_cached(&stake_addresses).await?;
84 /// println!("Cached account info: {:?}", account_info);
85 /// Ok(())
86 /// }
87 /// ```
88 pub async fn get_account_info_cached(
89 &self,
90 stake_addresses: &[String],
91 ) -> Result<Vec<AccountInfo>> {
92 let request: StakeAddressesRequest = StakeAddressesRequest::new(stake_addresses.to_vec());
93 self.post("/account_info_cached", &request).await
94 }
95
96 /// Get a list of all UTxOs for given stake addresses
97 ///
98 /// # Arguments
99 ///
100 /// * `stake_addresses` - List of stake addresses to query
101 /// * `extended` - Optional flag to include extended information
102 ///
103 /// # Examples
104 ///
105 /// ```no_run
106 /// use koios_sdk::Client;
107 ///
108 /// #[tokio::main]
109 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
110 /// let client = Client::new()?;
111 /// let stake_addresses = vec![
112 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
113 /// ];
114 /// let utxos = client.get_account_utxos(&stake_addresses, Some(true)).await?;
115 /// println!("Account UTXOs: {:?}", utxos);
116 /// Ok(())
117 /// }
118 /// ```
119 pub async fn get_account_utxos(
120 &self,
121 stake_addresses: &[String],
122 extended: Option<bool>,
123 ) -> Result<Vec<UtxoInfo>> {
124 let request = StakeAddressesWithExtendedRequest::new(stake_addresses.to_vec(), extended);
125 self.post("/account_utxos", &request).await
126 }
127
128 /// Get a list of all transactions for a given stake address
129 ///
130 /// # Arguments
131 ///
132 /// * `stake_address` - Stake address to query
133 /// * `after_block_height` - Optional block height to filter from
134 ///
135 /// # Examples
136 ///
137 /// ```no_run
138 /// use koios_sdk::Client;
139 /// use koios_sdk::types::{StakeAddress, AfterBlockHeight};
140 ///
141 /// #[tokio::main]
142 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
143 /// let client = Client::new()?;
144 /// let stake_address = "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7";
145 /// let txs = client.get_account_transactions(
146 /// StakeAddress::new(stake_address),
147 /// Some(AfterBlockHeight::new(42000000))
148 /// ).await?;
149 /// println!("Account transactions: {:?}", txs);
150 /// Ok(())
151 /// }
152 /// ```
153 pub async fn get_account_transactions(
154 &self,
155 stake_address: StakeAddress,
156 after_block_height: Option<AfterBlockHeight>,
157 ) -> Result<Vec<AddressTransaction>> {
158 // Encode stake address
159 let encoded_stake_addr = urlencoding::encode(stake_address.value()).to_string();
160
161 // Start building endpoint with encoded stake address
162 let mut endpoint = format!("/account_txs?_stake_address={}", encoded_stake_addr);
163
164 // Add encoded block height if present
165 if let Some(height) = after_block_height {
166 let encoded_height = urlencoding::encode(&height.value().to_string()).to_string();
167 endpoint.push_str(&format!("&_after_block_height={}", encoded_height));
168 }
169
170 self.get(&endpoint).await
171 }
172
173 /// Get the full rewards history for given stake addresses
174 ///
175 /// # Arguments
176 ///
177 /// * `stake_addresses` - List of stake addresses to query
178 /// * `epoch_no` - Optional epoch number to query from
179 ///
180 /// # Examples
181 ///
182 /// ```no_run
183 /// use koios_sdk::Client;
184 ///
185 /// #[tokio::main]
186 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
187 /// let client = Client::new()?;
188 /// let stake_addresses = vec![
189 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
190 /// ];
191 /// let epoch_no = Some("320".to_string());
192 /// let rewards = client.get_account_rewards(&stake_addresses, epoch_no).await?;
193 /// println!("Account rewards: {:?}", rewards);
194 /// Ok(())
195 /// }
196 /// ```
197 pub async fn get_account_rewards(
198 &self,
199 stake_addresses: &[String],
200 epoch_no: Option<String>,
201 ) -> Result<Vec<AccountRewards>> {
202 let request = StakeAddressesWithEpochRequest::new(
203 stake_addresses.to_vec(),
204 epoch_no.map(|e| e.parse().unwrap()),
205 );
206 self.post("/account_rewards", &request).await
207 }
208
209 /// Get the account updates for given stake addresses
210 ///
211 /// # Arguments
212 ///
213 /// * `stake_addresses` - List of stake addresses to query
214 ///
215 /// # Examples
216 ///
217 /// ```no_run
218 /// use koios_sdk::Client;
219 ///
220 /// #[tokio::main]
221 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
222 /// let client = Client::new()?;
223 /// let stake_addresses = vec![
224 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
225 /// ];
226 /// let updates = client.get_account_updates(&stake_addresses).await?;
227 /// println!("Account updates: {:?}", updates);
228 /// Ok(())
229 /// }
230 /// ```
231 pub async fn get_account_updates(
232 &self,
233 stake_addresses: &[String],
234 ) -> Result<Vec<AccountUpdates>> {
235 let request = StakeAddressesRequest::new(stake_addresses.to_vec());
236 self.post("/account_updates", &request).await
237 }
238
239 /// Get all addresses associated with given staking accounts
240 ///
241 /// # Arguments
242 ///
243 /// * `stake_addresses` - List of stake addresses to query
244 /// * `first_only` - Optional flag to return only the first address
245 /// * `empty` - Optional flag to include empty addresses
246 ///
247 /// # Examples
248 ///
249 /// ```no_run
250 /// use koios_sdk::Client;
251 ///
252 /// #[tokio::main]
253 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
254 /// let client = Client::new()?;
255 /// let stake_addresses = vec![
256 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
257 /// ];
258 /// let addresses = client.get_account_addresses(
259 /// &stake_addresses,
260 /// Some(true),
261 /// Some(false)
262 /// ).await?;
263 /// println!("Account addresses: {:?}", addresses);
264 /// Ok(())
265 /// }
266 /// ```
267 pub async fn get_account_addresses(
268 &self,
269 stake_addresses: &[String],
270 first_only: Option<bool>,
271 empty: Option<bool>,
272 ) -> Result<Vec<AccountAddresses>> {
273 let request = StakeAddressesWithFirstOnlyAndEmptyRequest::new(
274 stake_addresses.to_vec(),
275 first_only,
276 empty,
277 );
278 self.post("/account_addresses", &request).await
279 }
280
281 /// Get the native asset balance for given stake addresses
282 ///
283 /// # Arguments
284 ///
285 /// * `stake_addresses` - List of stake addresses to query
286 ///
287 /// # Examples
288 ///
289 /// ```no_run
290 /// use koios_sdk::Client;
291 ///
292 /// #[tokio::main]
293 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
294 /// let client = Client::new()?;
295 /// let stake_addresses = vec![
296 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
297 /// ];
298 /// let assets = client.get_account_assets(&stake_addresses).await?;
299 /// println!("Account assets: {:?}", assets);
300 /// Ok(())
301 /// }
302 /// ```
303 pub async fn get_account_assets(
304 &self,
305 stake_addresses: &[String],
306 ) -> Result<Vec<AccountAssets>> {
307 let request = StakeAddressesRequest::new(stake_addresses.to_vec());
308 self.post("/account_assets", &request).await
309 }
310
311 /// Get the staking history of given stake addresses
312 ///
313 /// # Arguments
314 ///
315 /// * `stake_addresses` - List of stake addresses to query
316 /// * `epoch_no` - Optional epoch number to query from
317 ///
318 /// # Examples
319 ///
320 /// ```no_run
321 /// use koios_sdk::Client;
322 ///
323 /// #[tokio::main]
324 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
325 /// let client = Client::new()?;
326 /// let stake_addresses = vec![
327 /// "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
328 /// ];
329 /// let epoch_no = Some("320".to_string());
330 /// let history = client.get_account_history(&stake_addresses, epoch_no).await?;
331 /// println!("Account history: {:?}", history);
332 /// Ok(())
333 /// }
334 /// ```
335 pub async fn get_account_history(
336 &self,
337 stake_addresses: &[String],
338 epoch_no: Option<String>,
339 ) -> Result<Vec<AccountHistory>> {
340 let request = StakeAddressesWithEpochRequest::new(
341 stake_addresses.to_vec(),
342 epoch_no.map(|e| e.parse().unwrap()),
343 );
344 self.post("/account_history", &request).await
345 }
346}