Skip to content

Commit b3b6f47

Browse files
committed
feat(types): support CairoOption
1 parent 87f6b2c commit b3b6f47

File tree

7 files changed

+69
-11
lines changed

7 files changed

+69
-11
lines changed

examples/example-vite-react-sdk/src/useSystemCalls.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export const useSystemCalls = () => {
5252
try {
5353
// Execute the spawn action from the client
5454
// await client.actions.spawn(account!);
55-
console.log(dojoProvider.spawn);
5655
await dojoProvider.spawn(account!);
5756

5857
// Wait for the entity to be updated with the new state

packages/core/src/provider/DojoProvider.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,13 @@ class DojoProviderBase extends Provider {
356356
continue;
357357
}
358358

359-
// check here in interfaces and then in function having name matching
360359
const interfaceAbi = abiItems.find(
361360
(item: any) =>
362361
item?.type === "interface" &&
363362
item?.items.find(
364363
(i: any) =>
365364
i?.type === "function" && i?.name === systemName
366365
)
367-
// item?.type === "function" && item?.name === systemName
368366
);
369367

370368
if (!interfaceAbi) {

packages/core/src/types/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type ModelPath = ModelPathFromAbi<typeof compiledAbi>; // e.g. "dojo_starter-Pos
4848
## Extracted type groups
4949

5050
- **`structs`** – Cairo structs mapped to TypeScript objects with nested references resolved.
51-
- **`enums`** – Cairo enums with a `CairoCustomEnum` instance type, a `variantNames` union, and a strongly typed `variants` map.
51+
- **`enums`** – Cairo enums with a `CairoCustomEnum` instance type (or `CairoOption` for `core::option::Option`), a `variantNames` union, and a strongly typed `variants` map.
5252
- **`interfaces`** – Contract interfaces keyed by fully qualified name with typed function signatures.
5353
- **`models`** – Dojo models grouped by namespace; the shape aligns with `SchemaType` used throughout the SDK.
5454
- **`actions`** – System action interfaces grouped by namespace, preserving typed inputs and outputs.
@@ -58,6 +58,10 @@ type Direction = Abi["enums"]["dojo_starter::models::Direction"];
5858
type DirectionValue = Direction["type"]; // CairoCustomEnum
5959
type DirectionVariants = Direction["variantNames"]; // "Up" | "Down" | ...
6060

61+
type OptionalDirection =
62+
Abi["enums"]["core::option::Option::<dojo_starter::models::Direction>"];
63+
type OptionalDirectionValue = OptionalDirection["type"]; // CairoOption<DirectionValue>
64+
6165
type Actions = Abi["actions"]["dojo_starter"]["IActions"];
6266
type SpawnInputs = Actions["spawn"]["inputs"]; // { entity: Position; ... }
6367
```

packages/core/src/types/example-usage.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { compiledAbi } from "../../../../worlds/dojo-starter/compiled-abi";
1111
import {
1212
Account,
1313
CairoCustomEnum,
14+
CairoOption,
15+
CairoOptionVariant,
1416
ETransactionVersion,
1517
ProviderInterface,
1618
} from "starknet";
@@ -39,6 +41,8 @@ type MyAbiStructs = MyAbi["structs"];
3941
type MyAbiEnums = MyAbi["enums"];
4042
type MyAbiFunctions = MyAbi["functions"];
4143
type MyAbiInterfaces = MyAbi["interfaces"];
44+
type OptionnalNumber = MyAbiEnums["core::option::Option::<core::integer::u32>"];
45+
const e = new CairoOption(CairoOptionVariant.Some, 12);
4246

4347
type DojoStarterActions =
4448
MyAbiInterfaces["dojo_starter::systems::actions::IActions"];
@@ -66,8 +70,8 @@ provider.register_contract(account, {
6670
});
6771
provider.delete_entities(account, {
6872
model_selector: "test",
69-
indexes: ["Keys"],
70-
layout: "Fixed",
73+
indexes: new CairoCustomEnum({ Id: "()" }),
74+
layout: new CairoCustomEnum({ Fixed: "()" }),
7175
});
7276

7377
// Now you can use the extracted types
@@ -89,6 +93,16 @@ type DirectionValue = Direction["type"]; // CairoCustomEnum
8993
type DirectionVariantNames = Direction["variantNames"]; // "None" | "Up" | "Down" | "Left" | "Right"
9094
type DirectionVariantMap = Direction["variants"]; // Object with variant names as keys
9195

96+
type OptionalDirection =
97+
MyAbi["enums"]["core::option::Option::<dojo_starter::models::Direction>"];
98+
type OptionalDirectionValue = OptionalDirection["type"]; // CairoOption<DirectionValue>
99+
const od: OptionalDirectionValue = new CairoOption(CairoOptionVariant.None);
100+
101+
const maybeDirection: OptionalDirectionValue = new CairoOption(
102+
CairoOptionVariant.Some,
103+
new CairoCustomEnum({ Up: "()" })
104+
);
105+
92106
// Interface types - access interface functions
93107
type IWorld = MyAbi["interfaces"]["dojo::world::iworld::IWorld"];
94108
type WorldResourceFunction = IWorld["resource"]; // { inputs: { selector: string }, outputs: Resource }

packages/core/src/types/index.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ const sampleAbi = {
3838
{ name: "Right", type: "()" },
3939
],
4040
},
41+
{
42+
type: "enum",
43+
name: "core::option::Option::<demo::models::Direction>",
44+
variants: [
45+
{ name: "Some", type: "demo::models::Direction" },
46+
{ name: "None", type: "()" },
47+
],
48+
},
49+
{
50+
type: "enum",
51+
name: "core::option::Option::<core::integer::u32>",
52+
variants: [
53+
{ name: "Some", type: "core::integer::u32" },
54+
{ name: "None", type: "()" },
55+
],
56+
},
4157
{
4258
type: "function",
4359
name: "move",
@@ -109,6 +125,30 @@ describe("ExtractAbiTypes", () => {
109125
"Up" | "Down" | "Left" | "Right"
110126
>();
111127

128+
type OptionalDirection = Extracted["enums"][
129+
"core::option::Option::<demo::models::Direction>"
130+
];
131+
132+
expectTypeOf<OptionalDirection["type"]>().toEqualTypeOf<
133+
CairoOption<DirectionEnum["type"]>
134+
>();
135+
expectTypeOf<OptionalDirection["variantNames"]>().toEqualTypeOf<
136+
"Some" | "None"
137+
>();
138+
expectTypeOf<OptionalDirection["variants"].Some>().toEqualTypeOf<
139+
DirectionEnum["type"]
140+
>();
141+
expectTypeOf<OptionalDirection["variants"].None>().toEqualTypeOf<
142+
void
143+
>();
144+
145+
type OptionalScore = Extracted["enums"][
146+
"core::option::Option::<core::integer::u32>"
147+
];
148+
149+
expectTypeOf<OptionalScore["type"]>().toEqualTypeOf<CairoOption<number>>();
150+
expectTypeOf<OptionalScore["variants"].Some>().toEqualTypeOf<number>();
151+
112152
type Actions = ActionsFromAbi<typeof sampleAbi>;
113153
expectTypeOf<keyof Actions>().toEqualTypeOf<"demo">();
114154
expectTypeOf<keyof Actions["demo"]>().toEqualTypeOf<"IActions">();

packages/core/src/types/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Calldata, CairoCustomEnum, RawArgs } from "starknet";
1+
import { Calldata, CairoCustomEnum, CairoOption, RawArgs } from "starknet";
22

33
/**
44
* Enumeration representing various entry points or functions available in the World.
@@ -355,10 +355,12 @@ type ExtractEnumType<
355355
> = ExtractEnumVariants<Name, ABI> extends infer VariantMap
356356
? [VariantMap] extends [never]
357357
? never
358-
: CairoCustomEnum & {
359-
readonly __variantMap?: VariantMap;
360-
readonly __variantNames?: keyof VariantMap & string;
361-
}
358+
: Name extends `core::option::Option::<${infer Inner}>`
359+
? CairoOption<MapCairoType<Inner, ABI>>
360+
: CairoCustomEnum & {
361+
readonly __variantMap?: VariantMap;
362+
readonly __variantNames?: keyof VariantMap & string;
363+
}
362364
: never;
363365

364366
/**

packages/sdk/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
tsup.node.bundled_*
4+
tsup.web.bundled_*

0 commit comments

Comments
 (0)