Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sozo): make event parsing logic modular and reusable #1556

Merged
merged 16 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/sozo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ tracing-log = "0.1.3"
tracing.workspace = true
url.workspace = true

cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.2.2" }

[dev-dependencies]
assert_fs = "1.0.10"
dojo-test-utils = { workspace = true, features = [ "build-examples" ] }
Expand Down
63 changes: 38 additions & 25 deletions bin/sozo/src/commands/events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::collections::HashMap;

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

use super::options::starknet::StarknetOptions;
Expand Down Expand Up @@ -56,7 +59,7 @@ impl EventsArgs {
return Err(anyhow!("Run scarb migrate before running this command"));
}

Some(extract_events(&Manifest::load_from_path(manifest_path)?))
Some(extract_events(&Manifest::load_from_path(manifest_path)?)?)
} else {
None
};
Expand All @@ -73,47 +76,57 @@ impl EventsArgs {
}
}

fn extract_events(manifest: &Manifest) -> HashMap<String, Vec<Event>> {
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());
fn is_event(token: &Token) -> bool {
match token {
Token::Composite(composite) => composite.is_event,
_ => false,
}
}

fn extract_events(manifest: &Manifest) -> Result<HashMap<String, Vec<Token>>> {
fn process_abi(
abi: &abi::Contract,
events_map: &mut HashMap<String, Vec<Token>>,
) -> Result<()> {
match serde_json::to_string(abi) {
Ok(abi_str) => match AbiParser::tokens_from_abi_string(&abi_str, &HashMap::new()) {
Ok(tokens) => {
for token in tokens.structs {
if is_event(&token) {
let event_name = starknet_keccak(token.type_name().as_bytes());

let vec = events_map.entry(event_name.to_string()).or_default();
vec.push(token.clone());
}
}
abi::EventKind::Enum { .. } => (),
}
}
Err(e) => return Err(anyhow!("Error parsing ABI: {}", e)),
},
Err(e) => return Err(anyhow!("Error serializing Contract to JSON: {}", e)),
}
Ok(())
}

let mut events_map = HashMap::new();

if let Some(abi) = manifest.world.abi.clone() {
inner_helper(&mut events_map, abi);
// Iterate over all ABIs in the manifest and process them
if let Some(abi) = manifest.world.abi.as_ref() {
process_abi(abi, &mut events_map)?;
}

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

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

events_map
Ok(events_map)
}

#[cfg(test)]
Expand All @@ -131,7 +144,7 @@ mod test {
#[test]
fn extract_events_work_as_expected() {
let manifest = Manifest::load_from_path("./tests/test_data/manifest.json").unwrap();
let result = extract_events(&manifest);
let result = extract_events(&manifest).unwrap();

// we are just collection all events from manifest file so just verifying count should work
assert!(result.len() == 13);
Expand Down
Loading
Loading