From 6a9e5ca314f2214074b7024dbb2a499e1e6e3cb3 Mon Sep 17 00:00:00 2001 From: Valentin Dosimont Date: Wed, 20 Nov 2024 12:23:17 +0100 Subject: [PATCH] feat: add i128 + use BigNumberish type --- .../plugins/typescript/generator/function.rs | 9 ++++--- .../plugins/typescript/generator/interface.rs | 12 ++++++--- .../src/plugins/typescript/generator/mod.rs | 26 ++++++++++--------- 3 files changed, 28 insertions(+), 19 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..45cc5d216c 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,7 +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( @@ -268,7 +269,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 +277,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..de3ce9b846 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 {