Skip to content

Commit

Permalink
fix(sozo): events subcommand (dojoengine#1915)
Browse files Browse the repository at this point in the history
* fix: dont ignore --json flag

* fix events subcommand
  • Loading branch information
lambda-0x authored May 2, 2024
1 parent 8f8bdd3 commit 90035c1
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 34 deletions.
5 changes: 3 additions & 2 deletions crates/sozo/ops/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ pub async fn parse(

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

Ok(())
}

Expand Down Expand Up @@ -107,7 +108,7 @@ fn extract_events(
}
}

for model in &manifest.contracts {
for model in &manifest.models {
if let Some(AbiFormat::Path(abi_path)) = model.inner.abi() {
let full_abi_path = manifest_dir.join(abi_path);
process_abi(&mut events_map, &full_abi_path)?;
Expand Down
71 changes: 42 additions & 29 deletions crates/sozo/ops/src/migration/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ where
pub async fn update_manifests_and_abis(
ws: &Workspace<'_>,
local_manifest: BaseManifest,
profile_dir: &Utf8PathBuf,
manifest_dir: &Utf8PathBuf,
profile_name: &str,
rpc_url: &str,
world_address: FieldElement,
Expand All @@ -745,8 +745,10 @@ pub async fn update_manifests_and_abis(
let ui = ws.config().ui();
ui.print_step(5, "✨", "Updating manifests...");

let deployed_path = profile_dir.join("manifest").with_extension("toml");
let deployed_path_json = profile_dir.join("manifest").with_extension("json");
let deployed_path =
manifest_dir.join(MANIFESTS_DIR).join(profile_name).join("manifest").with_extension("toml");
let deployed_path_json =
manifest_dir.join(MANIFESTS_DIR).join(profile_name).join("manifest").with_extension("json");

let mut local_manifest: DeploymentManifest = local_manifest.into();

Expand Down Expand Up @@ -796,54 +798,65 @@ pub async fn update_manifests_and_abis(

// copy abi files from `abi/base` to `abi/deployments/{chain_id}` and update abi path in
// local_manifest
update_manifest_abis(&mut local_manifest, profile_dir, profile_name).await;
update_manifest_abis(&mut local_manifest, manifest_dir, profile_name).await;

local_manifest.write_to_path_toml(&deployed_path)?;
local_manifest.write_to_path_json(&deployed_path_json, profile_dir)?;
local_manifest.write_to_path_json(&deployed_path_json, manifest_dir)?;
ui.print("\n✨ Done.");

Ok(())
}

async fn update_manifest_abis(
local_manifest: &mut DeploymentManifest,
profile_dir: &Utf8PathBuf,
manifest_dir: &Utf8PathBuf,
profile_name: &str,
) {
fs::create_dir_all(profile_dir.join(ABIS_DIR).join(DEPLOYMENTS_DIR))
.await
.expect("Failed to create folder");
fs::create_dir_all(
manifest_dir.join(MANIFESTS_DIR).join(profile_name).join(ABIS_DIR).join(DEPLOYMENTS_DIR),
)
.await
.expect("Failed to create folder");

async fn inner_helper<T>(
profile_dir: &Utf8PathBuf,
manifest_dir: &Utf8PathBuf,
profile_name: &str,
manifest: &mut Manifest<T>,
) where
T: ManifestMethods,
{
// for example:
// from: manifests/dev/abis/base/contract/dojo_world_world.json
// to: manifests/dev/abis/deployments/contract/dojo_world_world.json
//
// Unwraps in call to abi is safe because we always write abis for DojoContracts as relative
// path.
// In this relative path, we only what the root from
// ABI directory.
let base_relative_path = manifest
.inner
.abi()
.unwrap()
.to_path()
.unwrap()
.strip_prefix(Utf8PathBuf::new().join(MANIFESTS_DIR).join(profile_name))

// manifests/dev/abis/base/contract/dojo_world_world.json
let base_relative_path = manifest.inner.abi().unwrap().to_path().unwrap();

// contract/dojo_world_world.json
let stripped_path = base_relative_path
.strip_prefix(
Utf8PathBuf::new()
.join(MANIFESTS_DIR)
.join(profile_name)
.join(ABIS_DIR)
.join(BASE_DIR),
)
.unwrap();

// The filename is safe to unwrap as it's always
// present in the base relative path.
let deployed_relative_path = Utf8PathBuf::new().join(ABIS_DIR).join(DEPLOYMENTS_DIR).join(
base_relative_path
.strip_prefix(Utf8PathBuf::new().join(ABIS_DIR).join(BASE_DIR))
.unwrap(),
);
let deployed_relative_path = Utf8PathBuf::new()
.join(MANIFESTS_DIR)
.join(profile_name)
.join(ABIS_DIR)
.join(DEPLOYMENTS_DIR)
.join(stripped_path);

let full_base_path = profile_dir.join(base_relative_path);
let full_deployed_path = profile_dir.join(deployed_relative_path.clone());
let full_base_path = manifest_dir.join(base_relative_path);
let full_deployed_path = manifest_dir.join(deployed_relative_path.clone());

fs::create_dir_all(full_deployed_path.parent().unwrap())
.await
Expand All @@ -854,14 +867,14 @@ async fn update_manifest_abis(
manifest.inner.set_abi(Some(AbiFormat::Path(deployed_relative_path)));
}

inner_helper::<ManifestWorldContract>(profile_dir, profile_name, &mut local_manifest.world)
inner_helper::<ManifestWorldContract>(manifest_dir, profile_name, &mut local_manifest.world)
.await;

for contract in local_manifest.contracts.iter_mut() {
inner_helper::<DojoContract>(profile_dir, profile_name, contract).await;
inner_helper::<DojoContract>(manifest_dir, profile_name, contract).await;
}

for model in local_manifest.models.iter_mut() {
inner_helper::<DojoModel>(profile_dir, profile_name, model).await;
inner_helper::<DojoModel>(manifest_dir, profile_name, model).await;
}
}
6 changes: 3 additions & 3 deletions crates/sozo/ops/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
update_manifests_and_abis(
ws,
local_manifest,
&profile_dir,
&manifest_dir,
&profile_name,
&rpc_url,
world_address,
Expand All @@ -113,7 +113,7 @@ where
update_manifests_and_abis(
ws,
local_manifest,
&profile_dir,
&manifest_dir,
&profile_name,
&rpc_url,
world_address,
Expand All @@ -128,7 +128,7 @@ where
update_manifests_and_abis(
ws,
local_manifest.clone(),
&profile_dir,
&manifest_dir,
&profile_name,
&rpc_url,
world_address,
Expand Down
105 changes: 105 additions & 0 deletions examples/spawn-and-move/manifests/dev/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
[world]
kind = "WorldContract"
class_hash = "0x799bc4e9da10bfb3dd88e6f223c9cfbf7745435cd14f5d69675ea448e578cd"
original_class_hash = "0x799bc4e9da10bfb3dd88e6f223c9cfbf7745435cd14f5d69675ea448e578cd"
abi = "manifests/dev/abis/deployments/dojo_world_world.json"
address = "0x1385f25d20a724edc9c7b3bd9636c59af64cbaf9fcd12f33b3af96b2452f295"
transaction_hash = "0x6afefdcc49b3563a4f3657900ba71e9f9356861b15b942a73f2018f046a1048"
block_number = 3
seed = "dojo_examples"
name = "dojo::world::world"

[world.metadata]
profile_name = "dev"
rpc_url = "http://localhost:5050/"

[base]
kind = "Class"
class_hash = "0x679177a2cb757694ac4f326d01052ff0963eac0bc2a17116a2b87badcdf6f76"
original_class_hash = "0x679177a2cb757694ac4f326d01052ff0963eac0bc2a17116a2b87badcdf6f76"
name = "dojo::base::base"

[[contracts]]
kind = "DojoContract"
address = "0x3539c9b89b08095ba914653fb0f20e55d4b172a415beade611bc260b346d0f7"
class_hash = "0x471a1a36581d24f68ae6984c714daa75d424c0a6581872a65e645eb11a8e45"
original_class_hash = "0x471a1a36581d24f68ae6984c714daa75d424c0a6581872a65e645eb11a8e45"
base_class_hash = "0x679177a2cb757694ac4f326d01052ff0963eac0bc2a17116a2b87badcdf6f76"
abi = "manifests/dev/abis/deployments/contracts/dojo_examples_actions_actions.json"
reads = []
writes = []
computed = []
name = "dojo_examples::actions::actions"

[[models]]
kind = "DojoModel"
class_hash = "0x52659850f9939482810d9f6b468b91dc99e0b7fa42c2016cf12833ec06ce911"
original_class_hash = "0x52659850f9939482810d9f6b468b91dc99e0b7fa42c2016cf12833ec06ce911"
abi = "manifests/dev/abis/deployments/models/dojo_examples_actions_actions_moved.json"
name = "dojo_examples::actions::actions::moved"

[[models.members]]
name = "player"
type = "ContractAddress"
key = true

[[models.members]]
name = "direction"
type = "Direction"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0x6d5aef0819f5267c840c57d37ff774b4e185fc7da4a10e58cb9f575aa2ed1c"
original_class_hash = "0x6d5aef0819f5267c840c57d37ff774b4e185fc7da4a10e58cb9f575aa2ed1c"
abi = "manifests/dev/abis/deployments/models/dojo_examples_models_emote_message.json"
name = "dojo_examples::models::emote_message"

[[models.members]]
name = "identity"
type = "ContractAddress"
key = true

[[models.members]]
name = "emote"
type = "Emote"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0x511fbd833938f5c4b743eea1e67605a125d7ff60e8a09e8dc227ad2fb59ca54"
original_class_hash = "0x511fbd833938f5c4b743eea1e67605a125d7ff60e8a09e8dc227ad2fb59ca54"
abi = "manifests/dev/abis/deployments/models/dojo_examples_models_moves.json"
name = "dojo_examples::models::moves"

[[models.members]]
name = "player"
type = "ContractAddress"
key = true

[[models.members]]
name = "remaining"
type = "u8"
key = false

[[models.members]]
name = "last_direction"
type = "Direction"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0xb33ae053213ccb2a57967ffc4411901f3efab24781ca867adcd0b90f2fece5"
original_class_hash = "0xb33ae053213ccb2a57967ffc4411901f3efab24781ca867adcd0b90f2fece5"
abi = "manifests/dev/abis/deployments/models/dojo_examples_models_position.json"
name = "dojo_examples::models::position"

[[models.members]]
name = "player"
type = "ContractAddress"
key = true

[[models.members]]
name = "vec"
type = "Vec2"
key = false

0 comments on commit 90035c1

Please sign in to comment.