Skip to content

Commit

Permalink
feat: add i128 + use BigNumberish type
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed Nov 20, 2024
1 parent d005475 commit 6a9e5ca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -268,15 +269,15 @@ 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))
}

#[test]
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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
fn generate(&self, token: &Composite, buffer: &mut Buffer) -> BindgenResult<String> {
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} {{
Expand Down Expand Up @@ -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"
);
}

Expand Down
26 changes: 14 additions & 12 deletions crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
Expand Down Expand Up @@ -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()),
}
Expand Down Expand Up @@ -192,21 +194,21 @@ 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() }))
)
}

#[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![
Expand All @@ -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 {
Expand Down

0 comments on commit 6a9e5ca

Please sign in to comment.