From 64850859b12f6e93277ab5813908acef28d96f11 Mon Sep 17 00:00:00 2001 From: Darshan Chauhan <46072647+GradleD@users.noreply.github.com> Date: Sat, 9 Nov 2024 10:58:56 +0530 Subject: [PATCH] Update mod.rs add chunk size limit for starknet_getEvents method --- crates/katana/rpc/rpc/src/starknet/mod.rs | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/katana/rpc/rpc/src/starknet/mod.rs b/crates/katana/rpc/rpc/src/starknet/mod.rs index 3a15ef0130..da8535da62 100644 --- a/crates/katana/rpc/rpc/src/starknet/mod.rs +++ b/crates/katana/rpc/rpc/src/starknet/mod.rs @@ -52,6 +52,7 @@ use crate::utils; use crate::utils::events::{Cursor, EventBlockId}; pub type StarknetApiResult = Result; +const MAX_CHUNK_SIZE: usize = 100; #[allow(missing_debug_implementations)] pub struct StarknetApi { @@ -499,6 +500,36 @@ impl StarknetApi { } } + + async fn get_events_with_limit( + &self, + filter: EventFilterWithPage, + chunk_size: usize, + ) -> StarknetApiResult { + // Ensure the chunk size is within a reasonable limit + if chunk_size == 0 || chunk_size > Self::MAX_CHUNK_SIZE { + return Err(StarknetApiError::InvalidChunkSize); + } + + // Fetch events based on the provided filter and chunk size + let events = self.fetch_events(filter).await?; + + // Paginate the results based on the specified chunk size + let paginated_events = events.into_iter().take(chunk_size).collect::>(); + + // Prepare the response with the paginated events + let next_token = if events.len() > chunk_size { + Some(generate_next_token(&paginated_events)) + } else { + None + }; + + Ok(EventsPage { + events: paginated_events, + continuation_token: next_token, + }) + } + async fn transaction_status(&self, hash: TxHash) -> StarknetApiResult { let status = self .on_io_blocking_task(move |this| {