Skip to main content

hypersync_client/
types.rs

1use crate::rate_limit::RateLimitInfo;
2use crate::simple_types::{Block, Event, InternalEventJoinStrategy, Log, Trace, Transaction};
3use anyhow::Context;
4use arrow::array::RecordBatch;
5use hypersync_net_types::RollbackGuard;
6
7/// Query response in Arrow format
8#[derive(Default, Debug, Clone)]
9pub struct ArrowResponseData {
10    /// Query blocks response
11    pub blocks: Vec<RecordBatch>,
12    /// Query transactions response
13    pub transactions: Vec<RecordBatch>,
14    /// Query logs response
15    pub logs: Vec<RecordBatch>,
16    /// Query traces response
17    pub traces: Vec<RecordBatch>,
18    /// Query decoded_logs response.
19    ///
20    /// Populated only if event_signature is present.
21    pub decoded_logs: Vec<RecordBatch>,
22}
23
24/// Query response data in Rust native format
25#[derive(Default, Debug, Clone)]
26pub struct ResponseData {
27    /// Query blocks response
28    pub blocks: Vec<Vec<Block>>,
29    /// Query transactions response
30    pub transactions: Vec<Vec<Transaction>>,
31    /// Query logs response
32    pub logs: Vec<Vec<Log>>,
33    /// Query traces response
34    pub traces: Vec<Vec<Trace>>,
35}
36
37impl EventResponse {
38    /// Create EventResponse from ArrowResponse with the specified event join strategy
39    pub(crate) fn try_from_arrow_response(
40        arrow_response: &ArrowResponse,
41        event_join_strategy: &InternalEventJoinStrategy,
42    ) -> anyhow::Result<Self> {
43        let r: QueryResponse = arrow_response
44            .try_into()
45            .context("convert arrow response")?;
46        Ok(Self {
47            archive_height: r.archive_height,
48            next_block: r.next_block,
49            total_execution_time: r.total_execution_time,
50            data: event_join_strategy.join_from_response_data(r.data),
51            rollback_guard: r.rollback_guard,
52        })
53    }
54}
55
56impl TryFrom<&'_ ArrowResponse> for QueryResponse {
57    type Error = anyhow::Error;
58    fn try_from(arrow_response: &ArrowResponse) -> Result<Self, Self::Error> {
59        let blocks = arrow_response
60            .data
61            .blocks
62            .iter()
63            .map(Block::from_arrow)
64            .collect::<anyhow::Result<Vec<_>>>()
65            .context("convert blocks")?;
66
67        let transactions = arrow_response
68            .data
69            .transactions
70            .iter()
71            .map(Transaction::from_arrow)
72            .collect::<anyhow::Result<Vec<_>>>()
73            .context("convert transactions")?;
74
75        let logs = arrow_response
76            .data
77            .logs
78            .iter()
79            .map(Log::from_arrow)
80            .collect::<anyhow::Result<Vec<_>>>()
81            .context("convert logs")?;
82        let traces = arrow_response
83            .data
84            .traces
85            .iter()
86            .map(Trace::from_arrow)
87            .collect::<anyhow::Result<Vec<_>>>()
88            .context("convert traces")?;
89
90        Ok(QueryResponse {
91            archive_height: arrow_response.archive_height,
92            next_block: arrow_response.next_block,
93            total_execution_time: arrow_response.total_execution_time,
94            data: ResponseData {
95                blocks,
96                transactions,
97                logs,
98                traces,
99            },
100            rollback_guard: arrow_response.rollback_guard.clone(),
101        })
102    }
103}
104
105/// Query response from hypersync instance.
106/// Contain next_block field in case query didn't process all the block range
107#[derive(Debug, Clone)]
108pub struct QueryResponse<T = ResponseData> {
109    /// Current height of the source hypersync instance
110    pub archive_height: Option<u64>,
111    /// Next block to query for, the responses are paginated so
112    /// the caller should continue the query from this block if they
113    /// didn't get responses up to the to_block they specified in the Query.
114    pub next_block: u64,
115    /// Total time it took the hypersync instance to execute the query.
116    pub total_execution_time: u64,
117    /// Response data
118    pub data: T,
119    /// Rollback guard
120    pub rollback_guard: Option<RollbackGuard>,
121}
122
123/// Alias for Arrow Query response
124pub type ArrowResponse = QueryResponse<ArrowResponseData>;
125/// Alias for Event oriented, vectorized QueryResponse
126pub type EventResponse = QueryResponse<Vec<Event>>;
127
128/// Response that includes rate limit information from the server.
129///
130/// Returned by [`Client::get_with_rate_limit`] and [`Client::get_arrow_with_rate_limit`].
131/// Use this when you need to inspect rate limit headers for external monitoring or
132/// coordination across systems.
133#[derive(Debug, Clone)]
134pub struct QueryResponseWithRateLimit<T = ResponseData> {
135    /// The query response data.
136    pub response: QueryResponse<T>,
137    /// Rate limit information from response headers (if present).
138    pub rate_limit: RateLimitInfo,
139}