Skip to content

Commit 509f5e9

Browse files
feat: prettier
1 parent 9c2e4dd commit 509f5e9

File tree

1 file changed

+170
-23
lines changed

1 file changed

+170
-23
lines changed

packages/sdk/readme.md

Lines changed: 170 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
# Dojo SDK: Type-Safe Onchain State
1+
# Dojo SDK: Build onchain faster
22

3-
The Dojo SDK offers a powerful and intuitive way to interact with your onchain state. It provides a seamless experience for fetching and subscribing to data, supporting both simple and complex queries.
3+
The Dojo SDK provides a powerful, intuitive interface for interacting with onchain state. It streamlines data fetching and subscriptions, supporting both simple and complex queries.
4+
5+
## Table of Contents
6+
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [Initializing the SDK](#initializing-the-sdk)
10+
- [Understanding Queries](#understanding-queries)
11+
- [Querying Entities](#querying-entities)
12+
- [Subscribing to Entity Changes](#subscribing-to-entity-changes)
13+
- [Sending Signed Messages](#sending-signed-messages)
14+
- [Using with Zustand](#using-with-zustand)
15+
- [Optimistic Client Rendering](#optimistic-client-rendering)
16+
- [Advanced Usage](#advanced-usage)
17+
- [Complex Queries](#complex-queries)
18+
- [Troubleshooting](#troubleshooting)
419

520
## Key Features
621

722
- **Type Safety**: Leverage TypeScript for robust, error-resistant code.
8-
- **Intuitive Query Syntax**: Write queries that feel natural, similar to popular ORMs.
9-
- **Flexible Subscriptions**: Easily subscribe to specific state changes in your Dojo world.
10-
- **Built in support for signed messages**: Sign off-chain state and send to torii.
23+
- **Intuitive query syntax**: Write queries that feel natural, similar to popular ORMs.
24+
- **Flexible subscriptions**: Easily subscribe to specific state changes in your Dojo world.
25+
- **Signed messages**: Sign off-chain state and send to torii.
26+
- **Automatic Zustand Support**: Drop in zustand state management
27+
- **Optimistic Client Rendering**: Set state before a transaction has resolved to improve user experiences
1128

12-
## Understand Entities and Models
29+
### Understand Entities and Models
1330

1431
- Entities are uniquely identified by Keys defined in associated models
1532
- Entities can have multiple models, representing complex game states
@@ -43,7 +60,9 @@ const subscription = await sdk.subscribeEntityQuery(
4360
subscription.unsubscribe();
4461
```
4562

46-
## 🚀 Getting Started
63+
# Usage
64+
65+
## 🚀 Installation
4766

4867
```bash
4968
npm install @dojoengine/sdk
@@ -63,7 +82,7 @@ To take advantage of this type safety (You will need [dojo](https://github.com/d
6382

6483
This approach ensures that your code remains in sync with your Dojo world definition, catching potential issues early in the development process.
6584

66-
## Usage
85+
## Initializing the SDK
6786

6887
```typescript
6988
import { init, SchemaType } from "@dojoengine/sdk";
@@ -113,7 +132,7 @@ const db = await init<MockSchemaType>(
113132
// Voila! Now you have a typed interface
114133
```
115134
116-
## Query Explanation
135+
## Understanding Queries
117136
118137
The SDK utilizes two primary types of queries to interact with the Dojo Engine:
119138
@@ -125,22 +144,54 @@ Both query types enable filtering based on `entityIds` and specific model proper
125144
- **`SubscriptionQueryType`**:
126145
- Supports only the `$is` operator for exact matches.
127146
- **`QueryType`**:
128-
- Supports a variety of operators including `$eq`, `$neq`, `$gt`, `$gte`, `$lt`, and `$lte` for more advanced filtering.
129147
130-
## Examples
148+
- Supports a variety of operators for more advanced filtering:
149+
150+
| Operator | Description |
151+
| -------- | ------------------------ |
152+
| `$eq` | Equal to |
153+
| `$neq` | Not equal to |
154+
| `$gt` | Greater than |
155+
| `$gte` | Greater than or equal to |
156+
| `$lt` | Less than |
157+
| `$lte` | Less than or equal to |
158+
159+
- You combine queries with 'AND' with 'OR' from deep queries. See [Advanced Usage](#advanced-usage).
131160
132-
### Subscribing to Entity Updates
161+
## Querying Entities
133162
134-
This example demonstrates how to subscribe to entity updates in the `world` namespace, specifically for the `item` model. It filters for items where the type is "sword" and the durability is 5. This subscription will trigger the callback function whenever an item matching these criteria is updated, created, or deleted.
163+
This example fetches `player` entities from the `world` namespace where `id` is "1" and `name` is "Alice", demonstrating multiple conditions in a query.
164+
165+
Note: `$eq` is for exact matching. Other operators (`$gt`, `$lt`, etc.) are available for complex queries.
166+
167+
```typescript
168+
const entities = await sdk.getEntities(
169+
{
170+
world: {
171+
player: {
172+
$: { where: { id: { $eq: "1" }, name: { $eq: "Alice" } } },
173+
},
174+
},
175+
},
176+
(response) => {
177+
if (response.data) {
178+
console.log("Fetched entities:", response.data);
179+
} else if (response.error) {
180+
console.error("Fetch error:", response.error);
181+
}
182+
}
183+
);
184+
```
185+
186+
## Subscribing To Entity Changes
187+
188+
This example subscribes to `item` model updates in the `world` namespace, filtering for swords with durability 5. The callback triggers on matching item changes.
135189
136190
Key points:
137191
138-
- Namespace: `world`
139-
- Model: `item`
140-
- Conditions:
141-
- type: "sword"
142-
- durability: 5
143-
- Uses `$is` operator for exact matching in subscriptions
192+
- Namespace: `world`, Model: `item`
193+
- Conditions: type "sword", durability 5
194+
- Uses `$is` for exact matching
144195
145196
```typescript
146197
const subscription = await sdk.subscribeEntityQuery(
@@ -169,18 +220,112 @@ const subscription = await sdk.subscribeEntityQuery(
169220
subscription.unsubscribe();
170221
```
171222
172-
### Fetching Entities
223+
## Sending Signed Messages
224+
225+
```typescript
226+
const msg = db.generateTypedData("onchain_dash-Message", {
227+
identity: account?.address,
228+
content: toValidAscii(data.message),
229+
timestamp: Date.now(),
230+
});
231+
232+
try {
233+
const signature = await account.signMessage(msg);
234+
235+
try {
236+
await db.client.publishMessage(
237+
JSON.stringify(msg),
238+
signature as string[]
239+
);
240+
reset();
241+
} catch (error) {
242+
console.error("failed to publish message:", error);
243+
}
244+
} catch (error) {
245+
console.error("failed to sign message:", error);
246+
}
247+
```
248+
249+
## Using With Zustand
250+
251+
This module takes the `Schema` and outputs a typed store you can use around your app. See example [here](../../examples/example-vite-react-sdk/).
252+
253+
1. Import the module
254+
255+
```typescript
256+
import { createDojoStore } from "@dojoengine/sdk";
257+
258+
// import this outside of components
259+
export const useDojoStore = createDojoStore<Schema>();
260+
261+
...
262+
263+
// Using in your app
264+
const state = useDojoStore((state) => state);
265+
const entities = useDojoStore((state) => state.entities);
266+
267+
...
268+
269+
// Adding to a callback
270+
const subscription = await sdk.subscribeEntityQuery(
271+
{
272+
world: {
273+
item: {
274+
$: {
275+
where: {
276+
type: { $is: "sword" },
277+
durability: { $is: 5 },
278+
},
279+
},
280+
},
281+
},
282+
},
283+
(response) => {
284+
if (response.error) {
285+
console.error("Error setting up entity sync:", response.error);
286+
} else if (response.data && response.data[0].entityId !== "0x0") {
287+
// You just need to do this!
288+
state.setEntities(response.data);
289+
}
290+
}
291+
);
292+
```
293+
294+
## Optimistic Client Rendering
295+
296+
<!-- TODO -->
173297
174-
This example demonstrates fetching entities from the `world` namespace, specifically the `player` model. It retrieves players where the `id` equals "1" and the `name` equals "Alice". This showcases how to use multiple conditions in a query to precisely filter entities.
298+
# Advanced Usage
175299
176-
Note: The `$eq` operator is used for exact matching. Other operators like `$gt` (greater than), `$lt` (less than), etc., are available for more complex queries.
300+
Create complex 'AND' with 'OR' statements to narrow in on what you want to fetch.
301+
302+
## Complex Queries
177303
178304
```typescript
179305
const entities = await sdk.getEntities(
180306
{
181307
world: {
182308
player: {
183-
$: { where: { id: { $eq: "1" }, name: { $eq: "Alice" } } },
309+
$: {
310+
where: {
311+
AND: [
312+
{ score: { $gt: 100 } },
313+
{
314+
OR: [
315+
{ name: { $eq: "Alice" } },
316+
{ name: { $eq: "Bob" } },
317+
],
318+
},
319+
],
320+
},
321+
},
322+
},
323+
item: {
324+
$: {
325+
where: {
326+
AND: [{ durability: { $lt: 50 } }],
327+
},
328+
},
184329
},
185330
},
186331
},
@@ -193,3 +338,5 @@ const entities = await sdk.getEntities(
193338
}
194339
);
195340
```
341+
342+
## Troubleshooting

0 commit comments

Comments
 (0)