From 9fd4cc4d85989285bb3f74a9c1c6df23cb291ebf Mon Sep 17 00:00:00 2001 From: RareSecond Date: Fri, 19 Apr 2024 03:29:33 +0200 Subject: [PATCH] Update defaults for new TypeScript bindgen (#1846) * Updated imports * Filled in default values for rpc, torii and relay * Removed invalid older setter * Fixed incorrectly setting relayUrl * Updated test --------- Co-authored-by: glihm --- .../src/plugins/typescript_v2/mod.rs | 25 +++--- .../src/test_data/mocks/dojo_examples.ts | 79 ++++++++++--------- 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs b/crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs index b23549aa89..94c046cf24 100644 --- a/crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs +++ b/crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs @@ -51,7 +51,12 @@ import { createClient, valueToToriiValueAndOperator, } from \"@dojoengine/torii-client\"; -import { LOCAL_KATANA, createManifestFromJson } from \"@dojoengine/core\";" +import { + LOCAL_KATANA, + LOCAL_RELAY, + LOCAL_TORII, + createManifestFromJson, +} from \"@dojoengine/core\";" .to_string() } @@ -230,7 +235,7 @@ function convertQueryToToriiClause(query: Query): Clause | undefined {{ "type InitialParams = GeneralParams & ( | {{ - rpcUrl: string; + rpcUrl?: string; worldAddress: string; {system_addresses} }} @@ -245,8 +250,8 @@ function convertQueryToToriiClause(query: Query): Clause | undefined {{ let mut out = String::new(); out += "type GeneralParams = { - toriiUrl: string; - relayUrl: string; + toriiUrl?: string; + relayUrl?: string; account?: Account; };"; @@ -338,19 +343,19 @@ function convertQueryToToriiClause(query: Query): Clause | undefined {{ {system_properties} constructor(params: InitialParams) {{ - this.rpcUrl = LOCAL_KATANA; if (\"manifest\" in params) {{ const config = createManifestFromJson(params.manifest); + this.rpcUrl = config.world.metadata.rpc_url; this.worldAddress = config.world.address; {system_address_initializations} }} else {{ - this.rpcUrl = params.rpcUrl; + this.rpcUrl = params.rpcUrl || LOCAL_KATANA; this.worldAddress = params.worldAddress; {system_address_initializations_from_params} }} - this.toriiUrl = params.toriiUrl; - this.relayUrl = params.relayUrl; + this.toriiUrl = params.toriiUrl || LOCAL_TORII; + this.relayUrl = params.relayUrl || LOCAL_RELAY; this._account = params.account; {system_initializations} @@ -616,7 +621,7 @@ mod tests { // This test currently is very naive, but DojoData is unsorted, so the output // can change between tests. This is a temporary solution until we have a better // way to test this. - assert_eq!(actual_output_without_header.len(), 7479); - assert_eq!(expected_output_without_header.len(), 7479); + assert_eq!(actual_output_without_header.len(), 7592); + assert_eq!(expected_output_without_header.len(), 7592); } } diff --git a/crates/dojo-bindgen/src/test_data/mocks/dojo_examples.ts b/crates/dojo-bindgen/src/test_data/mocks/dojo_examples.ts index 49e3805bf7..0e807cd262 100644 --- a/crates/dojo-bindgen/src/test_data/mocks/dojo_examples.ts +++ b/crates/dojo-bindgen/src/test_data/mocks/dojo_examples.ts @@ -1,4 +1,4 @@ -// Generated by dojo-bindgen on Fri, 12 Apr 2024 13:23:24 +0000. Do not modify this file manually. +// Generated by dojo-bindgen on Wed, 17 Apr 2024 07:58:49 +0000. Do not modify this file manually. import { Account } from "starknet"; import { Clause, @@ -7,21 +7,26 @@ import { createClient, valueToToriiValueAndOperator, } from "@dojoengine/torii-client"; -import { LOCAL_KATANA, createManifestFromJson } from "@dojoengine/core"; +import { + LOCAL_KATANA, + LOCAL_RELAY, + LOCAL_TORII, + createManifestFromJson, +} from "@dojoengine/core"; -// Type definition for `dojo_examples::actions::actions::Moved` struct -export interface Moved { - player: string; - direction: Direction; +// Type definition for `dojo_examples::models::EmoteMessage` struct +export interface EmoteMessage { + identity: string; + emote: Emote; } -// Type definition for `dojo_examples::models::Direction` enum -export enum Direction { +// Type definition for `dojo_examples::models::Emote` enum +export enum Emote { None, - Left, - Right, - Up, - Down, + Happy, + Sad, + Angry, + Love, } @@ -38,11 +43,10 @@ export interface Position { } -// Type definition for `dojo_examples::models::Moves` struct -export interface Moves { +// Type definition for `dojo_examples::actions::actions::Moved` struct +export interface Moved { player: string; - remaining: number; - last_direction: Direction; + direction: Direction; } // Type definition for `dojo_examples::models::Direction` enum @@ -55,19 +59,20 @@ export enum Direction { } -// Type definition for `dojo_examples::models::EmoteMessage` struct -export interface EmoteMessage { - identity: string; - emote: Emote; +// Type definition for `dojo_examples::models::Moves` struct +export interface Moves { + player: string; + remaining: number; + last_direction: Direction; } -// Type definition for `dojo_examples::models::Emote` enum -export enum Emote { +// Type definition for `dojo_examples::models::Direction` enum +export enum Direction { None, - Happy, - Sad, - Angry, - Love, + Left, + Right, + Up, + Down, } @@ -153,17 +158,17 @@ class ActionsCalls extends BaseCalls { } type Query = Partial<{ - Moved: ModelClause; + EmoteMessage: ModelClause; Position: ModelClause; + Moved: ModelClause; Moves: ModelClause; - EmoteMessage: ModelClause; }>; type ResultMapping = { - Moved: Moved; + EmoteMessage: EmoteMessage; Position: Position; + Moved: Moved; Moves: Moves; - EmoteMessage: EmoteMessage; }; type QueryResult = { @@ -192,15 +197,15 @@ function convertQueryToToriiClause(query: Query): Clause | undefined { return clauses[0]; } type GeneralParams = { - toriiUrl: string; - relayUrl: string; + toriiUrl?: string; + relayUrl?: string; account?: Account; }; type InitialParams = GeneralParams & ( | { - rpcUrl: string; + rpcUrl?: string; worldAddress: string; actionsAddress: string; } @@ -220,9 +225,9 @@ export class DojoExamples { actionsAddress: string; constructor(params: InitialParams) { - this.rpcUrl = LOCAL_KATANA; if ("manifest" in params) { const config = createManifestFromJson(params.manifest); + this.rpcUrl = config.world.metadata.rpc_url; this.worldAddress = config.world.address; const actionsAddress = config.contracts.find( @@ -236,12 +241,12 @@ export class DojoExamples { this.actionsAddress = actionsAddress; } else { - this.rpcUrl = params.rpcUrl; + this.rpcUrl = params.rpcUrl || LOCAL_KATANA; this.worldAddress = params.worldAddress; this.actionsAddress = params.actionsAddress; } - this.toriiUrl = params.toriiUrl; - this.relayUrl = params.relayUrl; + this.toriiUrl = params.toriiUrl || LOCAL_TORII; + this.relayUrl = params.relayUrl || LOCAL_RELAY; this._account = params.account; this.actions = new ActionsCalls(this.actionsAddress, this._account);