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
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! IOTA node core API

pub mod routes;

use iota_types::{
    api::response::{OutputMetadataResponse, OutputWithMetadataResponse},
    block::output::OutputId,
};

#[cfg(not(target_family = "wasm"))]
use crate::constants::MAX_PARALLEL_API_REQUESTS;
use crate::{Client, Result};

impl Client {
    /// Request outputs by their output ID in parallel
    pub async fn get_outputs(&self, output_ids: Vec<OutputId>) -> Result<Vec<OutputWithMetadataResponse>> {
        let mut outputs = Vec::new();

        #[cfg(target_family = "wasm")]
        for output_id in output_ids {
            outputs.push(self.get_output(&output_id).await?);
        }

        #[cfg(not(target_family = "wasm"))]
        for output_ids_chunk in output_ids.chunks(MAX_PARALLEL_API_REQUESTS).map(<[OutputId]>::to_vec) {
            let mut tasks = Vec::new();
            for output_id in output_ids_chunk {
                let client_ = self.clone();

                tasks.push(async move {
                    tokio::spawn(async move {
                        let output_response = client_.get_output(&output_id).await?;
                        crate::Result::Ok(output_response)
                    })
                    .await
                });
            }
            for res in futures::future::try_join_all(tasks).await? {
                let output_response = res?;
                outputs.push(output_response);
            }
        }
        Ok(outputs)
    }

    /// Request outputs by their output ID in parallel, ignoring failed requests
    /// Useful to get data about spent outputs, that might not be pruned yet
    pub async fn try_get_outputs(&self, output_ids: Vec<OutputId>) -> Result<Vec<OutputWithMetadataResponse>> {
        let mut outputs = Vec::new();

        #[cfg(target_family = "wasm")]
        for output_id in output_ids {
            if let Ok(output_response) = self.get_output(&output_id).await {
                outputs.push(output_response);
            }
        }

        #[cfg(not(target_family = "wasm"))]
        for output_ids_chunk in output_ids.chunks(MAX_PARALLEL_API_REQUESTS).map(<[OutputId]>::to_vec) {
            let mut tasks = Vec::new();
            for output_id in output_ids_chunk {
                let client_ = self.clone();

                tasks.push(async move { tokio::spawn(async move { client_.get_output(&output_id).await.ok() }).await });
            }
            for output_response in (futures::future::try_join_all(tasks).await?).into_iter().flatten() {
                outputs.push(output_response);
            }
        }
        Ok(outputs)
    }

    /// Requests metadata for outputs by their output ID in parallel, ignoring failed requests
    pub async fn try_get_outputs_metadata(&self, output_ids: Vec<OutputId>) -> Result<Vec<OutputMetadataResponse>> {
        let mut output_metadata_responses = Vec::new();

        #[cfg(target_family = "wasm")]
        for output_id in output_ids {
            if let Ok(output_metadata_response) = self.get_output_metadata(&output_id).await {
                output_metadata_responses.push(output_metadata_response);
            }
        }

        #[cfg(not(target_family = "wasm"))]
        for output_ids_chunk in output_ids.chunks(MAX_PARALLEL_API_REQUESTS).map(<[OutputId]>::to_vec) {
            let mut tasks = Vec::new();
            for output_id in output_ids_chunk {
                let client_ = self.clone();

                tasks.push(async move {
                    tokio::spawn(async move { client_.get_output_metadata(&output_id).await.ok() }).await
                });
            }
            for output_metadata_response in (futures::future::try_join_all(tasks).await?).into_iter().flatten() {
                output_metadata_responses.push(output_metadata_response);
            }
        }

        Ok(output_metadata_responses)
    }
}