Skip to content

Commit

Permalink
feat: add EventValue for ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Nov 8, 2024
1 parent d580acc commit 0bdcb48
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 94 deletions.
110 changes: 22 additions & 88 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ rpassword = "7.2.0"
rstest = "0.18.2"
rstest_reuse = "0.6.0"
salsa = "0.16.1"
scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
#scarb = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
#scarb-ui = { git = "https://github.com/dojoengine/scarb", rev = "a6d3b5b17b288502fe9cc63c96c0ae22fd175857" }
scarb = { path = "/Users/glihm/swm/scarb/scarb" }
scarb-ui = { path = "/Users/glihm/swm/scarb/utils/scarb-ui" }
semver = "1.0.5"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = { version = "1.0", features = [ "arbitrary_precision" ] }
Expand Down
1 change: 0 additions & 1 deletion crates/dojo/core/src/event/event.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ pub impl EventImpl<E, +ModelParser<E>, +EventDefinition<E>, +Serde<E>, +Introspe
dojo::utils::selector_from_namespace_and_name(namespace_hash, @Self::name())
}
}

16 changes: 13 additions & 3 deletions crates/dojo/lang/src/attribute_macros/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ impl DojoEvent {
});
}

let members_values = members
.iter()
.filter_map(|m| {
if m.key {
None
} else {
Some(RewriteNode::Text(format!("pub {}: {},\n", m.name, m.ty)))
}
})
.collect::<Vec<_>>();

let member_names = members
.iter()
.map(|member| RewriteNode::Text(format!("{},\n", member.name.clone())))
Expand All @@ -96,9 +107,7 @@ impl DojoEvent {
// and do not derive IntrospectPacked.
if derive_attr_names.contains(&DOJO_PACKED_DERIVE.to_string()) {
diagnostics.push(PluginDiagnostic {
message: format!(
"Event should derive {DOJO_INTROSPECT_DERIVE} instead of {DOJO_PACKED_DERIVE}."
),
message: format!("Deriving {DOJO_PACKED_DERIVE} on event is not allowed."),
stable_ptr: struct_ast.name(db).stable_ptr().untyped(),
severity: Severity::Error,
});
Expand All @@ -125,6 +134,7 @@ impl DojoEvent {
("serialized_keys".to_string(), RewriteNode::new_modified(serialized_keys)),
("serialized_values".to_string(), RewriteNode::new_modified(serialized_values)),
("unique_hash".to_string(), RewriteNode::Text(unique_hash)),
("members_values".to_string(), RewriteNode::new_modified(members_values)),
]),
);

Expand Down
16 changes: 16 additions & 0 deletions crates/dojo/lang/src/attribute_macros/patches/event.patch.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// EventValue on it's own does nothing since events are always emitted and
// never read from the storage. However, it's required by the ABI to
// ensure that the event definition contains both keys and values easily distinguishable.
// Only derives strictly required traits.
#[derive(Drop, Serde)]
pub struct $type_name$Value {
$members_values$
}

pub impl $type_name$Definition of dojo::event::EventDefinition<$type_name$>{
#[inline(always)]
Expand All @@ -24,6 +32,7 @@ pub impl $type_name$EventImpl = dojo::event::event::EventImpl<$type_name$>;
#[starknet::contract]
pub mod e_$type_name$ {
use super::$type_name$;
use super::$type_name$Value;

#[storage]
struct Storage {}
Expand All @@ -47,6 +56,13 @@ pub mod e_$type_name$ {
let _event = event;
}

// Outputs EventValue to allow a simple diff from the ABI compared to the
// event to retrieved the keys of an event.
#[external(v0)]
fn ensure_values(self: @ContractState, value: $type_name$Value) {
let _value = value;
}

// Ensures the generated contract has a unique classhash, using
// a hardcoded hash computed on event and member names.
#[external(v0)]
Expand Down
Binary file modified spawn-and-move-db.tar.gz
Binary file not shown.
Binary file modified types-test-db.tar.gz
Binary file not shown.

0 comments on commit 0bdcb48

Please sign in to comment.