Skip to content

Commit

Permalink
Update defaults for new TypeScript bindgen (#1846)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
RareSecond and glihm authored Apr 19, 2024
1 parent dbad879 commit 9fd4cc4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
25 changes: 15 additions & 10 deletions crates/dojo-bindgen/src/plugins/typescript_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -230,7 +235,7 @@ function convertQueryToToriiClause(query: Query): Clause | undefined {{
"type InitialParams = GeneralParams &
(
| {{
rpcUrl: string;
rpcUrl?: string;
worldAddress: string;
{system_addresses}
}}
Expand All @@ -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;
};";

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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);
}
}
79 changes: 42 additions & 37 deletions crates/dojo-bindgen/src/test_data/mocks/dojo_examples.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
}


Expand All @@ -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
Expand All @@ -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,
}


Expand Down Expand Up @@ -153,17 +158,17 @@ class ActionsCalls extends BaseCalls {
}

type Query = Partial<{
Moved: ModelClause<Moved>;
EmoteMessage: ModelClause<EmoteMessage>;
Position: ModelClause<Position>;
Moved: ModelClause<Moved>;
Moves: ModelClause<Moves>;
EmoteMessage: ModelClause<EmoteMessage>;
}>;

type ResultMapping = {
Moved: Moved;
EmoteMessage: EmoteMessage;
Position: Position;
Moved: Moved;
Moves: Moves;
EmoteMessage: EmoteMessage;
};

type QueryResult<T extends Query> = {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(
Expand All @@ -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);

Expand Down

0 comments on commit 9fd4cc4

Please sign in to comment.