Skip to content

Commit

Permalink
feat: Add ctx.block() to EventContext
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-herasme committed Jul 26, 2024
1 parent 8eca896 commit f8301bc
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ghost-crab/src/event_handler.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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]
Expand Down

0 comments on commit f8301bc

Please sign in to comment.