Skip to content

Commit

Permalink
fix: build-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed Jan 6, 2025
1 parent 50cc19e commit ffc30b4
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 62 deletions.
48 changes: 25 additions & 23 deletions examples/example-vite-react-sdk/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEffect, useMemo } from "react";
import { QueryBuilder, SDK, createDojoStore } from "@dojoengine/sdk";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import {
AccountInterface,
addAddressPadding,
CairoOption,
CairoOptionVariant,
} from "starknet";
ParsedEntity,
QueryBuilder,
SDK,
createDojoStore,
} from "@dojoengine/sdk";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import { AccountInterface, addAddressPadding } from "starknet";

import {
Direction,
Expand All @@ -16,7 +16,7 @@ import {
import { useDojo } from "./useDojo.tsx";
import useModel from "./useModel.tsx";
import { useSystemCalls } from "./useSystemCalls.ts";
import { useAccount, useConnect } from "@starknet-react/core";
import { useAccount } from "@starknet-react/core";
import { WalletAccount } from "./wallet-account.tsx";
import { HistoricalEvents } from "./historical-events.tsx";

Expand Down Expand Up @@ -70,17 +70,14 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
)
)
.build(),
callback: (response) => {
if (response.error) {
console.error(
"Error setting up entity sync:",
response.error
);
callback: ({ error, data }) => {
if (error) {
console.error("Error setting up entity sync:", error);
} else if (
response.data &&
response.data[0].entityId !== "0x0"
data &&
(data[0] as ParsedEntity<SchemaType>).entityId !== "0x0"
) {
state.updateEntity(response.data[0]);
state.updateEntity(data[0] as ParsedEntity<SchemaType>);
}
},
});
Expand Down Expand Up @@ -122,7 +119,9 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
return;
}
if (resp.data) {
state.setEntities(resp.data);
state.setEntities(
resp.data as ParsedEntity<SchemaType>[]
);
}
},
});
Expand Down Expand Up @@ -248,8 +247,8 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
const moves =
entity.models.dojo_starter.Moves;
const lastDirection =
moves?.last_direction.isSome()
? moves.last_direction.unwrap()
moves?.last_direction?.isSome()
? moves.last_direction?.unwrap()
: "N/A";

return (
Expand All @@ -264,10 +263,12 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
{position?.player ?? "N/A"}
</td>
<td className="border border-gray-700 p-2">
{position?.vec?.x ?? "N/A"}
{position?.vec?.x.toString() ??
"N/A"}
</td>
<td className="border border-gray-700 p-2">
{position?.vec?.y ?? "N/A"}
{position?.vec?.y.toString() ??
"N/A"}
</td>
<td className="border border-gray-700 p-2">
{moves?.can_move?.toString() ??
Expand All @@ -277,7 +278,8 @@ function App({ sdk }: { sdk: SDK<SchemaType> }) {
{lastDirection}
</td>
<td className="border border-gray-700 p-2">
{moves?.remaining ?? "N/A"}
{moves?.remaining?.toString() ??
"N/A"}
</td>
</tr>
);
Expand Down
24 changes: 13 additions & 11 deletions examples/example-vite-react-sdk/src/historical-events.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { SDK } from "@dojoengine/sdk";
import { ParsedEntity, SDK } from "@dojoengine/sdk";
import { useAccount } from "@starknet-react/core";
import { SchemaType } from "./typescript/models.gen";
import { AccountInterface, addAddressPadding } from "starknet";
import { useEffect, useState } from "react";
import { Subscription } from "@dojoengine/torii-client";

export function HistoricalEvents({ sdk }: { sdk: SDK<SchemaType> }) {
const { account } = useAccount();
const [events, setEvents] = useState([]);
const [unsubscribe, setSubscription] = useState(null);
const [events, setEvents] = useState<ParsedEntity<SchemaType>[][]>([]);
const [subscription, setSubscription] = useState<Subscription | null>(null);

useEffect(() => {
async function getHistoricalEvents(account: AccountInterface) {
Expand All @@ -24,6 +25,7 @@ export function HistoricalEvents({ sdk }: { sdk: SDK<SchemaType> }) {
callback: () => {},
historical: true,
});
// @ts-expect-error FIX: type here
setEvents(e);
} catch (error) {
setEvents([]);
Expand All @@ -34,7 +36,7 @@ export function HistoricalEvents({ sdk }: { sdk: SDK<SchemaType> }) {
if (account) {
getHistoricalEvents(account);
}
}, [account, setEvents]);
}, [account, setEvents, sdk]);

useEffect(() => {
async function subscribeHistoricalEvent(account: AccountInterface) {
Expand All @@ -48,16 +50,16 @@ export function HistoricalEvents({ sdk }: { sdk: SDK<SchemaType> }) {
// }
// },
query: { entityIds: [addAddressPadding(account.address)] },
callback: (resp, error) => {
console.log(resp, error);
callback: ({ data, error }) => {
console.log(data, error);
},
historical: true,
});
setSubscription(s);
} catch (error) {
setEvents([]);
if (unsubscribe) {
unsubscribe();
if (subscription) {
subscription.free();
}
console.error(error);
}
Expand All @@ -78,20 +80,20 @@ export function HistoricalEvents({ sdk }: { sdk: SDK<SchemaType> }) {
return (
<div className="mt-6">
<h2 className="text-white">Player Events :</h2>
{events.map((e, key) => {
{events.map((e: ParsedEntity<SchemaType>[], key) => {
return <Event event={e[0]} key={key} />;
})}
</div>
);
}
function Event({ event }) {
function Event({ event }: { event: ParsedEntity<SchemaType> }) {
if (!event) return null;
const player = event.models?.dojo_starter?.Moved?.player;
const direction = event.models?.dojo_starter?.Moved?.direction;

return (
<div className="text-white flex gap-3">
<div>{event.entityId}</div>
<div>{event.entityId.toString()}</div>
<div>
<div>Player: {player}</div>
<div>Direction: {direction}</div>
Expand Down
4 changes: 2 additions & 2 deletions examples/example-vite-react-sdk/src/starknet-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PropsWithChildren } from "react";
import { Chain, mainnet } from "@starknet-react/chains";
import { mainnet } from "@starknet-react/chains";
import { jsonRpcProvider, StarknetConfig, voyager } from "@starknet-react/core";
import { dojoConfig } from "../dojoConfig";
import {
Expand All @@ -16,7 +16,7 @@ predeployedAccounts({

export default function StarknetProvider({ children }: PropsWithChildren) {
const provider = jsonRpcProvider({
rpc: (chain: Chain) => ({ nodeUrl: dojoConfig.rpcUrl as string }),
rpc: () => ({ nodeUrl: dojoConfig.rpcUrl as string }),
});

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { DojoProvider } from "@dojoengine/core";
import {
Account,
AccountInterface,
BigNumberish,
CairoOption,
CairoCustomEnum,
ByteArray,
} from "starknet";
import { Account, AccountInterface } from "starknet";
import * as models from "./models.gen";

export function setupWorld(provider: DojoProvider) {
Expand Down
4 changes: 2 additions & 2 deletions examples/example-vite-react-sdk/src/typescript/models.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ export const schema: SchemaType = {
Position: {
fieldOrder: ["player", "vec"],
player: "",
vec: { fieldOrder: ["x", "y"], x: 0, y: 0 },
vec: { x: 0, y: 0 },
},
PositionValue: {
fieldOrder: ["vec"],
vec: { fieldOrder: ["x", "y"], x: 0, y: 0 },
vec: { x: 0, y: 0 },
},
Vec2: {
fieldOrder: ["x", "y"],
Expand Down
6 changes: 3 additions & 3 deletions examples/example-vite-react-sdk/src/useModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ function useModel<
// Select only the specific model data for the given entityId
const modelData = useDojoStore(
(state) =>
state.entities[entityId]?.models?.[namespace]?.[modelName] as
| SchemaType[N][M]
| undefined
state.entities[entityId.toString()]?.models?.[namespace]?.[
modelName
] as SchemaType[N][M] | undefined
);

return modelData;
Expand Down
2 changes: 1 addition & 1 deletion examples/example-vite-react-sdk/tsconfig.app.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"root":["./src/app.tsx","./src/dojocontext.tsx","./src/bindings.ts","./src/contracts.gen.ts","./src/main.tsx","./src/usedojo.tsx","./src/usemodel.tsx","./src/usesystemcalls.ts","./src/vite-env.d.ts"],"version":"5.7.2"}
{"root":["./src/app.tsx","./src/dojocontext.tsx","./src/historical-events.tsx","./src/main.tsx","./src/starknet-provider.tsx","./src/usedojo.tsx","./src/usemodel.tsx","./src/usesystemcalls.ts","./src/vite-env.d.ts","./src/wallet-account.tsx","./src/typescript/contracts.gen.ts","./src/typescript/models.gen.ts"],"version":"5.7.2"}
Loading

0 comments on commit ffc30b4

Please sign in to comment.