From f8301bcbd54e80b44e273158c6f246129faeb527 Mon Sep 17 00:00:00 2001 From: Luis Herasme <luis.alberto.herasme.cuevas@gmail.com> Date: Thu, 25 Jul 2024 20:11:34 -0400 Subject: [PATCH] feat: Add ctx.block() to EventContext --- ghost-crab/src/event_handler.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ghost-crab/src/event_handler.rs b/ghost-crab/src/event_handler.rs index e17b061..c4df51e 100644 --- a/ghost-crab/src/event_handler.rs +++ b/ghost-crab/src/event_handler.rs @@ -1,10 +1,12 @@ use crate::cache::manager::CacheProvider; use crate::indexer::TemplateManager; use crate::latest_block_manager::LatestBlockManager; +use alloy::eips::BlockNumberOrTag; use alloy::primitives::Address; use alloy::providers::Provider; use alloy::rpc::types::eth::Filter; use alloy::rpc::types::eth::Log; +use alloy::rpc::types::Block; use alloy::transports::TransportError; use async_trait::async_trait; use ghost_crab_common::config::ExecutionMode; @@ -18,6 +20,19 @@ pub struct EventContext { pub contract_address: Address, } +impl EventContext { + pub async fn block(&self) -> Result<Option<Block>, TransportError> { + match self.log.block_number { + Some(block_number) => { + self.provider + .get_block_by_number(BlockNumberOrTag::Number(block_number), false) + .await + } + None => Err(TransportError::local_usage_str("Error occurred while fetching the current block number within an EventHandler. The log.block_number value is None.")), + } + } +} + pub type EventHandlerInstance = Arc<Box<(dyn EventHandler + Send + Sync)>>; #[async_trait]