Skip to content

Commit

Permalink
fix: all bindgen functionns external instead of view (#2725)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed authored Nov 26, 2024
1 parent cc82661 commit 1aabd5f
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions crates/dojo/bindgen/src/plugins/typescript/generator/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cainome::parser::tokens::{CompositeType, Function, Token};
use cainome::parser::tokens::{CompositeType, Function, StateMutability, Token};
use convert_case::{Case, Casing};
use dojo_world::contracts::naming;

Expand Down Expand Up @@ -35,8 +35,9 @@ impl TsFunctionGenerator {
contract_name: &str,
token: &Function,
) -> String {
format!(
"\tconst {contract_name}_{} = async ({}) => {{
match token.state_mutability {
StateMutability::External => format!(
"\tconst {contract_name}_{} = async ({}) => {{
\t\ttry {{
\t\t\treturn await provider.execute(
\t\t\t\tsnAccount,
Expand All @@ -51,15 +52,36 @@ impl TsFunctionGenerator {
\t\t\tconsole.error(error);
\t\t}}
\t}};\n",
token.name.to_case(Case::Camel),
self.format_function_inputs(token),
token.name,
self.format_function_calldata(token)
)
token.name.to_case(Case::Camel),
self.format_function_inputs(token),
token.name,
self.format_function_calldata(token)
),
StateMutability::View => format!(
"\tconst {contract_name}_{} = async ({}) => {{
\t\ttry {{
\t\t\treturn await provider.call(\"{namespace}\", {{
\t\t\t\tcontractName: \"{contract_name}\",
\t\t\t\tentrypoint: \"{}\",
\t\t\t\tcalldata: [{}],
\t\t\t}});
\t\t}} catch (error) {{
\t\t\tconsole.error(error);
\t\t}}
\t}};\n",
token.name.to_case(Case::Camel),
self.format_function_inputs(token),
token.name,
self.format_function_calldata(token)
),
}
}

fn format_function_inputs(&self, token: &Function) -> String {
let inputs = vec!["snAccount: Account".to_owned()];
let inputs = match token.state_mutability {
StateMutability::External => vec!["snAccount: Account".to_owned()],
StateMutability::View => Vec::new(),
};
token
.inputs
.iter()
Expand Down Expand Up @@ -273,6 +295,13 @@ mod tests {
let expected = "snAccount: Account, value: BigNumberish";
assert_eq!(expected, generator.format_function_inputs(&function))
}
#[test]
fn test_format_function_inputs_view() {
let generator = TsFunctionGenerator {};
let function = create_basic_view_function();
let expected = "";
assert_eq!(expected, generator.format_function_inputs(&function))
}

#[test]
fn test_format_function_inputs_complex() {
Expand Down Expand Up @@ -408,6 +437,17 @@ mod tests {
)],
)
}

fn create_basic_view_function() -> Function {
Function {
name: "allowance".to_owned(),
state_mutability: cainome::parser::tokens::StateMutability::View,
inputs: vec![],
outputs: vec![],
named_outputs: vec![],
}
}

fn create_change_theme_function_camelized() -> Function {
create_test_function(
"changeTheme",
Expand Down

0 comments on commit 1aabd5f

Please sign in to comment.