Skip to content

Commit 37a0eae

Browse files
committed
feat: create reusable hooks and add sql hook to sdk
1 parent 395e561 commit 37a0eae

37 files changed

+816
-591
lines changed

.changeset/great-kiwis-peel.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@dojoengine/sdk": patch
3+
"@dojoengine/core": patch
4+
"@dojoengine/create-burner": patch
5+
"@dojoengine/create-dojo": patch
6+
"@dojoengine/predeployed-connector": patch
7+
"@dojoengine/react": patch
8+
"@dojoengine/state": patch
9+
"@dojoengine/torii-client": patch
10+
"@dojoengine/torii-wasm": patch
11+
"@dojoengine/utils": patch
12+
"@dojoengine/utils-wasm": patch
13+
---
14+
15+
Add @dojoengine/sdk/state @dojoengine/sdk/react @dojoengine/sdk/sql // Moves hooks to sdk // Update examples

examples/example-vite-kitchen-sink/src/components/caller-counter.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { useCallback, useEffect, useState } from "react";
2-
import { Button } from "./ui/button";
3-
import { useAccount, useSendTransaction } from "@starknet-react/core";
4-
import { useDojoDb } from "@/dojo/provider";
5-
import { ensureStarkFelt } from "@/lib/utils";
2+
63
import { ParsedEntity, QueryBuilder, SDK } from "@dojoengine/sdk";
4+
import { useDojoSDK } from "@dojoengine/sdk/react";
75
import { Subscription } from "@dojoengine/torii-wasm";
8-
import { dojoConfig } from "@/../dojoConfig";
96
import { SchemaType } from "@/typescript/models.gen";
7+
import { setupWorld } from "@/typescript/contracts.gen";
8+
import { dojoConfig } from "@/../dojoConfig";
9+
1010
import { addAddressPadding } from "starknet";
11+
import { Button } from "./ui/button";
12+
import { useAccount, useSendTransaction } from "@starknet-react/core";
1113

1214
export default function CallerCounter() {
1315
const [count, setCount] = useState(0);
@@ -29,7 +31,7 @@ export default function CallerCounter() {
2931
setIsLoading(true);
3032
}, [incrementCallerCounter, setIsLoading]);
3133

32-
const { db } = useDojoDb();
34+
const { sdk } = useDojoSDK<typeof setupWorld, SchemaType>();
3335
useEffect(() => {
3436
async function getEntity(db: SDK<SchemaType>, address: string) {
3537
const entity = await db.getEntities({
@@ -53,10 +55,10 @@ export default function CallerCounter() {
5355

5456
return parseInt(count.toString(), 16);
5557
}
56-
if (address && db) {
57-
getEntity(db, address).then(setCount).catch(console.error);
58+
if (address && sdk) {
59+
getEntity(sdk, address).then(setCount).catch(console.error);
5860
}
59-
}, [address, db]);
61+
}, [address, sdk]);
6062

6163
useEffect(() => {
6264
async function subscribeToEntityUpdates(
@@ -100,8 +102,8 @@ export default function CallerCounter() {
100102
});
101103
setSub(sub);
102104
}
103-
if (address && db && sub === null) {
104-
subscribeToEntityUpdates(db, address)
105+
if (address && sdk && sub === null) {
106+
subscribeToEntityUpdates(sdk, address)
105107
.then(() => {})
106108
.catch(console.error);
107109
}
@@ -110,7 +112,7 @@ export default function CallerCounter() {
110112
sub.free();
111113
}
112114
};
113-
}, [address, db, sub]);
115+
}, [address, sdk, sub]);
114116
return (
115117
<fieldset className="grid gap-6 rounded-lg border p-4">
116118
<legend className="-ml-1 px-1 text-sm font-medium">

examples/example-vite-kitchen-sink/src/components/chat.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { Label } from "./ui/label";
55
import { Textarea } from "./ui/textarea";
66
import { useCallback, useEffect, useRef, useState, KeyboardEvent } from "react";
77
import { useForm } from "react-hook-form";
8-
import { useDojoDb } from "@/dojo/provider";
98
import { useAccount } from "@starknet-react/core";
109
import { toValidAscii } from "@/lib/utils";
1110
import { ParsedEntity, SDK } from "@dojoengine/sdk";
1211
import { Subscription } from "@dojoengine/torii-wasm";
1312
import { shortAddress } from "@/lib/utils";
1413
import { Message, SchemaType } from "@/typescript/models.gen";
14+
import { useDojoSDK } from "@dojoengine/sdk/react";
15+
import { setupWorld } from "@/typescript/contracts.gen";
1516

1617
interface MessageItem {
1718
content: string;
@@ -30,7 +31,7 @@ export default function Chat() {
3031
const [sub, setSub] = useState<Subscription | null>(null);
3132
const formRef = useRef<HTMLFormElement>(null);
3233

33-
const { db } = useDojoDb();
34+
const { sdk: db } = useDojoSDK<typeof setupWorld, SchemaType>();
3435
const publish = useCallback(
3536
async (data: FormValues) => {
3637
if (!account || !db) return;

examples/example-vite-kitchen-sink/src/components/global-counter.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useCallback, useEffect, useState } from "react";
22
import { Button } from "./ui/button";
33
import { useSendTransaction } from "@starknet-react/core";
4-
import { useDojoDb } from "@/dojo/provider";
54
import { ParsedEntity, QueryBuilder, SDK } from "@dojoengine/sdk";
65
import { Subscription } from "@dojoengine/torii-wasm";
76
import { dojoConfig } from "@/../dojoConfig";
87
import { SchemaType } from "@/typescript/models.gen";
9-
import { addAddressPadding } from "starknet";
8+
import { useDojoSDK } from "@dojoengine/sdk/react";
9+
import { setupWorld } from "@/typescript/contracts.gen";
1010

1111
export default function GlobalCOunter() {
1212
const [count, setCount] = useState(0);
@@ -26,7 +26,7 @@ export default function GlobalCOunter() {
2626
setIsLoading(true);
2727
}, [incrementGlobalCounter, setIsLoading]);
2828

29-
const { db } = useDojoDb();
29+
const { sdk: db } = useDojoSDK<typeof setupWorld, SchemaType>();
3030

3131
useEffect(() => {
3232
async function getEntity(db: SDK<SchemaType>) {

examples/example-vite-kitchen-sink/src/components/theme-switch.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Subscription } from "@dojoengine/torii-wasm";
44
import { useAccount } from "@starknet-react/core";
55
import { ParsedEntity, QueryBuilder, SDK } from "@dojoengine/sdk";
66
import { SchemaType } from "@/typescript/models.gen";
7-
import { useDojoDb } from "@/dojo/provider";
87
import { Button } from "@/components/ui/button";
98
import {
109
DropdownMenu,
@@ -17,6 +16,8 @@ import {
1716
DropdownMenuTrigger,
1817
} from "@/components/ui/dropdown";
1918
import { CairoCustomEnum } from "starknet";
19+
import { useDojoSDK } from "@dojoengine/sdk/react";
20+
import { setupWorld } from "@/typescript/contracts.gen";
2021

2122
interface ThemeState {
2223
current: string | null;
@@ -47,7 +48,10 @@ export default function ThemeSwitchButton() {
4748
const [entityId, setEntityId] = useState<string | null>(null);
4849
const { address, account } = useAccount();
4950
const [sub, setSub] = useState<Subscription | null>(null);
50-
const { db, actions } = useDojoDb();
51+
const { sdk: db, client: actions } = useDojoSDK<
52+
typeof setupWorld,
53+
SchemaType
54+
>();
5155

5256
const handleChangeTheme = useCallback(
5357
async (theme: AvailableTheme) => {

examples/example-vite-kitchen-sink/src/dojo/provider.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/example-vite-kitchen-sink/src/main.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import Home from "./app/page";
77
import "./app/globals.css";
88

99
import { init } from "@dojoengine/sdk";
10-
import { env, getRpcUrl } from "@/env";
10+
import { DojoSdkProvider } from "@dojoengine/sdk/react";
1111
import { dojoConfig } from "../dojoConfig";
12-
import { DojoContext } from "@/dojo/provider";
13-
import { DojoProvider } from "@dojoengine/core";
1412
import { setupWorld } from "./typescript/contracts.gen";
1513
import { SchemaType, schema } from "./typescript/models.gen";
1614

15+
import { env, getRpcUrl } from "@/env";
16+
1717
async function main() {
18-
const db = await init<SchemaType>(
18+
const sdk = await init<SchemaType>(
1919
{
2020
client: {
2121
rpcUrl: getRpcUrl(),
@@ -32,16 +32,18 @@ async function main() {
3232
},
3333
schema
3434
);
35-
const provider = new DojoProvider(dojoConfig.manifest, getRpcUrl());
36-
const actions = setupWorld(provider);
3735

3836
createRoot(document.getElementById("root")!).render(
3937
<StrictMode>
40-
<DojoContext.Provider value={{ db, provider, actions }}>
38+
<DojoSdkProvider
39+
sdk={sdk}
40+
dojoConfig={dojoConfig}
41+
clientFn={setupWorld}
42+
>
4143
<RootLayout>
4244
<Home />
4345
</RootLayout>
44-
</DojoContext.Provider>
46+
</DojoSdkProvider>
4547
</StrictMode>
4648
);
4749
}

examples/example-vite-react-sdk/src/App.tsx

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
import { useEffect, useMemo } from "react";
2-
import {
3-
ParsedEntity,
4-
QueryBuilder,
5-
SDK,
6-
createDojoStore,
7-
} from "@dojoengine/sdk";
2+
import { ParsedEntity, QueryBuilder } from "@dojoengine/sdk";
83
import { getEntityIdFromKeys } from "@dojoengine/utils";
94
import { AccountInterface, addAddressPadding, CairoCustomEnum } from "starknet";
105

116
import { ModelsMapping, SchemaType } from "./typescript/models.gen.ts";
12-
import { useDojo } from "./useDojo.tsx";
13-
import useModel from "./useModel.tsx";
147
import { useSystemCalls } from "./useSystemCalls.ts";
158
import { useAccount } from "@starknet-react/core";
169
import { WalletAccount } from "./wallet-account.tsx";
1710
import { HistoricalEvents } from "./historical-events.tsx";
18-
19-
/**
20-
* Global store for managing Dojo game state.
21-
*/
22-
export const useDojoStore = createDojoStore<SchemaType>();
11+
import { useDojoSDK, useModel } from "@dojoengine/sdk/react";
2312

2413
/**
2514
* Main application component that provides game functionality and UI.
2615
* Handles entity subscriptions, state management, and user interactions.
2716
*
2817
* @param props.sdk - The Dojo SDK instance configured with the game schema
2918
*/
30-
function App({ sdk }: { sdk: SDK<SchemaType> }) {
31-
const {
32-
setup: { client },
33-
} = useDojo();
19+
function App() {
20+
const { useDojoStore, client, sdk } = useDojoSDK();
3421
const { account } = useAccount();
3522
const state = useDojoStore((state) => state);
3623
const entities = useDojoStore((state) => state.entities);
@@ -160,7 +147,6 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
160147
: "Need to Spawn"}
161148
</div>
162149
<div className="col-span-3 text-center text-base text-white">
163-
{/* @ts-expect-error we have an option here so type is ok */}
164150
{moves && moves.last_direction.isSome()
165151
? moves.last_direction.unwrap()
166152
: ""}
@@ -280,7 +266,6 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
280266
"N/A"}
281267
</td>
282268
<td className="border border-gray-700 p-2">
283-
{/* @ts-expect-error we have an option here so type is ok */}
284269
{lastDirection}
285270
</td>
286271
<td className="border border-gray-700 p-2">
@@ -296,7 +281,7 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
296281
</div>
297282

298283
{/* // Here sdk is passed as props but this can be done via contexts */}
299-
<HistoricalEvents sdk={sdk} />
284+
<HistoricalEvents />
300285
</div>
301286
</div>
302287
);

examples/example-vite-react-sdk/src/historical-events.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { ParsedEntity, SDK } from "@dojoengine/sdk";
1+
import { ParsedEntity } from "@dojoengine/sdk";
22
import { useAccount } from "@starknet-react/core";
33
import { SchemaType } from "./typescript/models.gen";
44
import { AccountInterface, addAddressPadding } from "starknet";
55
import { useEffect, useState } from "react";
66
import { Subscription } from "@dojoengine/torii-client";
7+
import { useDojoSDK } from "@dojoengine/sdk/react";
78

8-
export function HistoricalEvents({ sdk }: { sdk: SDK<SchemaType> }) {
9+
export function HistoricalEvents() {
910
const { account } = useAccount();
11+
const { sdk } = useDojoSDK();
1012
const [events, setEvents] = useState<ParsedEntity<SchemaType>[][]>([]);
1113
const [subscription, setSubscription] = useState<Subscription | null>(null);
1214

examples/example-vite-react-sdk/src/main.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { createRoot } from "react-dom/client";
33

44
import App from "./App.tsx";
55

6-
import "./index.css";
6+
// Dojo related imports
77
import { init } from "@dojoengine/sdk";
8+
import { DojoSdkProvider } from "@dojoengine/sdk/react";
89
import { SchemaType, schema } from "./typescript/models.gen.ts";
10+
import { setupWorld } from "./typescript/contracts.gen.ts";
11+
12+
import "./index.css";
913
import { dojoConfig } from "../dojoConfig.ts";
10-
import { DojoContextProvider } from "./DojoContext.tsx";
11-
import { setupBurnerManager } from "@dojoengine/create-burner";
1214
import StarknetProvider from "./starknet-provider.tsx";
1315

1416
/**
@@ -38,13 +40,15 @@ async function main() {
3840

3941
createRoot(document.getElementById("root")!).render(
4042
<StrictMode>
41-
<DojoContextProvider
42-
burnerManager={await setupBurnerManager(dojoConfig)}
43+
<DojoSdkProvider
44+
sdk={sdk}
45+
dojoConfig={dojoConfig}
46+
clientFn={setupWorld}
4347
>
4448
<StarknetProvider>
45-
<App sdk={sdk} />
49+
<App />
4650
</StarknetProvider>
47-
</DojoContextProvider>
51+
</DojoSdkProvider>
4852
</StrictMode>
4953
);
5054
}

examples/example-vite-react-sdk/src/useDojo.tsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)