koios_sdk/api/block.rs
1use crate::{
2 error::Result,
3 models::{
4 block::{Block, BlockInfo, BlockTransaction, BlockTransactionCbor},
5 transaction::TransactionInfo,
6 BlockHashesRequest, BlockTxInfoRequest,
7 },
8 Client,
9};
10
11impl Client {
12 /// Get summarized details about all blocks (paginated - latest first)
13 ///
14 /// # Examples
15 ///
16 /// ```no_run
17 /// use koios_sdk::Client;
18 ///
19 /// #[tokio::main]
20 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
21 /// let client = Client::new()?;
22 /// let blocks = client.get_blocks().await?;
23 /// println!("Latest blocks: {:?}", blocks);
24 /// Ok(())
25 /// }
26 /// ```
27 pub async fn get_blocks(&self) -> Result<Vec<Block>> {
28 self.get("/blocks").await
29 }
30
31 /// Get detailed information about specific blocks
32 ///
33 /// # Arguments
34 ///
35 /// * `block_hashes` - Vector of block hashes to query
36 ///
37 /// # Examples
38 ///
39 /// ```no_run
40 /// use koios_sdk::Client;
41 ///
42 /// #[tokio::main]
43 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
44 /// let client = Client::new()?;
45 /// let block_hashes = vec![
46 /// "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
47 /// ];
48 /// let block_info = client.get_block_info(&block_hashes).await?;
49 /// println!("Block info: {:?}", block_info);
50 /// Ok(())
51 /// }
52 /// ```
53 pub async fn get_block_info(&self, block_hashes: &[String]) -> Result<Vec<BlockInfo>> {
54 let request = BlockHashesRequest {
55 block_hashes: block_hashes.to_vec(),
56 };
57 self.post("/block_info", &request).await
58 }
59
60 /// Get a list of all transactions included in provided blocks
61 ///
62 /// # Arguments
63 ///
64 /// * `block_hashes` - Vector of block hashes to query
65 ///
66 /// # Examples
67 ///
68 /// ```no_run
69 /// use koios_sdk::Client;
70 ///
71 /// #[tokio::main]
72 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
73 /// let client = Client::new()?;
74 /// let block_hashes = vec![
75 /// "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
76 /// ];
77 /// let block_txs = client.get_block_transactions(&block_hashes).await?;
78 /// println!("Block transactions: {:?}", block_txs);
79 /// Ok(())
80 /// }
81 /// ```
82 pub async fn get_block_transactions(
83 &self,
84 block_hashes: &[String],
85 ) -> Result<Vec<BlockTransaction>> {
86 let request = BlockHashesRequest {
87 block_hashes: block_hashes.to_vec(),
88 };
89 self.post("/block_txs", &request).await
90 }
91
92 /// Get raw CBOR data for all transactions within requested blocks
93 ///
94 /// # Arguments
95 ///
96 /// * `block_hashes` - Vector of block hashes to query
97 ///
98 /// # Examples
99 ///
100 /// ```no_run
101 /// use koios_sdk::Client;
102 ///
103 /// #[tokio::main]
104 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
105 /// let client = Client::new()?;
106 /// let block_hashes = vec![
107 /// "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
108 /// ];
109 /// let tx_cbor = client.get_block_transaction_cbor(&block_hashes).await?;
110 /// println!("Transaction CBOR: {:?}", tx_cbor);
111 /// Ok(())
112 /// }
113 /// ```
114 pub async fn get_block_transaction_cbor(
115 &self,
116 block_hashes: &[String],
117 ) -> Result<Vec<BlockTransactionCbor>> {
118 let request = BlockHashesRequest {
119 block_hashes: block_hashes.to_vec(),
120 };
121 self.post("/block_tx_cbor", &request).await
122 }
123
124 /// Get detailed information about transactions for requested blocks
125 ///
126 /// # Arguments
127 ///
128 /// * `block_hashes` - Vector of block hashes to query
129 /// * `options` - Optional parameters for customizing the response
130 ///
131 /// # Examples
132 ///
133 /// ```no_run
134 /// use koios_sdk::Client;
135 /// use koios_sdk::models::BlockTxInfoRequest;
136 ///
137 /// #[tokio::main]
138 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
139 /// let client = Client::new()?;
140 /// let block_hashes = vec![
141 /// "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e".to_string()
142 /// ];
143 /// let options = BlockTxInfoRequest {
144 /// block_hashes,
145 /// inputs: Some(true),
146 /// metadata: Some(true),
147 /// ..Default::default()
148 /// };
149 /// let tx_info = client.get_block_transaction_info(&options).await?;
150 /// println!("Transaction info: {:?}", tx_info);
151 /// Ok(())
152 /// }
153 /// ```
154 #[deprecated(note = "This endpoint is deprecated in the Koios API")]
155 pub async fn get_block_transaction_info(
156 &self,
157 options: &BlockTxInfoRequest,
158 ) -> Result<Vec<TransactionInfo>> {
159 self.post("/block_tx_info", options).await
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use crate::Client;
166 use pretty_assertions::assert_eq;
167 use serde_json::json;
168 use wiremock::matchers::{method, path};
169 use wiremock::{Mock, MockServer, ResponseTemplate};
170
171 #[tokio::test]
172 async fn test_get_blocks() {
173 let mock_server = MockServer::start().await;
174 let client = Client::builder()
175 .base_url(mock_server.uri())
176 .build()
177 .unwrap();
178
179 let mock_response = json!([{
180 "hash": "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e",
181 "epoch_no": 321,
182 "abs_slot": 53384091,
183 "epoch_slot": 85691,
184 "block_height": 7017300,
185 "block_size": 4318,
186 "block_time": 1630106091,
187 "tx_count": 8,
188 "vrf_key": "vrf_vk1gn7g0xjwhxhm9pv4m0pfz4qw8fj95h5qkkc9zhl02wsm6v0urq9qgug5fx",
189 "op_cert_counter": 1,
190 "proto_major": 6,
191 "proto_minor": 0,
192 "parent_hash": "43c66ecb78f5938d7a3bf2cef6b575acda9c86a7c0c27dd91cdcd9e2f0f6e683"
193 }]);
194
195 Mock::given(method("GET"))
196 .and(path("/blocks"))
197 .respond_with(ResponseTemplate::new(200).set_body_json(&mock_response))
198 .mount(&mock_server)
199 .await;
200
201 let response = client.get_blocks().await.unwrap();
202 assert_eq!(response.len(), 1);
203 assert_eq!(
204 response[0].hash,
205 "f144a8264acf4bdfe2e1241170969c930d64ab6b0996a4a45237b623f1dd670e"
206 );
207 }
208
209 // Add more tests for other endpoints...
210}