helium_api/
blocks.rs

1use crate::{
2    models::{transactions::Transaction, Descriptions, Height},
3    *,
4};
5
6/// Get the current height of the blockchain
7pub async fn height(client: &Client) -> Result<u64> {
8    let height: Height = client.fetch("/blocks/height", NO_QUERY).await?;
9    Ok(height.height)
10}
11
12/// Retrieves block descriptions. Blocks descriptions are paged.
13/// A cursor field will be in the response when more results are available.
14pub async fn descriptions(client: &Client, cursor: Option<&str>) -> Result<Descriptions> {
15    let query = cursor.map_or(NO_QUERY.to_vec(), |c| vec![c]);
16    client
17        .fetch_data("/blocks", &query)
18        .await
19        .map(|Data { data, cursor }| Descriptions { data, cursor })
20}
21
22pub fn transactions_at_height(client: &Client, block: u64) -> Stream<Transaction> {
23    client.fetch_stream(format!("/blocks/{}/transactions", block).as_str(), NO_QUERY)
24}
25
26pub fn transactions_at_block_hash(client: &Client, hash: &str) -> Stream<Transaction> {
27    client.fetch_stream(
28        format!("/blocks/hash/{}/transactions", hash).as_str(),
29        NO_QUERY,
30    )
31}
32
33#[cfg(test)]
34mod test {
35    use super::*;
36    use tokio::test;
37
38    #[test]
39    async fn height() {
40        let client = get_test_client();
41        let height = blocks::height(&client).await.expect("height");
42        assert!(height > 0);
43    }
44
45    #[test]
46    async fn descriptions() {
47        let client = get_test_client();
48        let descriptions = blocks::descriptions(&client, None)
49            .await
50            .expect("descriptions");
51        assert!(descriptions.data.len() > 0);
52    }
53
54    #[test]
55    async fn transactions_at_height() {
56        let client = get_test_client();
57        let transactions = blocks::transactions_at_height(&client, 1378232)
58            .take(10)
59            .into_vec()
60            .await
61            .expect("transactions");
62        assert_eq!(transactions.len(), 10);
63    }
64
65    #[test]
66    async fn transactions_at_block_hash() {
67        let client = get_test_client();
68        let transactions = blocks::transactions_at_block_hash(
69            &client,
70            "BogDArZ5QxbgSd4wLmCS8NRtRzwvCA5fGn1V2TtsYoU",
71        )
72        .take(10)
73        .into_vec()
74        .await
75        .expect("transactions");
76        assert_eq!(transactions.len(), 10);
77    }
78}