koios_sdk/api/
block.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
use crate::{
    error::Result,
    models::{
        block::{Block, BlockInfo, BlockTransaction, BlockTransactionCbor},
        transaction::TransactionInfo,
        BlockHashesRequest, BlockTxInfoRequest,
    },
    Client,
};

impl Client {
    /// Get summarized details about all blocks (paginated - latest first)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let blocks = client.get_blocks().await?;
    ///     println!("Latest blocks: {:?}", blocks);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_blocks(&self) -> Result<Vec<Block>> {
        self.get("/blocks").await
    }

    /// Get detailed information about specific blocks
    ///
    /// # Arguments
    ///
    /// * `block_hashes` - Vector of block hashes 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 block_hashes = vec![
    ///         "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
    ///     ];
    ///     let block_info = client.get_block_info(&block_hashes).await?;
    ///     println!("Block info: {:?}", block_info);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_block_info(&self, block_hashes: &[String]) -> Result<Vec<BlockInfo>> {
        let request = BlockHashesRequest {
            block_hashes: block_hashes.to_vec(),
        };
        self.post("/block_info", &request).await
    }

    /// Get a list of all transactions included in provided blocks
    ///
    /// # Arguments
    ///
    /// * `block_hashes` - Vector of block hashes 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 block_hashes = vec![
    ///         "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
    ///     ];
    ///     let block_txs = client.get_block_transactions(&block_hashes).await?;
    ///     println!("Block transactions: {:?}", block_txs);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_block_transactions(
        &self,
        block_hashes: &[String],
    ) -> Result<Vec<BlockTransaction>> {
        let request = BlockHashesRequest {
            block_hashes: block_hashes.to_vec(),
        };
        self.post("/block_txs", &request).await
    }

    /// Get raw CBOR data for all transactions within requested blocks
    ///
    /// # Arguments
    ///
    /// * `block_hashes` - Vector of block hashes 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 block_hashes = vec![
    ///         "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
    ///     ];
    ///     let tx_cbor = client.get_block_transaction_cbor(&block_hashes).await?;
    ///     println!("Transaction CBOR: {:?}", tx_cbor);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_block_transaction_cbor(
        &self,
        block_hashes: &[String],
    ) -> Result<Vec<BlockTransactionCbor>> {
        let request = BlockHashesRequest {
            block_hashes: block_hashes.to_vec(),
        };
        self.post("/block_tx_cbor", &request).await
    }

    /// Get detailed information about transactions for requested blocks
    ///
    /// # Arguments
    ///
    /// * `block_hashes` - Vector of block hashes to query
    /// * `options` - Optional parameters for customizing the response
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use koios_sdk::Client;
    /// use koios_sdk::models::BlockTxInfoRequest;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new()?;
    ///     let block_hashes = vec![
    ///         "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
    ///     ];
    ///     let options = BlockTxInfoRequest {
    ///         block_hashes,
    ///         inputs: Some(true),
    ///         metadata: Some(true),
    ///         ..Default::default()
    ///     };
    ///     let tx_info = client.get_block_transaction_info(&options).await?;
    ///     println!("Transaction info: {:?}", tx_info);
    ///     Ok(())
    /// }
    /// ```
    #[deprecated(note = "This endpoint is deprecated in the Koios API")]
    pub async fn get_block_transaction_info(
        &self,
        options: &BlockTxInfoRequest,
    ) -> Result<Vec<TransactionInfo>> {
        self.post("/block_tx_info", options).await
    }
}

#[cfg(test)]
mod tests {
    use crate::Client;
    use pretty_assertions::assert_eq;
    use serde_json::json;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[tokio::test]
    async fn test_get_blocks() {
        let mock_server = MockServer::start().await;
        let client = Client::builder()
            .base_url(mock_server.uri())
            .build()
            .unwrap();

        let mock_response = json!([{
            "hash": "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e",
            "epoch_no": 321,
            "abs_slot": 53384091,
            "epoch_slot": 85691,
            "block_height": 7017300,
            "block_size": 4318,
            "block_time": 1630106091,
            "tx_count": 8,
            "vrf_key": "vrf_vk1gn7g0xjwhxhm9pv4m0pfz4qw8fj95h5qkkc9zhl02wsm6v0urq9qgug5fx",
            "op_cert_counter": 1,
            "proto_major": 6,
            "proto_minor": 0,
            "parent_hash": "43c66ecb78f5938d7a3bf2cef6b575acda9c86a7c0c27dd91cdcd9e2f0f6e683"
        }]);

        Mock::given(method("GET"))
            .and(path("/blocks"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&mock_response))
            .mount(&mock_server)
            .await;

        let response = client.get_blocks().await.unwrap();
        assert_eq!(response.len(), 1);
        assert_eq!(
            response[0].hash,
            "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e"
        );
    }

    // Add more tests for other endpoints...
}