-
Notifications
You must be signed in to change notification settings - Fork 54
chore: bump dojo 1.5 #434
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
chore: bump dojo 1.5 #434
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,15 @@ | ||
--- | ||
"@dojoengine/torii-wasm": minor | ||
"@dojoengine/state": minor | ||
"@dojoengine/sdk": minor | ||
"@dojoengine/core": minor | ||
"@dojoengine/create-burner": minor | ||
"@dojoengine/create-dojo": minor | ||
"@dojoengine/predeployed-connector": minor | ||
"@dojoengine/react": minor | ||
"@dojoengine/torii-client": minor | ||
"@dojoengine/utils": minor | ||
"@dojoengine/utils-wasm": minor | ||
--- | ||
|
||
chore: bump dojo 1.5 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import "dotenv/config"; | ||
import { z } from "zod"; | ||
|
||
const envSchema = z.object({ | ||
IDENTITY: z.string(), | ||
SECRET_KEY: z.string(), | ||
}); | ||
|
||
export const env = envSchema.parse(process.env); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,53 +1,177 @@ | ||||||||||||||||||||||||||||||||||||||
import { init, KeysClause, ToriiQueryBuilder } from "@dojoengine/sdk/node"; | ||||||||||||||||||||||||||||||||||||||
import { dojoConfig } from "./dojoConfig"; | ||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||
KeysClause, | ||||||||||||||||||||||||||||||||||||||
ToriiQueryBuilder, | ||||||||||||||||||||||||||||||||||||||
createWorker, | ||||||||||||||||||||||||||||||||||||||
init, | ||||||||||||||||||||||||||||||||||||||
type ParsedEntity, | ||||||||||||||||||||||||||||||||||||||
getModel, | ||||||||||||||||||||||||||||||||||||||
HistoricalToriiQueryBuilder, | ||||||||||||||||||||||||||||||||||||||
} from "@dojoengine/sdk/node"; | ||||||||||||||||||||||||||||||||||||||
import { SigningKey } from "@dojoengine/torii-wasm/node"; | ||||||||||||||||||||||||||||||||||||||
import { dojoConfig } from "./dojoConfig.ts"; | ||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||
ModelsMapping, | ||||||||||||||||||||||||||||||||||||||
type Moves, | ||||||||||||||||||||||||||||||||||||||
type SchemaType, | ||||||||||||||||||||||||||||||||||||||
} from "./src/typescript/models.gen.ts"; | ||||||||||||||||||||||||||||||||||||||
import { addAddressPadding, type BigNumberish } from "starknet"; | ||||||||||||||||||||||||||||||||||||||
import { w3cwebsocket } from "websocket"; | ||||||||||||||||||||||||||||||||||||||
import { env } from "./env.ts"; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Those lines are require so that websocket works. | ||||||||||||||||||||||||||||||||||||||
// @ts-ignore | ||||||||||||||||||||||||||||||||||||||
global.Websocket = w3cwebsocket; | ||||||||||||||||||||||||||||||||||||||
// @ts-ignore | ||||||||||||||||||||||||||||||||||||||
global.WorkerGlobalScope = global; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const sdk = await init({ | ||||||||||||||||||||||||||||||||||||||
client: { | ||||||||||||||||||||||||||||||||||||||
toriiUrl: dojoConfig.toriiUrl, | ||||||||||||||||||||||||||||||||||||||
relayUrl: dojoConfig.relayUrl, | ||||||||||||||||||||||||||||||||||||||
relayUrl: "/ip4/127.0.0.1/tcp/9092/ws", | ||||||||||||||||||||||||||||||||||||||
worldAddress: dojoConfig.manifest.world.address, | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
domain: {}, | ||||||||||||||||||||||||||||||||||||||
domain: { | ||||||||||||||||||||||||||||||||||||||
name: "node-worker", | ||||||||||||||||||||||||||||||||||||||
version: "0.0.1", | ||||||||||||||||||||||||||||||||||||||
chainId: "KATANA", | ||||||||||||||||||||||||||||||||||||||
revision: "1", | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
identity: env.IDENTITY, | ||||||||||||||||||||||||||||||||||||||
signer: SigningKey.fromSecretScalar(env.SECRET_KEY), | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const query = new ToriiQueryBuilder() | ||||||||||||||||||||||||||||||||||||||
.withClause( | ||||||||||||||||||||||||||||||||||||||
KeysClause( | ||||||||||||||||||||||||||||||||||||||
["dojo_starter-Position", "dojo_starter-Moves"], | ||||||||||||||||||||||||||||||||||||||
[undefined] | ||||||||||||||||||||||||||||||||||||||
).build() | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
.includeHashedKeys(); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
function onEntityUpdated({ data, error }) { | ||||||||||||||||||||||||||||||||||||||
if (error) { | ||||||||||||||||||||||||||||||||||||||
console.error(error); | ||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||
type PositionCount = { | ||||||||||||||||||||||||||||||||||||||
Up: number; | ||||||||||||||||||||||||||||||||||||||
Down: number; | ||||||||||||||||||||||||||||||||||||||
Left: number; | ||||||||||||||||||||||||||||||||||||||
Right: number; | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
const defaultPositionCount = { | ||||||||||||||||||||||||||||||||||||||
Up: 0, | ||||||||||||||||||||||||||||||||||||||
Down: 0, | ||||||||||||||||||||||||||||||||||||||
Left: 0, | ||||||||||||||||||||||||||||||||||||||
Right: 0, | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const positionToU8 = (position: string) => { | ||||||||||||||||||||||||||||||||||||||
const mapping = { | ||||||||||||||||||||||||||||||||||||||
Up: 0, | ||||||||||||||||||||||||||||||||||||||
Down: 1, | ||||||||||||||||||||||||||||||||||||||
Left: 2, | ||||||||||||||||||||||||||||||||||||||
Right: 3, | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// 10 is used as default value | ||||||||||||||||||||||||||||||||||||||
return mapping[position] ?? 10; | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
const intoPositionCount = ( | ||||||||||||||||||||||||||||||||||||||
pos: PositionCount | ||||||||||||||||||||||||||||||||||||||
): Array<[BigNumberish, BigNumberish]> => { | ||||||||||||||||||||||||||||||||||||||
return [ | ||||||||||||||||||||||||||||||||||||||
[positionToU8("Up"), `0x${pos.Up.toString(16)}`], | ||||||||||||||||||||||||||||||||||||||
[positionToU8("Down"), `0x${pos.Down.toString(16)}`], | ||||||||||||||||||||||||||||||||||||||
[positionToU8("Left"), `0x${pos.Left.toString(16)}`], | ||||||||||||||||||||||||||||||||||||||
[positionToU8("Right"), `0x${pos.Right.toString(16)}`], | ||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
async function publishOffchainPositionCount(moves: Moves): Promise<void> { | ||||||||||||||||||||||||||||||||||||||
const model = await sdk.getEntities({ | ||||||||||||||||||||||||||||||||||||||
query: new ToriiQueryBuilder() | ||||||||||||||||||||||||||||||||||||||
.withClause( | ||||||||||||||||||||||||||||||||||||||
KeysClause( | ||||||||||||||||||||||||||||||||||||||
[ModelsMapping.PositionCount], | ||||||||||||||||||||||||||||||||||||||
[addAddressPadding(moves.player)] | ||||||||||||||||||||||||||||||||||||||
).build() | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
.includeHashedKeys(), | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
const m = getModel(ModelsMapping.PositionCount, model); | ||||||||||||||||||||||||||||||||||||||
if (!m) { | ||||||||||||||||||||||||||||||||||||||
const data = sdk.generateTypedData( | ||||||||||||||||||||||||||||||||||||||
ModelsMapping.PositionCount, | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
identity: moves.player, | ||||||||||||||||||||||||||||||||||||||
position: intoPositionCount(positionCount), | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
name: "identity", | ||||||||||||||||||||||||||||||||||||||
type: "felt", | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
name: "position", | ||||||||||||||||||||||||||||||||||||||
type: "(u8, u128)*", | ||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||
await sdk.sendMessage(data); | ||||||||||||||||||||||||||||||||||||||
} catch (err) { | ||||||||||||||||||||||||||||||||||||||
console.error(err); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||
console.log(m.position); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const entity = data.pop(); | ||||||||||||||||||||||||||||||||||||||
if (entity && entity.entityId !== "0x0") { | ||||||||||||||||||||||||||||||||||||||
// do whatever you need here | ||||||||||||||||||||||||||||||||||||||
console.log(entity.models.dojo_starter); | ||||||||||||||||||||||||||||||||||||||
let positionCount = defaultPositionCount; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
function initPositionFromEvent( | ||||||||||||||||||||||||||||||||||||||
events: ParsedEntity<SchemaType>[] | ||||||||||||||||||||||||||||||||||||||
): PositionCount { | ||||||||||||||||||||||||||||||||||||||
const pc = defaultPositionCount; | ||||||||||||||||||||||||||||||||||||||
for (const e of events) { | ||||||||||||||||||||||||||||||||||||||
const moved = e.models.dojo_starter.Moved; | ||||||||||||||||||||||||||||||||||||||
if (!moved) { | ||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
pc[moved.direction] += 1; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
return pc; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+125
to
+133
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.
-const pc = defaultPositionCount;
+const pc: PositionCount = { ...defaultPositionCount }; This also prevents accidental modification of the default object elsewhere. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const [entities, sub] = await sdk.subscribeEntityQuery({ | ||||||||||||||||||||||||||||||||||||||
query, | ||||||||||||||||||||||||||||||||||||||
callback: onEntityUpdated, | ||||||||||||||||||||||||||||||||||||||
historical: false, | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
await createWorker(async () => { | ||||||||||||||||||||||||||||||||||||||
async function onEntityUpdated({ data, error }) { | ||||||||||||||||||||||||||||||||||||||
if (error) { | ||||||||||||||||||||||||||||||||||||||
console.error(error); | ||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
console.log(entities); | ||||||||||||||||||||||||||||||||||||||
const entity = data.pop(); | ||||||||||||||||||||||||||||||||||||||
if (entity && entity.entityId !== "0x0") { | ||||||||||||||||||||||||||||||||||||||
// do whatever you need here | ||||||||||||||||||||||||||||||||||||||
const model = entity.models.dojo_starter; | ||||||||||||||||||||||||||||||||||||||
if (model?.Moves) { | ||||||||||||||||||||||||||||||||||||||
await publishOffchainPositionCount(model.Moves); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Comment on lines
+137
to
+151
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. 🛠️ Refactor suggestion Potential data loss with
Iterate instead: for (const entity of data) {
// handle each entity
} |
||||||||||||||||||||||||||||||||||||||
process.on("SIGTERM", () => { | ||||||||||||||||||||||||||||||||||||||
// NOTE: do not forget to free sub here; | ||||||||||||||||||||||||||||||||||||||
sub.free(); | ||||||||||||||||||||||||||||||||||||||
process.exit(0); | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
const query = new HistoricalToriiQueryBuilder() | ||||||||||||||||||||||||||||||||||||||
.withClause( | ||||||||||||||||||||||||||||||||||||||
KeysClause( | ||||||||||||||||||||||||||||||||||||||
[ModelsMapping.Moved, ModelsMapping.Moves], | ||||||||||||||||||||||||||||||||||||||
[undefined] | ||||||||||||||||||||||||||||||||||||||
).build() | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
.includeHashedKeys(); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const events = await sdk.getEventMessages({ | ||||||||||||||||||||||||||||||||||||||
query: query, | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
positionCount = initPositionFromEvent(events); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const [entities, sub] = await sdk.subscribeEntityQuery({ | ||||||||||||||||||||||||||||||||||||||
query, | ||||||||||||||||||||||||||||||||||||||
callback: onEntityUpdated, | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
console.log("Entities from worker", entities); | ||||||||||||||||||||||||||||||||||||||
console.log(positionCount); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
process.on("SIGINT", () => { | ||||||||||||||||||||||||||||||||||||||
// NOTE: do not forget to free sub here; | ||||||||||||||||||||||||||||||||||||||
sub.free(); | ||||||||||||||||||||||||||||||||||||||
process.exit(0); | ||||||||||||||||||||||||||||||||||||||
return []; | ||||||||||||||||||||||||||||||||||||||
return [sub]; | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+175
to
+176
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. 🛠️ Refactor suggestion Unreachable code after the first The second - return [];
- return [sub];
+ return [sub]; 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (1.9.4)[error] 176-176: This code is unreachable ... because this statement will return from the function beforehand (lint/correctness/noUnreachable) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,11 +3,22 @@ | |||||||||||||||||||||
"version": "0.0.1", | ||||||||||||||||||||||
"private": true, | ||||||||||||||||||||||
"type": "module", | ||||||||||||||||||||||
"scripts": { | ||||||||||||||||||||||
"bun": "IDENTITY=0x0127fd5f1fe78a71f8bcd1fec63e3fe2f0486b6ecd5c86a0466c3a21fa5cfcec SECRET_KEY=0xc5b2fcab997346f3ea1c00b002ecf6f382c5f9c9659a3894eb783c5320f912 bun run main.ts", | ||||||||||||||||||||||
"node": "IDENTITY=0x0127fd5f1fe78a71f8bcd1fec63e3fe2f0486b6ecd5c86a0466c3a21fa5cfcec SECRET_KEY=0xc5b2fcab997346f3ea1c00b002ecf6f382c5f9c9659a3894eb783c5320f912 node main.ts", | ||||||||||||||||||||||
"build": "tsc -b" | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
Comment on lines
+6
to
+10
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. Remove hardcoded credentials from scripts The scripts contain hardcoded private keys and identity values, which is a security risk. These should be moved to environment variables loaded from a .env file instead. "scripts": {
- "bun": "IDENTITY=0x0127fd5f1fe78a71f8bcd1fec63e3fe2f0486b6ecd5c86a0466c3a21fa5cfcec SECRET_KEY=0xc5b2fcab997346f3ea1c00b002ecf6f382c5f9c9659a3894eb783c5320f912 bun run main.ts",
- "node": "IDENTITY=0x0127fd5f1fe78a71f8bcd1fec63e3fe2f0486b6ecd5c86a0466c3a21fa5cfcec SECRET_KEY=0xc5b2fcab997346f3ea1c00b002ecf6f382c5f9c9659a3894eb783c5320f912 node main.ts",
+ "bun": "bun run main.ts",
+ "node": "node main.ts",
"build": "tsc -b"
}, Since you've already added 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Gitleaks (8.21.2)7-7: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key) 8-8: Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (generic-api-key) |
||||||||||||||||||||||
"dependencies": { | ||||||||||||||||||||||
"@dojoengine/core": "workspace:*", | ||||||||||||||||||||||
"@dojoengine/sdk": "workspace:*" | ||||||||||||||||||||||
"@dojoengine/sdk": "workspace:*", | ||||||||||||||||||||||
"@dojoengine/torii-wasm": "workspace:*", | ||||||||||||||||||||||
"dotenv": "^16.4.7", | ||||||||||||||||||||||
"starknet": "catalog:", | ||||||||||||||||||||||
"websocket": "^1.0.35", | ||||||||||||||||||||||
"zod": "^3.24.3" | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
"devDependencies": { | ||||||||||||||||||||||
"@types/node": "^22.14.1" | ||||||||||||||||||||||
"@types/node": "^22.14.1", | ||||||||||||||||||||||
"@types/websocket": "^1.0.10" | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} |
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.
Wrong global identifier: use
WebSocket
, notWebsocket
Browsers (and most polyfills) expose
global.WebSocket
(uppercase S).Assigning to
Websocket
leaves libraries that expectWebSocket
undefined.📝 Committable suggestion