Skip to content

Nodejs wasm #257

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

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/node/bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# deps
node_modules/

.env
.dev.vars
16 changes: 16 additions & 0 deletions examples/node/bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
To install dependencies:

```sh
bun install
```

To run:

```sh
bun run dev
```

open http://localhost:3000

- [] User CRUD
- [] Add in DB for maintaining balances
7 changes: 7 additions & 0 deletions examples/node/bot/dojoConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import manifest from "../../dojo-starter/manifests/dev/deployment/manifest.json" assert { type: "json" };

import { createDojoConfig } from "@dojoengine/core";

export const dojoConfig = createDojoConfig({
manifest,
});
10 changes: 10 additions & 0 deletions examples/node/bot/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Config } from "drizzle-kit";

export default {
schema: "./src/db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
introspect: {
casing: "camel",
},
} satisfies Config;
8 changes: 8 additions & 0 deletions examples/node/bot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createClient } from "@dojoengine/torii-client";

export const toriiClient = await createClient([], {
rpcUrl: dojoConfig.rpcUrl,
toriiUrl: dojoConfig.toriiUrl,
relayUrl: "",
worldAddress: dojoConfig.manifest.world.address || "",
});
22 changes: 22 additions & 0 deletions examples/node/bot/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { config } from "dotenv";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
import { drizzle } from "drizzle-orm/postgres-js";

config();

const databaseUrl = drizzle(
postgres(`${process.env.DATABASE_URL}`, { ssl: "require", max: 1 })
);

const main = async () => {
try {
await migrate(databaseUrl, { migrationsFolder: "drizzle" });
console.log("Migration complete");
} catch (error) {
console.log(error);
}
process.exit(0);
};

main();
53 changes: 53 additions & 0 deletions examples/node/bot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "my-app",
"type": "module",
"main": "src/index.ts",
"scripts": {
"dev": "bun run --hot src/index.ts",
"db:generate": "drizzle-kit generate",
"db:migrate": "tsx migrate.ts",
"test": "vitest"
},
"dependencies": {
"@dojoengine/core": "workspace:*",
"@dojoengine/recs": "^2.0.13",
"@dojoengine/state": "workspace:*",
"@dojoengine/torii-client": "workspace:*",
"@dojoengine/torii-wasm": "workspace:*",
"@dojoengine/utils": "workspace:*",
"@hono/zod-validator": "^0.2.2",
"@neondatabase/serverless": "^0.9.3",
"@pinecone-database/pinecone": "^1.1.2",
"@reservoir0x/reservoir-sdk": "^2.2.12",
"@sapphire/decorators": "^6.1.0",
"@sapphire/framework": "^5.2.1",
"discord.js": "^14.15.3",
"dotenv": "^16.4.5",
"drizzle-kit": "^0.22.7",
"drizzle-orm": "^0.31.2",
"ethers": "^6.13.0",
"express": "^4.18.2",
"hono": "^4.4.8",
"langchain": "^0.2.7 ",
"node-cron": "^3.0.3",
"openai": "^4.52.1",
"postgres": "^3.4.4",
"tsx": "^4.15.5",
"twitter-api-v2": "^1.15.2",
"typescript": "^5.2.2",
"vitest": "^1.6.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-graphql-request": "^6.0.0",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@types/bun": "latest",
"@types/express": "^4.17.17",
"@types/node": "^20.11.10",
"@types/node-cron": "^3.0.10",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0"
}
}
37 changes: 37 additions & 0 deletions examples/node/bot/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Hono } from "hono";
import { GatewayIntentBits } from "discord.js";
import { SapphireClient } from "@sapphire/framework";
import * as torii from "@dojoengine/torii-wasm";
import { syncEntities } from "@dojoengine/state";

import { createWorld } from "@dojoengine/recs";
import { dojoConfig } from "../dojoConfig";

const app = new Hono();

async function initializeToriiClient() {
return await torii.createClient({
rpcUrl: dojoConfig.rpcUrl,
toriiUrl: dojoConfig.toriiUrl,
relayUrl: "",
worldAddress: dojoConfig.manifest.world.address || "",
});
}

let toriiClient: Awaited<ReturnType<typeof torii.createClient>>;

app.use(async (c, next) => {
if (!toriiClient) {
toriiClient = await initializeToriiClient();

toriiClient.onEntityUpdated([], (fetchedEntities: any, data: any) => {
console.log("Entity updated", data);
});
}
await next();
});

export default {
port: 7070,
fetch: app.fetch,
};
12 changes: 12 additions & 0 deletions examples/node/bot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"strict": true,
"jsxImportSource": "hono/jsx",
"esModuleInterop": true
}
}
7 changes: 4 additions & 3 deletions examples/node/torii-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"devDependencies": {
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"graphql": "^16.8.1",
"@graphql-codegen/typescript-graphql-request": "^6.0.0",
"graphql-request": "^6.1.0",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@types/express": "^4.17.17",
"@types/node": "^20.11.10",
"@types/node-cron": "^3.0.10",
"bun-types": "latest"
"bun-types": "latest",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0"
},
"dependencies": {
"@dojoengine/core": "workspace:*",
Expand All @@ -22,6 +22,7 @@
"@sapphire/framework": "^4.6.0",
"discord.js": "^14.13.0",
"express": "^4.18.2",
"graphql-tag": "^2.12.6",
"langchain": "^0.0.200",
"node-cron": "^3.0.2",
"openai": "^4.20.1",
Expand Down
4 changes: 1 addition & 3 deletions examples/node/torii-bot/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { getSdk } from "./generated/graphql.js";
import { GraphQLClient } from "graphql-request";
import { dojoConfig } from "../dojoConfig.js";

export const sdk = getSdk(
new GraphQLClient(dojoConfig().toriiUrl + "/graphql")
);
export const sdk = getSdk(new GraphQLClient(dojoConfig.toriiUrl + "/graphql"));

export const POLL_INTERVAL = 3000;
3 changes: 2 additions & 1 deletion packages/torii-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"./pkg/node/dojo_c.d.ts"
],
"sideEffects": [
"./pkg/web/dojo_c.js"
"./pkg/web/dojo_c.js",
"./pkg/web/dojo_c_bg.wasm"
],
"devDependencies": {
"tsup": "^8.1.0",
Expand Down
Loading
Loading