Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Jan 3, 2024
1 parent 8afd8c9 commit 4711301
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
79 changes: 49 additions & 30 deletions crates/sozo/src/commands/events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use cairo_lang_starknet::abi::{self, Event};
use cairo_lang_starknet::abi::{self, Event, Item};
use clap::Parser;
use dojo_world::manifest::Manifest;
use dojo_world::metadata::dojo_metadata_from_workspace;
use scarb::core::Config;
use starknet::core::utils::starknet_keccak;

use super::options::starknet::StarknetOptions;
use super::options::world::WorldOptions;
Expand Down Expand Up @@ -46,15 +47,20 @@ pub struct EventsArgs {

impl EventsArgs {
pub fn run(self, config: &Config) -> Result<()> {
let target_dir = config.target_dir_override().unwrap();
let manifest_path = target_dir.join(config.profile().as_str()).join("manifest.json");
let event_map = if !self.json {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let target_dir = ws.target_dir().path_existent()?;
let manifest_path = target_dir.join(config.profile().as_str()).join("manifest.json");

if !manifest_path.exists() {
return Err(anyhow!("Run scarb migrate before running this command"));
}
if !manifest_path.exists() {
return Err(anyhow!("Run scarb migrate before running this command"));
}

Some(extract_events(&Manifest::load_from_path(manifest_path)?))
} else {
None
};

let manifest = Manifest::load_from_path(manifest_path)?;
let events = extract_events(&manifest);
let env_metadata = if config.manifest_path().exists() {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;

Expand All @@ -63,40 +69,53 @@ impl EventsArgs {
} else {
None
};
config.tokio_handle().block_on(events::execute(self, env_metadata, events))

config.tokio_handle().block_on(events::execute(self, env_metadata, event_map))
}
}

fn extract_events(manifest: &Manifest) -> HashMap<String, Vec<Event>> {
fn inner_helper(_events: &mut HashMap<String, Vec<Event>>, _contract: &Option<abi::Contract>) {
// if let Some(contract) = contract {
// for item in &contract.items {
// if let Item::Event(e) = item {
// match e.kind {
// abi::EventKind::Struct { .. } => {
// let event_name =
// starknet_keccak(e.name.split("::").last().unwrap().as_bytes());
// let vec = events.entry(event_name.to_string()).or_default();
// vec.push(e.clone());
// }
// abi::EventKind::Enum { .. } => (),
// }
// }
// }
// }
fn inner_helper(events: &mut HashMap<String, Vec<Event>>, abi: abi::Contract) {
for item in abi.into_iter() {
if let Item::Event(e) = item {
match e.kind {
abi::EventKind::Struct { .. } => {
let event_name = starknet_keccak(
e.name
.split("::")
.last()
.expect("valid fully qualified name")
.as_bytes(),
);
let vec = events.entry(event_name.to_string()).or_default();
vec.push(e.clone());
}
abi::EventKind::Enum { .. } => (),
}
}
}
}

let mut events_map = HashMap::new();

inner_helper(&mut events_map, &manifest.world.abi);
inner_helper(&mut events_map, &manifest.executor.abi);
if let Some(abi) = manifest.world.abi.clone() {
inner_helper(&mut events_map, abi);
}

if let Some(abi) = manifest.executor.abi.clone() {
inner_helper(&mut events_map, abi);
}

for contract in &manifest.contracts {
inner_helper(&mut events_map, &contract.abi);
if let Some(abi) = contract.abi.clone() {
inner_helper(&mut events_map, abi);
}
}

for model in &manifest.models {
inner_helper(&mut events_map, &model.abi);
for model in &manifest.contracts {
if let Some(abi) = model.abi.clone() {
inner_helper(&mut events_map, abi);
}
}

events_map
Expand Down
19 changes: 10 additions & 9 deletions crates/sozo/src/ops/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::commands::events::EventsArgs;
pub async fn execute(
args: EventsArgs,
env_metadata: Option<Environment>,
events_map: HashMap<String, Vec<Event>>,
events_map: Option<HashMap<String, Vec<Event>>>,
) -> Result<()> {
let EventsArgs {
chunk_size,
Expand All @@ -23,7 +23,7 @@ pub async fn execute(
to_block,
events,
continuation_token,
json,
..
} = args;

let from_block = from_block.map(BlockId::Number);
Expand All @@ -38,29 +38,30 @@ pub async fn execute(

let res = provider.get_events(event_filter, continuation_token, chunk_size).await?;

if json {
let value = serde_json::to_value(res)?;
println!("{}", serde_json::to_string_pretty(&value)?);
if let Some(events_map) = events_map {
parse_and_print_events(res, events_map)?;
} else {
parse_and_print_events(res, events_map);
println!("{}", serde_json::to_string_pretty(&res)?);
}

Ok(())
}

fn parse_and_print_events(
res: starknet::core::types::EventsPage,
events_map: HashMap<String, Vec<Event>>,
) {
) -> Result<()> {
println!("Continuation token: {:?}", res.continuation_token);
println!("----------------------------------------------");
for event in res.events {
if let Some(e) = parse_event(event.clone(), &events_map) {
println!("{}\n", e);
println!("{e}");
} else {
// Couldn't parse event
println!("{}\n", serde_json::to_string_pretty(&event).unwrap());
println!("{}", serde_json::to_string_pretty(&event)?);
}
}
Ok(())
}

fn parse_event(
Expand Down

0 comments on commit 4711301

Please sign in to comment.