From be292627806633ba1b5dd3e14e6ca0082e43fb22 Mon Sep 17 00:00:00 2001 From: Valentin Dosimont Date: Wed, 20 Nov 2024 12:23:17 +0100 Subject: [PATCH 1/2] feat: add i128 + use BigNumberish type --- .../plugins/typescript/generator/function.rs | 10 +++---- .../plugins/typescript/generator/interface.rs | 12 ++++++--- .../src/plugins/typescript/generator/mod.rs | 26 ++++++++++--------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs b/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs index 321f971806..aca7aaf6c3 100644 --- a/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs +++ b/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs @@ -12,7 +12,7 @@ impl TsFunctionGenerator { fn check_imports(&self, buffer: &mut Buffer) { if !buffer.has("import { DojoProvider } from ") { buffer.insert(0, "import { DojoProvider } from \"@dojoengine/core\";".to_owned()); - buffer.insert(1, "import { Account } from \"starknet\";".to_owned()); + buffer.insert(1, "import { Account, BigNumberish } from \"starknet\";".to_owned()); buffer.insert(2, "import * as models from \"./models.gen\";\n".to_owned()); } } @@ -235,8 +235,8 @@ mod tests { fn test_generate_system_function() { let generator = TsFunctionGenerator {}; let function = create_change_theme_function(); - let expected = "\tconst actions_changeTheme = async (snAccount: Account, value: number) \ - => { + let expected = "\tconst actions_changeTheme = async (snAccount: Account, value: \ + BigNumberish) => { \t\ttry { \t\t\treturn await provider.execute( \t\t\t\tsnAccount, @@ -268,7 +268,7 @@ mod tests { fn test_format_function_inputs() { let generator = TsFunctionGenerator {}; let function = create_change_theme_function(); - let expected = "snAccount: Account, value: number"; + let expected = "snAccount: Account, value: BigNumberish"; assert_eq!(expected, generator.format_function_inputs(&function)) } @@ -276,7 +276,7 @@ mod tests { fn test_format_function_inputs_complex() { let generator = TsFunctionGenerator {}; let function = create_change_theme_function(); - let expected = "snAccount: Account, value: number"; + let expected = "snAccount: Account, value: BigNumberish"; assert_eq!(expected, generator.format_function_inputs(&function)) } diff --git a/crates/dojo/bindgen/src/plugins/typescript/generator/interface.rs b/crates/dojo/bindgen/src/plugins/typescript/generator/interface.rs index f75efadf94..111a082d3c 100644 --- a/crates/dojo/bindgen/src/plugins/typescript/generator/interface.rs +++ b/crates/dojo/bindgen/src/plugins/typescript/generator/interface.rs @@ -4,14 +4,20 @@ use super::JsType; use crate::error::BindgenResult; use crate::plugins::{BindgenModelGenerator, Buffer}; +const BIGNUMNERISH_IMPORT: &str = "import type { BigNumberish } from 'starknet';"; + pub(crate) struct TsInterfaceGenerator; impl BindgenModelGenerator for TsInterfaceGenerator { - fn generate(&self, token: &Composite, _buffer: &mut Buffer) -> BindgenResult { + fn generate(&self, token: &Composite, buffer: &mut Buffer) -> BindgenResult { if token.r#type != CompositeType::Struct || token.inners.is_empty() { return Ok(String::new()); } + if !buffer.has(BIGNUMNERISH_IMPORT) { + buffer.push(BIGNUMNERISH_IMPORT.to_owned()); + } + Ok(format!( "// Type definition for `{path}` struct export interface {name} {{ @@ -82,8 +88,8 @@ mod tests { assert_eq!( result, "// Type definition for `core::test::TestStruct` struct\nexport interface TestStruct \ - {\n\tfieldOrder: string[];\n\tfield1: number;\n\tfield2: number;\n\tfield3: \ - number;\n}\n" + {\n\tfieldOrder: string[];\n\tfield1: BigNumberish;\n\tfield2: \ + BigNumberish;\n\tfield3: BigNumberish;\n}\n" ); } diff --git a/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs b/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs index 2c33e067c2..6abfa5c682 100644 --- a/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs +++ b/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs @@ -54,16 +54,17 @@ pub(crate) struct JsType(String); impl From<&str> for JsType { fn from(value: &str) -> Self { match value { - "felt252" => JsType("number".to_owned()), + "felt252" => JsType("BigNumberish".to_owned()), "ContractAddress" => JsType("string".to_owned()), "ByteArray" => JsType("string".to_owned()), - "u8" => JsType("number".to_owned()), - "u16" => JsType("number".to_owned()), - "u32" => JsType("number".to_owned()), - "u64" => JsType("number".to_owned()), - "u128" => JsType("number".to_owned()), - "u256" => JsType("number".to_owned()), - "U256" => JsType("number".to_owned()), + "u8" => JsType("BigNumberish".to_owned()), + "u16" => JsType("BigNumberish".to_owned()), + "u32" => JsType("BigNumberish".to_owned()), + "u64" => JsType("BigNumberish".to_owned()), + "u128" => JsType("BigNumberish".to_owned()), + "u256" => JsType("BigNumberish".to_owned()), + "U256" => JsType("BigNumberish".to_owned()), + "i128" => JsType("BigNumberish".to_owned()), "bool" => JsType("boolean".to_owned()), _ => JsType(value.to_owned()), } @@ -112,6 +113,7 @@ impl From<&str> for JsDefaultValue { "u128" => JsDefaultValue("0".to_string()), "u256" => JsDefaultValue("0".to_string()), "U256" => JsDefaultValue("0".to_string()), + "i128" => JsDefaultValue("0".to_string()), "bool" => JsDefaultValue("false".to_string()), _ => JsDefaultValue(value.to_string()), } @@ -192,13 +194,13 @@ mod tests { #[test] fn test_js_type_basics() { assert_eq!( - "number", + "BigNumberish", JsType::from(&Token::CoreBasic(CoreBasic { type_path: "core::integer::u8".to_owned() })) ); assert_eq!( - "number", + "BigNumberish", JsType::from(&Token::CoreBasic(CoreBasic { type_path: "core::felt252".to_owned() })) ) } @@ -206,7 +208,7 @@ mod tests { #[test] fn test_tuple_type() { assert_eq!( - "[number, number]", + "[BigNumberish, BigNumberish]", JsType::from(&Token::Tuple(Tuple { type_path: "(core::integer::u8,core::integer::u128)".to_owned(), inners: vec![ @@ -220,7 +222,7 @@ mod tests { #[test] fn test_array_type() { assert_eq!( - "Array<[number, number]>", + "Array<[BigNumberish, BigNumberish]>", JsType::from(&Token::Array(Array { type_path: "core::array::Span<(core::integer::u8,core::integer::u128)>".to_owned(), inner: Box::new(Token::Tuple(Tuple { From 33d0043b5aca23327460cefd0ff574fbd9d43261 Mon Sep 17 00:00:00 2001 From: Valentin Dosimont Date: Thu, 21 Nov 2024 11:09:49 +0100 Subject: [PATCH 2/2] fix: move string declarations to variables --- .../plugins/typescript/generator/constants.rs | 16 +++++ .../plugins/typescript/generator/function.rs | 4 +- .../src/plugins/typescript/generator/mod.rs | 63 ++++++++++--------- 3 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 crates/dojo/bindgen/src/plugins/typescript/generator/constants.rs diff --git a/crates/dojo/bindgen/src/plugins/typescript/generator/constants.rs b/crates/dojo/bindgen/src/plugins/typescript/generator/constants.rs new file mode 100644 index 0000000000..24f3dc3772 --- /dev/null +++ b/crates/dojo/bindgen/src/plugins/typescript/generator/constants.rs @@ -0,0 +1,16 @@ +pub const CAIRO_FELT252: &str = "felt252"; +pub const CAIRO_CONTRACT_ADDRESS: &str = "ContractAddress"; +pub const CAIRO_BYTE_ARRAY: &str = "ByteArray"; +pub const CAIRO_U8: &str = "u8"; +pub const CAIRO_U16: &str = "u16"; +pub const CAIRO_U32: &str = "u32"; +pub const CAIRO_U64: &str = "u64"; +pub const CAIRO_U128: &str = "u128"; +pub const CAIRO_U256: &str = "u256"; +pub const CAIRO_U256_STRUCT: &str = "U256"; +pub const CAIRO_I128: &str = "i128"; +pub const CAIRO_BOOL: &str = "bool"; + +pub const JS_BOOLEAN: &str = "boolean"; +pub const JS_STRING: &str = "string"; +pub const JS_BIGNUMBERISH: &str = "BigNumberish"; diff --git a/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs b/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs index aca7aaf6c3..c30cf4ff57 100644 --- a/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs +++ b/crates/dojo/bindgen/src/plugins/typescript/generator/function.rs @@ -2,6 +2,7 @@ use cainome::parser::tokens::{CompositeType, Function, Token}; use convert_case::{Case, Casing}; use dojo_world::contracts::naming; +use super::constants::JS_BIGNUMBERISH; use super::JsType; use crate::error::BindgenResult; use crate::plugins::{BindgenContractGenerator, Buffer}; @@ -12,7 +13,8 @@ impl TsFunctionGenerator { fn check_imports(&self, buffer: &mut Buffer) { if !buffer.has("import { DojoProvider } from ") { buffer.insert(0, "import { DojoProvider } from \"@dojoengine/core\";".to_owned()); - buffer.insert(1, "import { Account, BigNumberish } from \"starknet\";".to_owned()); + buffer + .insert(1, format!("import {{ Account, {} }} from \"starknet\";", JS_BIGNUMBERISH)); buffer.insert(2, "import * as models from \"./models.gen\";\n".to_owned()); } } diff --git a/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs b/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs index 6abfa5c682..49978627fe 100644 --- a/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs +++ b/crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs @@ -1,6 +1,12 @@ use cainome::parser::tokens::{Composite, Token}; +use constants::{ + CAIRO_BOOL, CAIRO_BYTE_ARRAY, CAIRO_CONTRACT_ADDRESS, CAIRO_FELT252, CAIRO_I128, CAIRO_U128, + CAIRO_U16, CAIRO_U256, CAIRO_U256_STRUCT, CAIRO_U32, CAIRO_U64, CAIRO_U8, JS_BIGNUMBERISH, + JS_BOOLEAN, JS_STRING, +}; use convert_case::{Case, Casing}; +pub(crate) mod constants; pub(crate) mod r#enum; pub(crate) mod erc; pub(crate) mod function; @@ -54,18 +60,18 @@ pub(crate) struct JsType(String); impl From<&str> for JsType { fn from(value: &str) -> Self { match value { - "felt252" => JsType("BigNumberish".to_owned()), - "ContractAddress" => JsType("string".to_owned()), - "ByteArray" => JsType("string".to_owned()), - "u8" => JsType("BigNumberish".to_owned()), - "u16" => JsType("BigNumberish".to_owned()), - "u32" => JsType("BigNumberish".to_owned()), - "u64" => JsType("BigNumberish".to_owned()), - "u128" => JsType("BigNumberish".to_owned()), - "u256" => JsType("BigNumberish".to_owned()), - "U256" => JsType("BigNumberish".to_owned()), - "i128" => JsType("BigNumberish".to_owned()), - "bool" => JsType("boolean".to_owned()), + CAIRO_FELT252 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_CONTRACT_ADDRESS => JsType(JS_STRING.to_owned()), + CAIRO_BYTE_ARRAY => JsType(JS_STRING.to_owned()), + CAIRO_U8 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_U16 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_U32 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_U64 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_U128 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_U256 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_U256_STRUCT => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_I128 => JsType(JS_BIGNUMBERISH.to_owned()), + CAIRO_BOOL => JsType(JS_BOOLEAN.to_owned()), _ => JsType(value.to_owned()), } } @@ -103,18 +109,18 @@ pub(crate) struct JsDefaultValue(String); impl From<&str> for JsDefaultValue { fn from(value: &str) -> Self { match value { - "felt252" => JsDefaultValue("0".to_string()), - "ContractAddress" => JsDefaultValue("\"\"".to_string()), - "ByteArray" => JsDefaultValue("\"\"".to_string()), - "u8" => JsDefaultValue("0".to_string()), - "u16" => JsDefaultValue("0".to_string()), - "u32" => JsDefaultValue("0".to_string()), - "u64" => JsDefaultValue("0".to_string()), - "u128" => JsDefaultValue("0".to_string()), - "u256" => JsDefaultValue("0".to_string()), - "U256" => JsDefaultValue("0".to_string()), - "i128" => JsDefaultValue("0".to_string()), - "bool" => JsDefaultValue("false".to_string()), + CAIRO_FELT252 => JsDefaultValue("0".to_string()), + CAIRO_CONTRACT_ADDRESS => JsDefaultValue("\"\"".to_string()), + CAIRO_BYTE_ARRAY => JsDefaultValue("\"\"".to_string()), + CAIRO_U8 => JsDefaultValue("0".to_string()), + CAIRO_U16 => JsDefaultValue("0".to_string()), + CAIRO_U32 => JsDefaultValue("0".to_string()), + CAIRO_U64 => JsDefaultValue("0".to_string()), + CAIRO_U128 => JsDefaultValue("0".to_string()), + CAIRO_U256 => JsDefaultValue("0".to_string()), + CAIRO_U256_STRUCT => JsDefaultValue("0".to_string()), + CAIRO_I128 => JsDefaultValue("0".to_string()), + CAIRO_BOOL => JsDefaultValue("false".to_string()), _ => JsDefaultValue(value.to_string()), } } @@ -177,6 +183,7 @@ mod tests { Tuple, }; + use crate::plugins::typescript::generator::constants::JS_BIGNUMBERISH; use crate::plugins::typescript::generator::{generate_type_init, JsDefaultValue, JsType}; impl PartialEq for &str { @@ -194,13 +201,13 @@ mod tests { #[test] fn test_js_type_basics() { assert_eq!( - "BigNumberish", + JS_BIGNUMBERISH, JsType::from(&Token::CoreBasic(CoreBasic { type_path: "core::integer::u8".to_owned() })) ); assert_eq!( - "BigNumberish", + JS_BIGNUMBERISH, JsType::from(&Token::CoreBasic(CoreBasic { type_path: "core::felt252".to_owned() })) ) } @@ -208,7 +215,7 @@ mod tests { #[test] fn test_tuple_type() { assert_eq!( - "[BigNumberish, BigNumberish]", + format!("[{}, {}]", JS_BIGNUMBERISH, JS_BIGNUMBERISH).as_str(), JsType::from(&Token::Tuple(Tuple { type_path: "(core::integer::u8,core::integer::u128)".to_owned(), inners: vec![ @@ -222,7 +229,7 @@ mod tests { #[test] fn test_array_type() { assert_eq!( - "Array<[BigNumberish, BigNumberish]>", + format!("Array<[{}, {}]>", JS_BIGNUMBERISH, JS_BIGNUMBERISH).as_str(), JsType::from(&Token::Array(Array { type_path: "core::array::Span<(core::integer::u8,core::integer::u128)>".to_owned(), inner: Box::new(Token::Tuple(Tuple {