Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add i128 + use BigNumberish type #2705

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/dojo/bindgen/src/plugins/typescript/generator/constants.rs
Original file line number Diff line number Diff line change
@@ -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";
12 changes: 7 additions & 5 deletions crates/dojo/bindgen/src/plugins/typescript/generator/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 } 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());
}
}
Expand Down Expand Up @@ -235,8 +237,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,
Expand Down Expand Up @@ -268,15 +270,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
61 changes: 35 additions & 26 deletions crates/dojo/bindgen/src/plugins/typescript/generator/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -54,17 +60,18 @@ pub(crate) struct JsType(String);
impl From<&str> for JsType {
fn from(value: &str) -> Self {
match value {
"felt252" => JsType("number".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()),
"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()),
}
}
Expand Down Expand Up @@ -102,17 +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()),
"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()),
}
}
Expand Down Expand Up @@ -175,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<JsType> for &str {
Expand All @@ -192,21 +201,21 @@ mod tests {
#[test]
fn test_js_type_basics() {
assert_eq!(
"number",
JS_BIGNUMBERISH,
JsType::from(&Token::CoreBasic(CoreBasic {
type_path: "core::integer::u8".to_owned()
}))
);
assert_eq!(
"number",
JS_BIGNUMBERISH,
JsType::from(&Token::CoreBasic(CoreBasic { type_path: "core::felt252".to_owned() }))
)
}

#[test]
fn test_tuple_type() {
assert_eq!(
"[number, number]",
format!("[{}, {}]", JS_BIGNUMBERISH, JS_BIGNUMBERISH).as_str(),
JsType::from(&Token::Tuple(Tuple {
type_path: "(core::integer::u8,core::integer::u128)".to_owned(),
inners: vec![
Expand All @@ -220,7 +229,7 @@ mod tests {
#[test]
fn test_array_type() {
assert_eq!(
"Array<[number, number]>",
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 {
Expand Down
Loading