From ab5f9c52cfeb3bfc9ad01b63f9b3557339313932 Mon Sep 17 00:00:00 2001 From: Luis Herasme Date: Thu, 25 Jul 2024 23:30:28 -0400 Subject: [PATCH] refactor: Refactor indexer and add errors --- ghost-crab-macros/src/lib.rs | 2 +- ghost-crab/src/block_handler.rs | 2 +- ghost-crab/src/event_handler.rs | 2 +- ghost-crab/src/indexer/error.rs | 22 +++++++ ghost-crab/src/{ => indexer}/indexer.rs | 78 ++++++++++++------------- ghost-crab/src/indexer/mod.rs | 3 + ghost-crab/src/indexer/templates.rs | 26 +++++++++ ghost-crab/src/lib.rs | 2 +- ghost-crab/src/prelude.rs | 2 +- 9 files changed, 94 insertions(+), 45 deletions(-) create mode 100644 ghost-crab/src/indexer/error.rs rename ghost-crab/src/{ => indexer}/indexer.rs (68%) create mode 100644 ghost-crab/src/indexer/mod.rs create mode 100644 ghost-crab/src/indexer/templates.rs diff --git a/ghost-crab-macros/src/lib.rs b/ghost-crab-macros/src/lib.rs index ace44d8..bbf2916 100644 --- a/ghost-crab-macros/src/lib.rs +++ b/ghost-crab-macros/src/lib.rs @@ -48,7 +48,7 @@ pub fn block_handler(metadata: TokenStream, input: TokenStream) -> TokenStream { #fn_body } - fn name() -> String { + fn name(&self) -> String { String::from(#name) } } diff --git a/ghost-crab/src/block_handler.rs b/ghost-crab/src/block_handler.rs index 3235d7f..4de9626 100644 --- a/ghost-crab/src/block_handler.rs +++ b/ghost-crab/src/block_handler.rs @@ -1,5 +1,5 @@ use crate::cache::manager::CacheProvider; -use crate::indexer::TemplateManager; +use crate::indexer::templates::TemplateManager; use crate::latest_block_manager::LatestBlockManager; use alloy::providers::Provider; use alloy::rpc::types::eth::Block; diff --git a/ghost-crab/src/event_handler.rs b/ghost-crab/src/event_handler.rs index c4df51e..71d8429 100644 --- a/ghost-crab/src/event_handler.rs +++ b/ghost-crab/src/event_handler.rs @@ -1,5 +1,5 @@ use crate::cache::manager::CacheProvider; -use crate::indexer::TemplateManager; +use crate::indexer::templates::TemplateManager; use crate::latest_block_manager::LatestBlockManager; use alloy::eips::BlockNumberOrTag; use alloy::primitives::Address; diff --git a/ghost-crab/src/indexer/error.rs b/ghost-crab/src/indexer/error.rs new file mode 100644 index 0000000..040686d --- /dev/null +++ b/ghost-crab/src/indexer/error.rs @@ -0,0 +1,22 @@ +use core::fmt; + +#[derive(Debug)] +pub enum AddHandlerError { + NotFound(String), + NetworkNotFound(String), +} + +impl fmt::Display for AddHandlerError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + AddHandlerError::NotFound(handler) => { + write!(f, "Handler not found: {}", handler) + } + AddHandlerError::NetworkNotFound(network) => { + write!(f, "Network not found: {}", network) + } + } + } +} + +impl std::error::Error for AddHandlerError {} diff --git a/ghost-crab/src/indexer.rs b/ghost-crab/src/indexer/indexer.rs similarity index 68% rename from ghost-crab/src/indexer.rs rename to ghost-crab/src/indexer/indexer.rs index 653d42f..b327ca6 100644 --- a/ghost-crab/src/indexer.rs +++ b/ghost-crab/src/indexer/indexer.rs @@ -1,27 +1,12 @@ use crate::block_handler::{process_blocks, BlockHandlerInstance, ProcessBlocksInput}; use crate::cache::manager::RPCManager; use crate::event_handler::{process_events, EventHandlerInstance, ProcessEventsInput}; -use alloy::primitives::Address; + use ghost_crab_common::config::{self, Config, ConfigError}; -use tokio::sync::mpsc::error::SendError; -use tokio::sync::mpsc::{self, Receiver, Sender}; +use tokio::sync::mpsc::{self, Receiver}; -pub struct Template { - pub start_block: u64, - pub address: Address, - pub handler: EventHandlerInstance, -} - -#[derive(Clone)] -pub struct TemplateManager { - tx: Sender