Skip to content

Commit

Permalink
chore: Use Address instead of String and adds ".address()" to the han…
Browse files Browse the repository at this point in the history
…dler
  • Loading branch information
luis-herasme committed Jul 19, 2024
1 parent 286501c commit 93e2c51
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
14 changes: 14 additions & 0 deletions ghost-crab-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,29 @@ fn create_handler(metadata: TokenStream, input: TokenStream, is_template: bool)
let abi;
let network;
let execution_mode;
let address;

if is_template {
let source = config.templates.get(&name).expect("Source not found.");

abi = source.abi.clone();
network = source.network.clone();
execution_mode = source.execution_mode.clone().unwrap_or(ExecutionMode::Parallel);
address = quote! {
Address::ZERO
}
} else {
let source = config.data_sources.get(&name).expect("Source not found.");

abi = source.abi.clone();
network = source.network.clone();
execution_mode = source.execution_mode.clone().unwrap_or(ExecutionMode::Parallel);

let address_literal = Literal::string(&source.address);

address = quote! {
address!(#address_literal)
}
};

let rpc_url = config.networks.get(&network).expect("RPC url not found for network");
Expand Down Expand Up @@ -217,6 +227,10 @@ fn create_handler(metadata: TokenStream, input: TokenStream, is_template: bool)
#is_template
}

fn address(&self) -> Address {
#address
}

fn network(&self) - String {
String::from(#network)
}
Expand Down
3 changes: 2 additions & 1 deletion ghost-crab/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub trait Handler {
async fn handle(&self, params: Context);
fn get_source(&self) -> String;
fn is_template(&self) -> bool;
fn address(&self) -> Address;
fn network(&self) -> String;
fn rpc_url(&self) -> String;
fn execution_mode(&self) -> ExecutionMode;
Expand All @@ -31,7 +32,7 @@ pub trait Handler {
pub struct HandlerConfig {
pub start_block: u64,
pub step: u64,
pub address: String,
pub address: Address,
pub handler: HandleInstance,
pub templates: TemplateManager,
}
6 changes: 4 additions & 2 deletions ghost-crab/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::config;
use crate::handler::{HandleInstance, HandlerConfig};
use crate::process_logs::process_logs;
use crate::server::Server;
use alloy::primitives::Address;
use tokio::sync::mpsc::error::SendError;
use tokio::sync::mpsc::{self, Receiver, Sender};

Expand All @@ -13,7 +14,7 @@ pub struct TemplateManager {

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

Expand Down Expand Up @@ -70,10 +71,11 @@ impl Indexer {
}

let source = self.config.data_sources.get(&handler.get_source()).unwrap();
let address = handler.address();

self.handlers.push(HandlerConfig {
start_block: source.start_block,
address: source.address.clone(),
address,
step: 10_000,
handler,
templates: self.templates.clone(),
Expand Down
1 change: 1 addition & 0 deletions ghost-crab/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ 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;
3 changes: 0 additions & 3 deletions ghost-crab/src/process_logs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::cache::manager::RPC_MANAGER;
use crate::handler::{Context, HandlerConfig};
use crate::latest_block_manager::LatestBlockManager;
use alloy::primitives::Address;
use alloy::providers::Provider;
use alloy::rpc::types::eth::Filter;
use alloy::transports::TransportError;
Expand All @@ -18,8 +17,6 @@ pub async fn process_logs(

let provider = RPC_MANAGER.lock().await.get_or_create(network, rpc_url).await;
let mut current_block = start_block;
let address = address.parse::<Address>().unwrap();

let mut latest_block_manager =
LatestBlockManager::new(provider.clone(), Duration::from_secs(10));

Expand Down

0 comments on commit 93e2c51

Please sign in to comment.