iota-sdk 2.0.0-beta.1

The IOTA SDK provides developers with a seamless experience to develop on IOTA by providing account abstractions and clients to interact with node APIs.
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
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Node core API routes.

use packable::PackableExt;
use serde::{Deserialize, Serialize};
use url::Url;

use crate::{
    client::{
        constants::{DEFAULT_API_TIMEOUT, DEFAULT_USER_AGENT},
        node_api::query_tuples_to_query_string,
        node_manager::node::{Node, NodeAuth},
        Client, ClientError, ClientInner,
    },
    types::{
        api::core::{
            BlockMetadataResponse, BlockWithMetadataResponse, CommitteeResponse, CongestionResponse, InfoResponse,
            IssuanceBlockHeaderResponse, ManaRewardsResponse, NetworkMetricsResponse, OutputResponse,
            OutputWithMetadataResponse, PermanodeInfoResponse, RoutesResponse, SubmitBlockResponse,
            TransactionMetadataResponse, UtxoChangesFullResponse, UtxoChangesResponse, ValidatorResponse,
            ValidatorsResponse,
        },
        block::{
            address::ToBech32Ext,
            output::{AccountId, OutputId, OutputMetadata},
            payload::signed_transaction::TransactionId,
            slot::{EpochIndex, SlotCommitment, SlotCommitmentId, SlotIndex},
            Block, BlockDto, BlockId,
        },
        TryFromDto,
    },
};

/// Info path is the exact path extension for node APIs to request their info.
pub(crate) static INFO_PATH: &str = "api/core/v3/info";

/// Contains the info and the url from the node (useful when multiple nodes are used)
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NodeInfoResponse {
    /// The returned info
    pub info: InfoResponse,
    /// The url from the node which returned the info
    pub url: String,
}

impl ClientInner {
    // Node routes.

    /// Returns the health of the node.
    /// GET /health
    pub async fn get_health(&self, url: &str) -> Result<bool, ClientError> {
        const PATH: &str = "health";

        let mut url = Url::parse(url)?;
        url.set_path(PATH);
        let status = crate::client::node_manager::http_client::HttpClient::new(DEFAULT_USER_AGENT.to_string())
            .get(
                &Node {
                    url,
                    auth: None,
                    disabled: false,
                    permanode: false,
                },
                DEFAULT_API_TIMEOUT,
            )
            .await?
            .status();

        match status {
            200 => Ok(true),
            _ => Ok(false),
        }
    }

    /// Returns the available API route groups of the node.
    /// GET /api/routes
    pub async fn get_routes(&self) -> Result<RoutesResponse, ClientError> {
        const PATH: &str = "api/routes";

        self.get_request(PATH, None, false).await
    }

    /// Returns general information about the node.
    /// GET /api/core/v3/info
    pub async fn get_node_info(&self) -> Result<NodeInfoResponse, ClientError> {
        self.get_request(INFO_PATH, None, false).await
    }

    /// Returns network metrics.
    /// GET /api/core/v3/network/metrics
    pub async fn get_network_metrics(&self) -> Result<NetworkMetricsResponse, ClientError> {
        const PATH: &str = "api/core/v3/network/metrics";

        self.get_request(PATH, None, false).await
    }

    /// Returns whether the network is healthy (finalization is not delayed).
    /// GET /api/core/v3/network/health
    pub async fn get_network_health(&self) -> Result<bool, ClientError> {
        const PATH: &str = "api/core/v3/network/health";

        let nodes = self.node_manager.read().await.get_nodes(PATH, None)?;
        let client = crate::client::node_manager::http_client::HttpClient::new(DEFAULT_USER_AGENT.to_string());

        for node in &nodes {
            if let Ok(res) = client.get(node, DEFAULT_API_TIMEOUT).await {
                if res.status() == 200 {
                    return Ok(true);
                }
            }
        }

        Ok(false)
    }
}

impl Client {
    // Accounts routes.

    /// Checks if the account is ready to issue a block.
    /// GET /api/core/v3/accounts/{bech32Address}/congestion
    pub async fn get_account_congestion(
        &self,
        account_id: &AccountId,
        work_score: impl Into<Option<u32>> + Send,
    ) -> Result<CongestionResponse, ClientError> {
        let bech32_address = account_id.to_bech32(self.get_bech32_hrp().await?);
        let path = &format!("api/core/v3/accounts/{bech32_address}/congestion");
        let query = query_tuples_to_query_string([work_score.into().map(|s| ("workScore", s.to_string()))]);

        self.get_request(path, query.as_deref(), false).await
    }

    // Rewards routes.

    /// Returns the total available Mana rewards of an account or delegation output decayed up to `epochEnd` index
    /// provided in the response.
    /// Note that rewards for an epoch only become available at the beginning of the next epoch. If the end epoch of a
    /// staking feature is equal or greater than the current epoch, the rewards response will not include the potential
    /// future rewards for those epochs. `epochStart` and `epochEnd` indicates the actual range for which reward value
    /// is returned and decayed for.
    /// GET /api/core/v3/rewards/{outputId}
    pub async fn get_output_mana_rewards(
        &self,
        output_id: &OutputId,
        slot: impl Into<Option<SlotIndex>> + Send,
    ) -> Result<ManaRewardsResponse, ClientError> {
        let path = &format!("api/core/v3/rewards/{output_id}");
        let query = query_tuples_to_query_string([slot.into().map(|i| ("slot", i.to_string()))]);

        self.get_request(path, query.as_deref(), false).await
    }

    // Validators routes.

    /// Returns information of all stakers (registered validators) and if they are active, ordered by their holding
    /// stake.
    /// GET /api/core/v3/validators
    pub async fn get_validators(
        &self,
        page_size: impl Into<Option<u32>> + Send,
        cursor: impl Into<Option<String>> + Send,
    ) -> Result<ValidatorsResponse, ClientError> {
        const PATH: &str = "api/core/v3/validators";
        let query = query_tuples_to_query_string([
            page_size.into().map(|n| ("pageSize", n.to_string())),
            cursor.into().map(|c| ("cursor", c)),
        ]);

        self.get_request(PATH, query.as_deref(), false).await
    }

    /// Return information about a staker (registered validator).
    /// GET /api/core/v3/validators/{bech32Address}
    pub async fn get_validator(&self, account_id: &AccountId) -> Result<ValidatorResponse, ClientError> {
        let bech32_address = account_id.to_bech32(self.get_bech32_hrp().await?);
        let path = &format!("api/core/v3/validators/{bech32_address}");

        self.get_request(path, None, false).await
    }

    // Committee routes.

    /// Returns the information of committee members at the given epoch index. If epoch index is not provided, the
    /// current committee members are returned.
    /// GET /api/core/v3/committee/?epoch
    pub async fn get_committee(
        &self,
        epoch: impl Into<Option<EpochIndex>> + Send,
    ) -> Result<CommitteeResponse, ClientError> {
        const PATH: &str = "api/core/v3/committee";
        let query = query_tuples_to_query_string([epoch.into().map(|i| ("epoch", i.to_string()))]);

        self.get_request(PATH, query.as_deref(), false).await
    }

    // Blocks routes.

    /// Returns information that is ideal for attaching a block in the network.
    /// GET /api/core/v3/blocks/issuance
    pub async fn get_issuance(&self) -> Result<IssuanceBlockHeaderResponse, ClientError> {
        const PATH: &str = "api/core/v3/blocks/issuance";

        self.get_request(PATH, None, false).await
    }

    /// Returns the BlockId of the submitted block.
    /// POST /api/core/v3/blocks
    pub async fn post_block(&self, block: &Block) -> Result<BlockId, ClientError> {
        const PATH: &str = "api/core/v3/blocks";

        let block_dto = BlockDto::from(block);

        let response = self
            .post_request::<SubmitBlockResponse>(PATH, serde_json::to_value(block_dto)?)
            .await?;

        Ok(response.block_id)
    }

    /// Returns the BlockId of the submitted block.
    /// POST /api/core/v3/blocks
    pub async fn post_block_raw(&self, block: &Block) -> Result<BlockId, ClientError> {
        const PATH: &str = "api/core/v3/blocks";

        let response = self
            .post_request_bytes::<SubmitBlockResponse>(PATH, &block.pack_to_vec())
            .await?;

        Ok(response.block_id)
    }

    /// Finds a block by its ID and returns it as object.
    /// GET /api/core/v3/blocks/{blockId}
    pub async fn get_block(&self, block_id: &BlockId) -> Result<Block, ClientError> {
        let path = &format!("api/core/v3/blocks/{block_id}");

        let dto = self.get_request::<BlockDto>(path, None, false).await?;

        Ok(Block::try_from_dto_with_params(
            dto,
            &self.get_protocol_parameters().await?,
        )?)
    }

    /// Finds a block by its ID and returns it as raw bytes.
    /// GET /api/core/v3/blocks/{blockId}
    pub async fn get_block_raw(&self, block_id: &BlockId) -> Result<Vec<u8>, ClientError> {
        let path = &format!("api/core/v3/blocks/{block_id}");

        self.get_request_bytes(path, None).await
    }

    /// Returns the metadata of a block.
    /// GET /api/core/v3/blocks/{blockId}/metadata
    pub async fn get_block_metadata(&self, block_id: &BlockId) -> Result<BlockMetadataResponse, ClientError> {
        let path = &format!("api/core/v3/blocks/{block_id}/metadata");

        self.get_request(path, None, true).await
    }

    /// Returns a block with its metadata.
    /// GET /api/core/v3/blocks/{blockId}/full
    pub async fn get_block_with_metadata(&self, block_id: &BlockId) -> Result<BlockWithMetadataResponse, ClientError> {
        let path = &format!("api/core/v3/blocks/{block_id}/full");

        self.get_request(path, None, true).await
    }

    // UTXO routes.

    /// Finds an output by its ID and returns it as object.
    /// GET /api/core/v3/outputs/{outputId}
    pub async fn get_output(&self, output_id: &OutputId) -> Result<OutputResponse, ClientError> {
        let path = &format!("api/core/v3/outputs/{output_id}");

        self.get_request(path, None, false).await
    }

    /// Finds an output by its ID and returns it as raw bytes.
    /// GET /api/core/v3/outputs/{outputId}
    pub async fn get_output_raw(&self, output_id: &OutputId) -> Result<Vec<u8>, ClientError> {
        let path = &format!("api/core/v3/outputs/{output_id}");

        self.get_request_bytes(path, None).await
    }

    /// Finds output metadata by output ID.
    /// GET /api/core/v3/outputs/{outputId}/metadata
    pub async fn get_output_metadata(&self, output_id: &OutputId) -> Result<OutputMetadata, ClientError> {
        let path = &format!("api/core/v3/outputs/{output_id}/metadata");

        self.get_request(path, None, false).await
    }

    /// Finds an output with its metadata by output ID.
    /// GET /api/core/v3/outputs/{outputId}/full
    pub async fn get_output_with_metadata(
        &self,
        output_id: &OutputId,
    ) -> Result<OutputWithMetadataResponse, ClientError> {
        let path = &format!("api/core/v3/outputs/{output_id}/full");

        self.get_request(path, None, false).await
    }

    /// Returns the earliest confirmed block containing the transaction with the given ID.
    /// GET /api/core/v3/transactions/{transactionId}/included-block
    pub async fn get_included_block(&self, transaction_id: &TransactionId) -> Result<Block, ClientError> {
        let path = &format!("api/core/v3/transactions/{transaction_id}/included-block");

        let dto = self.get_request::<BlockDto>(path, None, true).await?;

        Ok(Block::try_from_dto_with_params(
            dto,
            &self.get_protocol_parameters().await?,
        )?)
    }

    /// Returns the earliest confirmed block containing the transaction with the given ID, as raw bytes.
    /// GET /api/core/v3/transactions/{transactionId}/included-block
    pub async fn get_included_block_raw(&self, transaction_id: &TransactionId) -> Result<Vec<u8>, ClientError> {
        let path = &format!("api/core/v3/transactions/{transaction_id}/included-block");

        self.get_request_bytes(path, None).await
    }

    /// Returns the metadata of the earliest block containing the tx that was confirmed.
    /// GET /api/core/v3/transactions/{transactionId}/included-block/metadata
    pub async fn get_included_block_metadata(
        &self,
        transaction_id: &TransactionId,
    ) -> Result<BlockMetadataResponse, ClientError> {
        let path = &format!("api/core/v3/transactions/{transaction_id}/included-block/metadata");

        self.get_request(path, None, true).await
    }

    /// Finds the metadata of a transaction.
    /// GET /api/core/v3/transactions/{transactionId}/metadata
    pub async fn get_transaction_metadata(
        &self,
        transaction_id: &TransactionId,
    ) -> Result<TransactionMetadataResponse, ClientError> {
        let path = &format!("api/core/v3/transactions/{transaction_id}/metadata");

        self.get_request(path, None, true).await
    }

    // Commitments routes.

    /// Finds a slot commitment by its ID and returns it as object.
    /// GET /api/core/v3/commitments/{commitmentId}
    pub async fn get_commitment(&self, commitment_id: &SlotCommitmentId) -> Result<SlotCommitment, ClientError> {
        let path = &format!("api/core/v3/commitments/{commitment_id}");

        self.get_request(path, None, false).await
    }

    /// Finds a slot commitment by its ID and returns it as raw bytes.
    /// GET /api/core/v3/commitments/{commitmentId}
    pub async fn get_commitment_raw(&self, commitment_id: &SlotCommitmentId) -> Result<Vec<u8>, ClientError> {
        let path = &format!("api/core/v3/commitments/{commitment_id}");

        self.get_request_bytes(path, None).await
    }

    /// Get all UTXO changes of a given slot by slot commitment ID.
    /// GET /api/core/v3/commitments/{commitmentId}/utxo-changes
    pub async fn get_utxo_changes(&self, commitment_id: &SlotCommitmentId) -> Result<UtxoChangesResponse, ClientError> {
        let path = &format!("api/core/v3/commitments/{commitment_id}/utxo-changes");

        self.get_request(path, None, false).await
    }

    /// Get all full UTXO changes of a given slot by slot commitment ID.
    /// GET /api/core/v3/commitments/{commitmentId}/utxo-changes/full
    pub async fn get_utxo_changes_full(
        &self,
        commitment_id: &SlotCommitmentId,
    ) -> Result<UtxoChangesFullResponse, ClientError> {
        let path = &format!("api/core/v3/commitments/{commitment_id}/utxo-changes/full");

        self.get_request(path, None, false).await
    }

    /// Finds a slot commitment by slot index and returns it as object.
    /// GET /api/core/v3/commitments/by-slot/{slot}
    pub async fn get_commitment_by_slot(&self, slot: SlotIndex) -> Result<SlotCommitment, ClientError> {
        let path = &format!("api/core/v3/commitments/by-slot/{slot}");

        self.get_request(path, None, false).await
    }

    /// Finds a slot commitment by slot index and returns it as raw bytes.
    /// GET /api/core/v3/commitments/by-slot/{slot}
    pub async fn get_commitment_by_slot_raw(&self, slot: SlotIndex) -> Result<Vec<u8>, ClientError> {
        let path = &format!("api/core/v3/commitments/by-slot/{slot}");

        self.get_request_bytes(path, None).await
    }

    /// Get all UTXO changes of a given slot by its index.
    /// GET /api/core/v3/commitments/by-slot/{slot}/utxo-changes
    pub async fn get_utxo_changes_by_slot(&self, slot: SlotIndex) -> Result<UtxoChangesResponse, ClientError> {
        let path = &format!("api/core/v3/commitments/by-slot/{slot}/utxo-changes");

        self.get_request(path, None, false).await
    }

    /// Get all full UTXO changes of a given slot by its index.
    /// GET /api/core/v3/commitments/by-slot/{slot}/utxo-changes/full
    pub async fn get_utxo_changes_full_by_slot(&self, slot: SlotIndex) -> Result<UtxoChangesFullResponse, ClientError> {
        let path = &format!("api/core/v3/commitments/by-slot/{slot}/utxo-changes/full");

        self.get_request(path, None, false).await
    }
}

impl Client {
    /// GET /api/core/v3/info endpoint
    pub async fn get_info(url: &str, auth: Option<NodeAuth>) -> Result<InfoResponse, ClientError> {
        let mut url = crate::client::node_manager::builder::validate_url(Url::parse(url)?)?;
        if let Some(auth) = &auth {
            if let Some((name, password)) = &auth.basic_auth_name_pwd {
                url.set_username(name).map_err(|_| ClientError::UrlAuth("username"))?;
                url.set_password(Some(password))
                    .map_err(|_| ClientError::UrlAuth("password"))?;
            }
        }

        if url.path().ends_with('/') {
            url.set_path(&format!("{}{}", url.path(), INFO_PATH));
        } else {
            url.set_path(&format!("{}/{}", url.path(), INFO_PATH));
        }

        let resp: InfoResponse =
            crate::client::node_manager::http_client::HttpClient::new(DEFAULT_USER_AGENT.to_string())
                .get(
                    &Node {
                        url,
                        auth,
                        disabled: false,
                        permanode: false,
                    },
                    DEFAULT_API_TIMEOUT,
                )
                .await?
                .into_json()
                .await?;

        Ok(resp)
    }

    /// GET /api/core/v3/info endpoint
    pub(crate) async fn get_permanode_info(mut node: Node) -> Result<PermanodeInfoResponse, ClientError> {
        log::debug!("get_permanode_info");
        if let Some(auth) = &node.auth {
            if let Some((name, password)) = &auth.basic_auth_name_pwd {
                node.url
                    .set_username(name)
                    .map_err(|_| ClientError::UrlAuth("username"))?;
                node.url
                    .set_password(Some(password))
                    .map_err(|_| ClientError::UrlAuth("password"))?;
            }
        }

        if node.url.path().ends_with('/') {
            node.url.set_path(&format!("{}{}", node.url.path(), INFO_PATH));
        } else {
            node.url.set_path(&format!("{}/{}", node.url.path(), INFO_PATH));
        }

        let resp: PermanodeInfoResponse =
            crate::client::node_manager::http_client::HttpClient::new(DEFAULT_USER_AGENT.to_string())
                .get(&node, DEFAULT_API_TIMEOUT)
                .await?
                .into_json()
                .await?;

        Ok(resp)
    }
}