Skip to content

Commit cabdce7

Browse files
MartianGreedThierry
andauthored
fix(sdk,core): re-export torii types and type DojoProvider manifest (#521)
* fix(sdk,core): re-export torii namespace and type DojoProvider manifest Fixes #482 — torii-wasm types (Subscription, TokenBalance, etc.) are now re-exported from @dojoengine/sdk via the `torii` namespace for both web and node entry points. Also replaces `manifest: any` in DojoProvider and utility functions with a proper `DojoManifest` interface that captures what the provider actually reads from the manifest object. * fix(core): address CodeRabbit review — Abi cast and JSDoc types --------- Co-authored-by: Thierry <thierry@openclaw.ai>
1 parent 85cfd91 commit cabdce7

5 files changed

Lines changed: 61 additions & 24 deletions

File tree

packages/core/src/provider/DojoProvider.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
type Abi,
23
type Account,
34
type AccountInterface,
45
type AllowArray,
@@ -15,7 +16,7 @@ import {
1516

1617
import { LOCAL_KATANA } from "../constants";
1718
import { ConsoleLogger, type LogLevel } from "../logger/logger";
18-
import { type DojoCall, WorldEntryPoints } from "../types";
19+
import { type DojoCall, type DojoManifest, WorldEntryPoints } from "../types";
1920
import { compileDojoCalldata } from "../utils/compile";
2021
import { getContractAbi, getContractByName, parseDojoCall } from "../utils";
2122
import { Provider } from "./provider";
@@ -101,17 +102,17 @@ type ActionMethodImplementation = (
101102
class DojoProviderBase extends Provider {
102103
public provider: RpcProvider;
103104
public contract: Contract;
104-
public manifest: any;
105+
public manifest: DojoManifest;
105106
public logger: ConsoleLogger;
106107

107108
/**
108109
* Constructor: Initializes the DojoProvider with the given world address, manifest and URL.
109110
*
110-
* @param {string} world_address - Address of the world.
111+
* @param {DojoManifest} manifest - The Dojo manifest object.
111112
* @param {string} [url=LOCAL_KATANA] - RPC URL (defaults to LOCAL_KATANA).
112113
*/
113114
constructor(
114-
manifest?: any,
115+
manifest: DojoManifest,
115116
url: string = LOCAL_KATANA,
116117
logLevel: LogLevel = "none"
117118
) {
@@ -121,7 +122,7 @@ class DojoProviderBase extends Provider {
121122
});
122123

123124
this.contract = new Contract({
124-
abi: manifest.world.abi ?? manifest.abis ?? [],
125+
abi: (manifest.world.abi ?? manifest.abis ?? []) as Abi,
125126
address: this.getWorldAddress(),
126127
providerOrAccount: this.provider,
127128
});
@@ -292,13 +293,24 @@ class DojoProviderBase extends Provider {
292293
nameSpace,
293294
call.contractName
294295
);
296+
297+
if (!contractInfos) {
298+
throw new Error(
299+
`Contract "${call.contractName}" not found in namespace "${nameSpace}"`
300+
);
301+
}
302+
303+
const contractAbi = getContractAbi(
304+
this.manifest,
305+
contractInfos
306+
);
295307
const contract = new Contract({
296-
abi: contractInfos.abi,
308+
abi: contractAbi,
297309
address: contractInfos.address,
298310
providerOrAccount: this.provider,
299311
});
300312
const compiledCalldata = compileDojoCalldata(
301-
contractInfos.abi,
313+
contractAbi,
302314
nameSpace,
303315
call.contractName,
304316
call.entrypoint,
@@ -357,9 +369,9 @@ class DojoProviderBase extends Provider {
357369
>;
358370

359371
const functionCounts = new Map<string, number>();
360-
for (const contract of this.manifest.contracts as Array<any>) {
372+
for (const contract of this.manifest.contracts ?? []) {
361373
if (!contract?.systems?.length) continue;
362-
for (const systemName of contract.systems as Array<string>) {
374+
for (const systemName of contract.systems) {
363375
functionCounts.set(
364376
systemName,
365377
(functionCounts.get(systemName) || 0) + 1
@@ -374,7 +386,7 @@ class DojoProviderBase extends Provider {
374386
value !== null &&
375387
"execute" in (value as Record<string, unknown>);
376388

377-
for (const contract of this.manifest.contracts as Array<any>) {
389+
for (const contract of this.manifest.contracts ?? []) {
378390
if (
379391
!contract?.systems?.length ||
380392
typeof contract.tag !== "string"
@@ -507,7 +519,7 @@ export type DojoProviderInstance<Actions = never> = DojoProviderBase &
507519
export type DojoProvider<Actions = never> = DojoProviderInstance<Actions>;
508520

509521
type DojoProviderConstructor = new <Actions = never>(
510-
manifest?: any,
522+
manifest: DojoManifest,
511523
url?: string,
512524
logLevel?: LogLevel
513525
) => DojoProvider<Actions>;

packages/core/src/types/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ export type DojoCall = {
133133
calldata: RawArgs | Calldata;
134134
};
135135

136+
/**
137+
* Minimal manifest shape consumed by DojoProvider and utility functions.
138+
* Supports both legacy (inline ABI) and modern (root-level abis) formats.
139+
*/
140+
export interface DojoManifest {
141+
world: {
142+
address: string;
143+
abi?: readonly Record<string, unknown>[];
144+
};
145+
/** Root-level ABI array (modern format). */
146+
abis?: readonly Record<string, unknown>[];
147+
contracts?: ReadonlyArray<{
148+
tag: string;
149+
address: string;
150+
systems?: readonly string[];
151+
abi?: readonly Record<string, unknown>[];
152+
}>;
153+
[key: string]: unknown;
154+
}
155+
136156
/**
137157
* Cairo to TypeScript type mappings
138158
*/

packages/core/src/utils/index.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,56 @@
11
import { Abi, Call, TypedData } from "starknet";
22

3-
import { DojoCall } from "../types";
3+
import { DojoCall, DojoManifest } from "../types";
44
import { compileDojoCalldata } from "./compile";
55

66
/**
77
* Gets the ABI for a contract, supporting both old (inline) and new (root-level) manifest formats.
88
*
9-
* @param {any} manifest - The manifest object.
10-
* @param {any} contract - The contract object.
9+
* @param {DojoManifest} manifest - The manifest object.
10+
* @param {object} contract - The contract object.
1111
* @returns {Abi} The ABI array.
1212
*/
13-
export const getContractAbi = (manifest: any, contract: any): Abi => {
13+
export const getContractAbi = (
14+
manifest: DojoManifest,
15+
contract: { abi?: readonly Record<string, unknown>[] }
16+
): Abi => {
1417
if (Array.isArray(contract?.abi) && contract.abi.length > 0) {
15-
return contract.abi;
18+
return contract.abi as Abi;
1619
}
1720
if (Array.isArray(manifest?.abis)) {
18-
return manifest.abis;
21+
return manifest.abis as Abi;
1922
}
2023
return [];
2124
};
2225

2326
/**
2427
* Gets a contract from a manifest by name.
2528
*
26-
* @param {any} manifest - The manifest object.
29+
* @param {DojoManifest} manifest - The manifest object.
30+
* @param {string} nameSpace - The namespace of the contract.
2731
* @param {string} name - The name of the contract.
28-
* @returns {any} The contract object.
29-
*
32+
* @returns The contract object, or undefined if not found.
3033
*/
3134
export const getContractByName = (
32-
manifest: any,
35+
manifest: DojoManifest,
3336
nameSpace: string,
3437
name: string
3538
) => {
36-
return manifest.contracts.find((contract: any) => {
39+
return (manifest.contracts ?? []).find((contract) => {
3740
return contract.tag === nameSpace + "-" + name;
3841
});
3942
};
4043

4144
/**
4245
* Convert a DojoCall to a Call replacing contractName with contractAddress
4346
*
44-
* @param {any} manifest - The manifest object.
47+
* @param {DojoManifest} manifest - The manifest object.
4548
* @param {DojoCall | Call} call - A DojoCall or Call
4649
* @returns {Call} The contract object.
4750
*
4851
*/
4952
export const parseDojoCall = (
50-
manifest: any,
53+
manifest: DojoManifest,
5154
nameSpace: string,
5255
call: DojoCall | Call
5356
): Call => {

packages/sdk/src/node/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { createSDK, type GrpcClientInterface } from "../createSDK.js";
1212

1313
export * from "@dojoengine/internal";
14+
export { torii };
1415
export * from "./worker.ts";
1516
export { initGrpc, type InitGrpcOptions } from "../initGrpc.js";
1617
export type { GrpcClientInterface } from "../createSDK.js";

packages/sdk/src/web/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { createSDK, type GrpcClientInterface } from "../createSDK.js";
1111

1212
export * from "@dojoengine/internal";
13+
export { torii };
1314
export { initGrpc, type InitGrpcOptions } from "../initGrpc.js";
1415
export type { GrpcClientInterface } from "../createSDK.js";
1516

0 commit comments

Comments
 (0)