diff --git a/ghost-crab-macros/src/lib.rs b/ghost-crab-macros/src/lib.rs index 5db4eb4..9629565 100644 --- a/ghost-crab-macros/src/lib.rs +++ b/ghost-crab-macros/src/lib.rs @@ -204,13 +204,13 @@ fn create_handler(metadata: TokenStream, input: TokenStream, is_template: bool) pub struct #fn_name; impl #fn_name { - pub fn new() -> Arc> { + pub fn new() -> Arc> { Arc::new(Box::new(#fn_name {})) } } #[async_trait] - impl Handler for #fn_name { + impl EventHandler for #fn_name { async fn handle(&self, #fn_args) { let decoded_log = #ctx .log diff --git a/ghost-crab/src/block_handler.rs b/ghost-crab/src/block_handler.rs index fbbb8f7..ed36eb1 100644 --- a/ghost-crab/src/block_handler.rs +++ b/ghost-crab/src/block_handler.rs @@ -35,7 +35,7 @@ pub struct ProcessBlocksInput { pub provider: RootProvider>, } -pub async fn process_logs_block( +pub async fn process_blocks( ProcessBlocksInput { handler, templates, provider }: ProcessBlocksInput, ) -> Result<(), TransportError> { let step = handler.step(); diff --git a/ghost-crab/src/process_logs.rs b/ghost-crab/src/event_handler.rs similarity index 65% rename from ghost-crab/src/process_logs.rs rename to ghost-crab/src/event_handler.rs index 79f4545..73dc273 100644 --- a/ghost-crab/src/process_logs.rs +++ b/ghost-crab/src/event_handler.rs @@ -1,25 +1,49 @@ -use crate::handler::{Context, HandleInstance}; use crate::indexer::TemplateManager; use crate::latest_block_manager::LatestBlockManager; use alloy::primitives::Address; use alloy::providers::{Provider, RootProvider}; use alloy::rpc::types::eth::Filter; +use alloy::rpc::types::eth::Log; use alloy::transports::http::{Client, Http}; use alloy::transports::TransportError; +use async_trait::async_trait; use ghost_crab_common::config::ExecutionMode; +use std::sync::Arc; use std::time::Duration; +pub struct EventContext { + pub log: Log, + pub provider: RootProvider>, + pub templates: TemplateManager, + pub contract_address: Address, +} + +pub type EventHandlerInstance = Arc>; + +#[async_trait] +pub trait EventHandler { + async fn handle(&self, params: EventContext); + fn get_source(&self) -> String; + fn is_template(&self) -> bool; + fn start_block(&self) -> u64; + fn address(&self) -> Address; + fn network(&self) -> String; + fn rpc_url(&self) -> String; + fn execution_mode(&self) -> ExecutionMode; + fn event_signature(&self) -> String; +} + #[derive(Clone)] pub struct ProcessEventsInput { pub start_block: u64, pub address: Address, pub step: u64, - pub handler: HandleInstance, + pub handler: EventHandlerInstance, pub templates: TemplateManager, pub provider: RootProvider>, } -pub async fn process_logs( +pub async fn process_events( ProcessEventsInput { start_block, step, address, handler, templates, provider }: ProcessEventsInput, ) -> Result<(), TransportError> { let execution_mode = handler.execution_mode(); @@ -63,7 +87,12 @@ pub async fn process_logs( tokio::spawn(async move { handler - .handle(Context { log, provider, templates, contract_address: address }) + .handle(EventContext { + log, + provider, + templates, + contract_address: address, + }) .await; }); } @@ -74,7 +103,12 @@ pub async fn process_logs( let provider = provider.clone(); handler - .handle(Context { log, provider, templates, contract_address: address }) + .handle(EventContext { + log, + provider, + templates, + contract_address: address, + }) .await; } } diff --git a/ghost-crab/src/handler.rs b/ghost-crab/src/handler.rs deleted file mode 100644 index 4781316..0000000 --- a/ghost-crab/src/handler.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::indexer::TemplateManager; -use alloy::primitives::Address; -use alloy::providers::RootProvider; -use alloy::rpc::types::eth::Log; -use alloy::transports::http::{Client, Http}; -use async_trait::async_trait; -use ghost_crab_common::config::ExecutionMode; -use std::sync::Arc; - -pub struct Context { - pub log: Log, - pub provider: RootProvider>, - pub templates: TemplateManager, - pub contract_address: Address, -} - -pub type HandleInstance = Arc>; - -#[async_trait] -pub trait Handler { - async fn handle(&self, params: Context); - fn get_source(&self) -> String; - fn is_template(&self) -> bool; - fn start_block(&self) -> u64; - fn address(&self) -> Address; - fn network(&self) -> String; - fn rpc_url(&self) -> String; - fn execution_mode(&self) -> ExecutionMode; - fn event_signature(&self) -> String; -} diff --git a/ghost-crab/src/indexer.rs b/ghost-crab/src/indexer.rs index b637b63..711a3d3 100644 --- a/ghost-crab/src/indexer.rs +++ b/ghost-crab/src/indexer.rs @@ -1,7 +1,6 @@ -use crate::block_handler::{process_logs_block, BlockHandlerInstance, ProcessBlocksInput}; +use crate::block_handler::{process_blocks, BlockHandlerInstance, ProcessBlocksInput}; use crate::cache::manager::RPCManager; -use crate::handler::HandleInstance; -use crate::process_logs::{process_logs, ProcessEventsInput}; +use crate::event_handler::{process_events, EventHandlerInstance, ProcessEventsInput}; use alloy::primitives::Address; use tokio::sync::mpsc::error::SendError; use tokio::sync::mpsc::{self, Receiver, Sender}; @@ -9,7 +8,7 @@ use tokio::sync::mpsc::{self, Receiver, Sender}; pub struct Template { pub start_block: u64, pub address: Address, - pub handler: HandleInstance, + pub handler: EventHandlerInstance, } #[derive(Clone)] @@ -44,7 +43,7 @@ impl Indexer { } } - pub async fn load_event_handler(&mut self, handler: HandleInstance) { + pub async fn load_event_handler(&mut self, handler: EventHandlerInstance) { if handler.is_template() { return; } @@ -74,7 +73,7 @@ impl Indexer { pub async fn start(mut self) { for block_handler in self.block_handlers { tokio::spawn(async move { - if let Err(error) = process_logs_block(block_handler).await { + if let Err(error) = process_blocks(block_handler).await { println!("Error processing logs for block handler: {error}"); } }); @@ -82,7 +81,7 @@ impl Indexer { for handler in self.handlers { tokio::spawn(async move { - if let Err(error) = process_logs(handler).await { + if let Err(error) = process_events(handler).await { println!("Error processing logs for handler: {error}"); } }); @@ -104,7 +103,7 @@ impl Indexer { }; tokio::spawn(async move { - if let Err(error) = process_logs(handler).await { + if let Err(error) = process_events(handler).await { println!("Error processing logs for handler: {error}"); } }); diff --git a/ghost-crab/src/lib.rs b/ghost-crab/src/lib.rs index 6e7ad93..e9e7fc0 100644 --- a/ghost-crab/src/lib.rs +++ b/ghost-crab/src/lib.rs @@ -1,10 +1,9 @@ pub mod block_handler; pub mod cache; -pub mod handler; +pub mod event_handler; pub mod indexer; pub mod latest_block_manager; pub mod prelude; -pub mod process_logs; -pub use indexer::Indexer; pub use ghost_crab_common::config; +pub use indexer::Indexer; diff --git a/ghost-crab/src/prelude.rs b/ghost-crab/src/prelude.rs index 45e8a67..aa90ff8 100644 --- a/ghost-crab/src/prelude.rs +++ b/ghost-crab/src/prelude.rs @@ -1,4 +1,4 @@ -pub use crate::handler::{Context, Handler}; +pub use crate::event_handler::{EventContext, EventHandler}; pub use alloy; pub use alloy::{ sol, @@ -16,7 +16,6 @@ pub use crate::cache; pub use crate::config; pub use crate::indexer; pub use crate::indexer::Template; -pub use crate::process_logs; pub use alloy::primitives::address; pub use alloy::primitives::Address; pub use alloy::providers::Provider;