Skip to content

Commit

Permalink
refacotr: handle bytearrays in systems
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Jun 3, 2024
1 parent d9c4f4f commit b3cc5d2
Showing 1 changed file with 45 additions and 30 deletions.
75 changes: 45 additions & 30 deletions crates/dojo-bindgen/src/plugins/unity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,43 @@ public class {} : ModelInstance {{
// Handled tokens should be a list of all structs and enums used by the contract
// Such as a set of referenced tokens from a model
fn format_system(system: &Function, handled_tokens: &[Composite]) -> String {
fn handle_arg_recursive(
type_name: &str,
token: &Token,
handled_tokens: &[Composite],
) -> String {
match handled_tokens.iter().find(|t| t.type_name() == token.type_name()) {
Some(t) => {
// Need to flatten the struct members.
match t.r#type {
CompositeType::Struct if t.type_name() == "ByteArray" => format!(
"calldata.AddRange(ByteArray.Serialize({}))",
type_name
),
CompositeType::Struct => t
.inners
.iter()
.map(|field| {
handle_arg_recursive(
&format!("{}.{}", type_name, field.name),
&field.token,
handled_tokens,
)
})
.collect::<Vec<String>>()
.join(",\n "),
_ => {
format!("calldata.Add(new FieldElement({}).Inner)", type_name)
}
}
}
None => match UnityPlugin::map_type(token).as_str() {
"FieldElement" => format!("calldata.Add({}.Inner)", type_name),
_ => format!("calldata.Add(new FieldElement({}).Inner)", type_name),
}
}
}

let args = system
.inputs
.iter()
Expand All @@ -244,49 +281,27 @@ public class {} : ModelInstance {{
let calldata = system
.inputs
.iter()
.map(|arg| {
let token = &arg.1;
let type_name = &arg.0;

match handled_tokens.iter().find(|t| t.type_name() == token.type_name()) {
Some(t) => {
// Need to flatten the struct members.
match t.r#type {
CompositeType::Struct => t
.inners
.iter()
.map(|field| {
format!("new FieldElement({}.{}).Inner", type_name, field.name)
})
.collect::<Vec<String>>()
.join(",\n "),
_ => {
format!("new FieldElement({}).Inner", type_name)
}
}
}
None => match UnityPlugin::map_type(token).as_str() {
"FieldElement" => format!("{}.Inner", type_name),
_ => format!("new FieldElement({}).Inner", type_name),
},
}
.map(|(name, token)| {
handle_arg_recursive(name, token, handled_tokens)
})
.collect::<Vec<String>>()
.join(",\n ");
.join(";\n\t\t");

format!(
"
// Call the `{system_name}` system with the specified Account and calldata
// Returns the transaction hash. Use `WaitForTransaction` to wait for the transaction to be \
confirmed.
public async Task<FieldElement> {system_name}(Account account{arg_sep}{args}) {{
List<FieldElement> calldata = new List<FieldElement>();
{calldata}
return await account.ExecuteRaw(new dojo.Call[] {{
new dojo.Call{{
to = contractAddress,
selector = \"{system_name}\",
calldata = new dojo.FieldElement[] {{
{calldata}
}}
calldata = calldata.ToArray()
}}
}});
}}
Expand Down

0 comments on commit b3cc5d2

Please sign in to comment.