Skip to content

Commit

Permalink
chore: Rename handler -> EventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-herasme committed Jul 20, 2024
1 parent 3d1bf73 commit 5003616
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 51 deletions.
4 changes: 2 additions & 2 deletions ghost-crab-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<(dyn Handler + Send + Sync)>> {
pub fn new() -> Arc<Box<(dyn EventHandler + Send + Sync)>> {
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
Expand Down
2 changes: 1 addition & 1 deletion ghost-crab/src/block_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct ProcessBlocksInput {
pub provider: RootProvider<Http<Client>>,
}

pub async fn process_logs_block(
pub async fn process_blocks(
ProcessBlocksInput { handler, templates, provider }: ProcessBlocksInput,
) -> Result<(), TransportError> {
let step = handler.step();
Expand Down
44 changes: 39 additions & 5 deletions ghost-crab/src/process_logs.rs → ghost-crab/src/event_handler.rs
Original file line number Diff line number Diff line change
@@ -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<Http<Client>>,
pub templates: TemplateManager,
pub contract_address: Address,
}

pub type EventHandlerInstance = Arc<Box<(dyn EventHandler + Send + Sync)>>;

#[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<Http<Client>>,
}

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();
Expand Down Expand Up @@ -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;
});
}
Expand All @@ -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;
}
}
Expand Down
30 changes: 0 additions & 30 deletions ghost-crab/src/handler.rs

This file was deleted.

15 changes: 7 additions & 8 deletions ghost-crab/src/indexer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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};

pub struct Template {
pub start_block: u64,
pub address: Address,
pub handler: HandleInstance,
pub handler: EventHandlerInstance,
}

#[derive(Clone)]
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -74,15 +73,15 @@ 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}");
}
});
}

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}");
}
});
Expand All @@ -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}");
}
});
Expand Down
5 changes: 2 additions & 3 deletions ghost-crab/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 1 addition & 2 deletions ghost-crab/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use crate::handler::{Context, Handler};
pub use crate::event_handler::{EventContext, EventHandler};
pub use alloy;
pub use alloy::{
sol,
Expand All @@ -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;

0 comments on commit 5003616

Please sign in to comment.