hypersync_client/
types.rs1use 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#[derive(Default, Debug, Clone)]
9pub struct ArrowResponseData {
10 pub blocks: Vec<RecordBatch>,
12 pub transactions: Vec<RecordBatch>,
14 pub logs: Vec<RecordBatch>,
16 pub traces: Vec<RecordBatch>,
18 pub decoded_logs: Vec<RecordBatch>,
22}
23
24#[derive(Default, Debug, Clone)]
26pub struct ResponseData {
27 pub blocks: Vec<Vec<Block>>,
29 pub transactions: Vec<Vec<Transaction>>,
31 pub logs: Vec<Vec<Log>>,
33 pub traces: Vec<Vec<Trace>>,
35}
36
37impl EventResponse {
38 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#[derive(Debug, Clone)]
108pub struct QueryResponse<T = ResponseData> {
109 pub archive_height: Option<u64>,
111 pub next_block: u64,
115 pub total_execution_time: u64,
117 pub data: T,
119 pub rollback_guard: Option<RollbackGuard>,
121}
122
123pub type ArrowResponse = QueryResponse<ArrowResponseData>;
125pub type EventResponse = QueryResponse<Vec<Event>>;
127
128#[derive(Debug, Clone)]
134pub struct QueryResponseWithRateLimit<T = ResponseData> {
135 pub response: QueryResponse<T>,
137 pub rate_limit: RateLimitInfo,
139}