-
Notifications
You must be signed in to change notification settings - Fork 54
feat: deprecate QueryBuilder -- Default to ToriiQueryBuilder #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
"@dojoengine/sdk": minor | ||
"template-vite-ts": minor | ||
"@dojoengine/core": minor | ||
"@dojoengine/create-burner": minor | ||
"@dojoengine/create-dojo": minor | ||
"@dojoengine/predeployed-connector": minor | ||
"@dojoengine/react": minor | ||
"@dojoengine/state": minor | ||
"@dojoengine/torii-client": minor | ||
"@dojoengine/torii-wasm": minor | ||
"@dojoengine/utils": minor | ||
"@dojoengine/utils-wasm": minor | ||
--- | ||
|
||
Default to ToriiQueryBuilder for queries with Clause |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,12 @@ import { useCallback, useEffect, useRef, useState, KeyboardEvent } from "react"; | |
import { useForm } from "react-hook-form"; | ||
import { useAccount } from "@starknet-react/core"; | ||
import { toValidAscii } from "@/lib/utils"; | ||
import { ParsedEntity, SDK } from "@dojoengine/sdk"; | ||
import { | ||
KeysClause, | ||
ParsedEntity, | ||
SDK, | ||
ToriiQueryBuilder, | ||
} from "@dojoengine/sdk"; | ||
import { Subscription } from "@dojoengine/torii-wasm"; | ||
import { shortAddress } from "@/lib/utils"; | ||
import { Message, SchemaType } from "@/typescript/models.gen"; | ||
|
@@ -61,40 +66,18 @@ export default function Chat() { | |
[db, account, reset] | ||
); | ||
|
||
useEffect(() => { | ||
async function getEntity(db: SDK<SchemaType>) { | ||
const entity = await db.getEntities({ | ||
query: { | ||
onchain_dash: { Message: { $: {} } }, | ||
}, | ||
callback: () => {}, | ||
}); | ||
|
||
return ( | ||
entity | ||
.map((e) => e.models.onchain_dash.Message) | ||
.filter(Boolean) | ||
// @ts-expect-error a & b are not undefined as they are filtered out with `filer(Boolean)` | ||
.sort((a: Message, b: Message): number => | ||
parseInt(a.timestamp.toString(), 16) < | ||
parseInt(b.timestamp.toString(), 16) | ||
? -1 | ||
: 1 | ||
) | ||
); | ||
} | ||
if (db && messages.length === 0 && sub === null) { | ||
// @ts-expect-error ts is getting drunk there | ||
getEntity(db).then(setMessages).catch(console.error); | ||
} | ||
}, [db, messages, sub]); | ||
|
||
useEffect(() => { | ||
async function subscribeToEntityUpdates(db: SDK<SchemaType>) { | ||
const sub = await db.subscribeEntityQuery({ | ||
query: { | ||
onchain_dash: { Message: { $: {} } }, | ||
}, | ||
const [initialMessages, sub] = await db.subscribeEntityQuery({ | ||
query: new ToriiQueryBuilder() | ||
.withClause( | ||
KeysClause( | ||
["onchain_dash-Message"], | ||
[undefined], | ||
"FixedLen" | ||
).build() | ||
) | ||
.includeHashedKeys(), | ||
callback: ({ data }) => { | ||
if (data) { | ||
const entity = data.pop() as ParsedEntity<SchemaType>; | ||
|
@@ -113,6 +96,18 @@ export default function Chat() { | |
}, | ||
}); | ||
setSub(sub); | ||
|
||
setMessages( | ||
initialMessages | ||
.map((e) => e.models.onchain_dash.Message as MessageItem) | ||
.filter(Boolean) | ||
.sort((a: Message, b: Message): number => | ||
parseInt(a.timestamp.toString(), 16) < | ||
parseInt(b.timestamp.toString(), 16) | ||
? -1 | ||
: 1 | ||
) | ||
); | ||
Comment on lines
+100
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null check for initialMessages. The code directly processes initialMessages without checking if it's empty, which could lead to runtime errors. Apply this diff to add the necessary check: + if (!initialMessages.length) {
+ setMessages([]);
+ return;
+ }
setMessages(
initialMessages
.map((e) => e.models.onchain_dash.Message as MessageItem)
.filter(Boolean)
.sort((a: Message, b: Message): number =>
parseInt(a.timestamp.toString(), 16) <
parseInt(b.timestamp.toString(), 16)
? -1
: 1
)
); |
||
} | ||
if (db && sub === null) { | ||
subscribeToEntityUpdates(db).then().catch(console.error); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { Button } from "./ui/button"; | ||
import { useSendTransaction } from "@starknet-react/core"; | ||
import { ParsedEntity, QueryBuilder, SDK } from "@dojoengine/sdk"; | ||
import { | ||
KeysClause, | ||
ParsedEntity, | ||
SDK, | ||
ToriiQueryBuilder, | ||
} from "@dojoengine/sdk"; | ||
import { Subscription } from "@dojoengine/torii-wasm"; | ||
import { dojoConfig } from "@/../dojoConfig"; | ||
import { SchemaType } from "@/typescript/models.gen"; | ||
|
@@ -28,45 +33,19 @@ export default function GlobalCOunter() { | |
|
||
const { sdk: db } = useDojoSDK<typeof setupWorld, SchemaType>(); | ||
|
||
useEffect(() => { | ||
async function getEntity(db: SDK<SchemaType>) { | ||
const entity = await db.getEntities({ | ||
query: new QueryBuilder<SchemaType>() | ||
.namespace("onchain_dash", (n) => | ||
n.entity("GlobalCounter", (e) => | ||
e.eq("global_counter_key", 9999999) | ||
) | ||
) | ||
.build(), | ||
callback: ({ data, error }) => {}, | ||
}); | ||
|
||
const counter = entity.pop() as ParsedEntity<SchemaType>; | ||
if (!counter) { | ||
return 0; | ||
} | ||
const count = counter.models.onchain_dash?.GlobalCounter?.counter; | ||
if (undefined === count) { | ||
return 0; | ||
} | ||
return parseInt(count.toString(), 16); | ||
} | ||
|
||
if (db) { | ||
getEntity(db).then(setCount).catch(console.error); | ||
} | ||
}, [db]); | ||
|
||
useEffect(() => { | ||
async function subscribeToEntityUpdates(db: SDK<SchemaType>) { | ||
const sub = await db.subscribeEntityQuery({ | ||
query: new QueryBuilder<SchemaType>() | ||
.namespace("onchain_dash", (n) => | ||
n.entity("GlobalCounter", (e) => | ||
e.eq("global_counter_key", 9999999) | ||
) | ||
const [initialEntities, sub] = await db.subscribeEntityQuery({ | ||
// Here it is important to includeHashedKeys as subscription requires hasedKeys to query entityIds | ||
query: new ToriiQueryBuilder() | ||
.withClause( | ||
KeysClause( | ||
["onchain_dash-GlobalCounter"], | ||
["9999999"], | ||
"VariableLen" | ||
).build() | ||
) | ||
.build(), | ||
.includeHashedKeys(), | ||
Comment on lines
+40
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainVerify the hardcoded values in KeysClause. The hardcoded values 🏁 Script executed: #!/bin/bash
# Search for other occurrences of these hardcoded values
rg "9999999|onchain_dash-GlobalCounter" Length of output: 812 Hardcoded Value Extraction Recommended The search confirms that the literal values "onchain_dash-GlobalCounter" and "9999999" are used in multiple parts of the codebase:
Given their repeated usage, it would be beneficial to extract these as constants or configuration values to improve maintainability and consistency. |
||
callback: ({ data, error }) => { | ||
if (data) { | ||
const entity = data.pop() as ParsedEntity<SchemaType>; | ||
|
@@ -95,6 +74,14 @@ export default function GlobalCOunter() { | |
}, | ||
}); | ||
setSub(sub); | ||
|
||
const count = | ||
initialEntities[0]?.models.onchain_dash?.GlobalCounter?.counter; | ||
if (undefined === count) { | ||
setCount(0); | ||
} else { | ||
setCount(parseInt(count.toString(), 16)); | ||
} | ||
} | ||
if (db && sub === null) { | ||
subscribeToEntityUpdates(db) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add null check for initialEntities[0].
The code directly accesses
initialEntities[0]
without checking if the array is empty, which could lead to runtime errors.Apply this diff to add the necessary check:
📝 Committable suggestion