koios_sdk/api/
account.rs

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
use crate::{
    error::Result,
    models::{
        account::{
            AccountAddresses, AccountAssets, AccountHistory, AccountInfo, AccountList,
            AccountRewards, AccountUpdates,
        },
        address::AddressTransaction,
        requests::{
            StakeAddressesRequest, StakeAddressesWithEpochRequest,
            StakeAddressesWithExtendedRequest, StakeAddressesWithFirstOnlyAndEmptyRequest,
        },
        UtxoInfo,
    },
    types::{AfterBlockHeight, StakeAddress},
    Client,
};

impl Client {
    /// Get a list of all stake addresses that have at least 1 transaction
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let accounts = client.get_account_list().await?;
    ///     println!("Account list: {:?}", accounts);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_list(&self) -> Result<Vec<AccountList>> {
        self.get("/account_list").await
    }

    /// Get the account information for given stake addresses
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let account_info = client.get_account_info(&stake_addresses).await?;
    ///     println!("Account info: {:?}", account_info);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_info(&self, stake_addresses: &[String]) -> Result<Vec<AccountInfo>> {
        let request = StakeAddressesRequest::new(stake_addresses.to_vec());
        self.post("/account_info", &request).await
    }

    /// Get the cached account information for given stake addresses
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let account_info = client.get_account_info_cached(&stake_addresses).await?;
    ///     println!("Cached account info: {:?}", account_info);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_info_cached(
        &self,
        stake_addresses: &[String],
    ) -> Result<Vec<AccountInfo>> {
        let request: StakeAddressesRequest = StakeAddressesRequest::new(stake_addresses.to_vec());
        self.post("/account_info_cached", &request).await
    }

    /// Get a list of all UTxOs for given stake addresses
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    /// * `extended` - Optional flag to include extended information
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let utxos = client.get_account_utxos(&stake_addresses, Some(true)).await?;
    ///     println!("Account UTXOs: {:?}", utxos);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_utxos(
        &self,
        stake_addresses: &[String],
        extended: Option<bool>,
    ) -> Result<Vec<UtxoInfo>> {
        let request = StakeAddressesWithExtendedRequest::new(stake_addresses.to_vec(), extended);
        self.post("/account_utxos", &request).await
    }

    /// Get a list of all transactions for a given stake address
    ///
    /// # Arguments
    ///
    /// * `stake_address` - Stake address to query
    /// * `after_block_height` - Optional block height to filter from
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    /// use koios_sdk::types::{StakeAddress, AfterBlockHeight};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_address = "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7";
    ///     let txs = client.get_account_transactions(
    ///         StakeAddress::new(stake_address),
    ///         Some(AfterBlockHeight::new(42000000))
    ///     ).await?;
    ///     println!("Account transactions: {:?}", txs);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_transactions(
        &self,
        stake_address: StakeAddress,
        after_block_height: Option<AfterBlockHeight>,
    ) -> Result<Vec<AddressTransaction>> {
        // Encode stake address
        let encoded_stake_addr = urlencoding::encode(stake_address.value()).to_string();

        // Start building endpoint with encoded stake address
        let mut endpoint = format!("/account_txs?_stake_address={}", encoded_stake_addr);

        // Add encoded block height if present
        if let Some(height) = after_block_height {
            let encoded_height = urlencoding::encode(&height.value().to_string()).to_string();
            endpoint.push_str(&format!("&_after_block_height={}", encoded_height));
        }

        self.get(&endpoint).await
    }

    /// Get the full rewards history for given stake addresses
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    /// * `epoch_no` - Optional epoch number to query from
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let epoch_no = Some("320".to_string());
    ///     let rewards = client.get_account_rewards(&stake_addresses, epoch_no).await?;
    ///     println!("Account rewards: {:?}", rewards);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_rewards(
        &self,
        stake_addresses: &[String],
        epoch_no: Option<String>,
    ) -> Result<Vec<AccountRewards>> {
        let request = StakeAddressesWithEpochRequest::new(
            stake_addresses.to_vec(),
            epoch_no.map(|e| e.parse().unwrap()),
        );
        self.post("/account_rewards", &request).await
    }

    /// Get the account updates for given stake addresses
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let updates = client.get_account_updates(&stake_addresses).await?;
    ///     println!("Account updates: {:?}", updates);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_updates(
        &self,
        stake_addresses: &[String],
    ) -> Result<Vec<AccountUpdates>> {
        let request = StakeAddressesRequest::new(stake_addresses.to_vec());
        self.post("/account_updates", &request).await
    }

    /// Get all addresses associated with given staking accounts
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    /// * `first_only` - Optional flag to return only the first address
    /// * `empty` - Optional flag to include empty addresses
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let addresses = client.get_account_addresses(
    ///         &stake_addresses,
    ///         Some(true),
    ///         Some(false)
    ///     ).await?;
    ///     println!("Account addresses: {:?}", addresses);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_addresses(
        &self,
        stake_addresses: &[String],
        first_only: Option<bool>,
        empty: Option<bool>,
    ) -> Result<Vec<AccountAddresses>> {
        let request = StakeAddressesWithFirstOnlyAndEmptyRequest::new(
            stake_addresses.to_vec(),
            first_only,
            empty,
        );
        self.post("/account_addresses", &request).await
    }

    /// Get the native asset balance for given stake addresses
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let assets = client.get_account_assets(&stake_addresses).await?;
    ///     println!("Account assets: {:?}", assets);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_assets(
        &self,
        stake_addresses: &[String],
    ) -> Result<Vec<AccountAssets>> {
        let request = StakeAddressesRequest::new(stake_addresses.to_vec());
        self.post("/account_assets", &request).await
    }

    /// Get the staking history of given stake addresses
    ///
    /// # Arguments
    ///
    /// * `stake_addresses` - List of stake addresses to query
    /// * `epoch_no` - Optional epoch number to query from
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let stake_addresses = vec![
    ///         "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7".to_string()
    ///     ];
    ///     let epoch_no = Some("320".to_string());
    ///     let history = client.get_account_history(&stake_addresses, epoch_no).await?;
    ///     println!("Account history: {:?}", history);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_account_history(
        &self,
        stake_addresses: &[String],
        epoch_no: Option<String>,
    ) -> Result<Vec<AccountHistory>> {
        let request = StakeAddressesWithEpochRequest::new(
            stake_addresses.to_vec(),
            epoch_no.map(|e| e.parse().unwrap()),
        );
        self.post("/account_history", &request).await
    }
}