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

feat: add event selector for event structs #69

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions contracts/src/gen.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ mod gen {
v2: felt252,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
E1: E1,
}

#[derive(Drop, starknet::Event)]
struct E1 {
#[key]
key: felt252,
value: Span<felt252>,
}

#[derive(Serde, Drop)]
struct PlainStruct {
f1: u8,
Expand Down
17 changes: 4 additions & 13 deletions crates/rs/src/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ impl CairoEnumEvent {
}
}
}

// TODO:
// For each enum in enums -> check if it's an event.
// if yes ->
// 1. impl a function to retrieve the selector + string name of the event.
// 2. impl `TryFrom` EmittedEvent. Need to take in account the new flat keyword.
// - if nested => the selector is the name of the enum variant.
// - if nested and the type it points to is also an enum => first selector is
// the name of the variant of the current enum, and then we've to check
// recursively until the event type is a struct and not an enum.
// - if it's flat, we just take the name of the current variant.
}

pub fn expand_event_enum(
Expand All @@ -65,6 +54,8 @@ impl CairoEnumEvent {
let event_name_str = composite.type_name_or_alias();
let event_name = utils::str_to_ident(&composite.type_name_or_alias());

let snrs_utils = utils::snrs_utils();

for variant in &composite.inners {
let selector_key_offset = utils::str_to_litint(&depth.to_string());

Expand Down Expand Up @@ -117,7 +108,7 @@ impl CairoEnumEvent {

quote! {
let selector = event.keys[#selector_key_offset];
if selector == starknet::core::utils::get_selector_from_name(#variant_name_str).unwrap_or_else(|_| panic!("Invalid selector for {}", #variant_name_str)) {
if selector == #snrs_utils::get_selector_from_name(#variant_name_str).unwrap_or_else(|_| panic!("Invalid selector for {}", #variant_name_str)) {
#inner_content
}
}
Expand All @@ -143,7 +134,7 @@ impl CairoEnumEvent {

quote! {
let selector = event.keys[#selector_key_offset];
if selector == starknet::core::utils::get_selector_from_name(#variant_name_str).unwrap_or_else(|_| panic!("Invalid selector for {}", #variant_name_str)) {
if selector == #snrs_utils::get_selector_from_name(#variant_name_str).unwrap_or_else(|_| panic!("Invalid selector for {}", #variant_name_str)) {
let mut key_offset = #selector_key_offset + 1;
let mut data_offset = 0;

Expand Down
28 changes: 25 additions & 3 deletions crates/rs/src/expand/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl CairoStruct {
}

let struct_name = utils::str_to_ident(&composite.type_name_or_alias());
let struct_name_str = utils::str_to_litstr(&composite.type_name_or_alias());

let mut sizes: Vec<TokenStream2> = vec![];
let mut sers: Vec<TokenStream2> = vec![];
Expand Down Expand Up @@ -154,6 +155,25 @@ impl CairoStruct {
}

let ccs = utils::cainome_cairo_serde();
let snrs_types = utils::snrs_types();
let snrs_utils = utils::snrs_utils();

let event_impl = if composite.is_event {
quote! {
impl #struct_name {
pub fn event_selector() -> #snrs_types::Felt {
// Ok to unwrap since the event name comes from the ABI, which is already validated.
#snrs_utils::get_selector_from_name(#struct_name_str).unwrap()
}

pub fn event_name() -> &'static str {
#struct_name_str
}
}
}
} else {
quote!()
};

let (impl_line, rust_type) = if composite.is_generic() {
let gen_args: Vec<Ident> = composite
Expand Down Expand Up @@ -189,20 +209,22 @@ impl CairoStruct {
__size
}

fn cairo_serialize(__rust: &Self::RustType) -> Vec<starknet::core::types::Felt> {
let mut __out: Vec<starknet::core::types::Felt> = vec![];
fn cairo_serialize(__rust: &Self::RustType) -> Vec<#snrs_types::Felt> {
let mut __out: Vec<#snrs_types::Felt> = vec![];
#(#sers)*
__out
}

fn cairo_deserialize(__felts: &[starknet::core::types::Felt], __offset: usize) -> #ccs::Result<Self::RustType> {
fn cairo_deserialize(__felts: &[#snrs_types::Felt], __offset: usize) -> #ccs::Result<Self::RustType> {
let mut __offset = __offset;
#(#desers)*
Ok(#struct_name {
#(#names),*
})
}
}

#event_impl
}
}
}
4 changes: 4 additions & 0 deletions crates/rs/src/expand/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub fn snrs_types() -> Type {
str_to_type("starknet::core::types")
}

pub fn snrs_utils() -> Type {
str_to_type("starknet::core::utils")
}

pub fn snrs_accounts() -> Type {
str_to_type("starknet::accounts")
}
Expand Down
10 changes: 10 additions & 0 deletions examples/structs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use cainome::rs::abigen;
use paste::paste;
use starknet::core::types::Felt;
Expand Down Expand Up @@ -27,6 +29,14 @@ macro_rules! test_enum {

#[tokio::main]
async fn main() {
assert_eq!(
E1::event_selector(),
Felt::from_str("0x00ba2026c84b59ce46a4007300eb97e3e275d4119261ee402d7a3eb40ad58807")
.unwrap()
);

assert_eq!(E1::event_name(), "E1");

let s = PlainStruct {
f1: 1,
f2: 2,
Expand Down
Loading