Skip to content

Commit 6d0dab2

Browse files
wip: example bot
1 parent b619de0 commit 6d0dab2

7 files changed

Lines changed: 128 additions & 21 deletions

File tree

packages/client/src/mud/stash.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { createStash } from "@latticexyz/stash/internal";
22
import config from "contracts/mud.config";
33

4-
export const stash: ReturnType<typeof createStash<typeof config>> = createStash(config);
4+
// For some reason when importing config in Node.js, instead of just
5+
// being the object, it's wrapped in an object with a `default` property.
6+
export const stash: ReturnType<typeof createStash<typeof config>> = createStash(
7+
"default" in config ? (config.default as typeof config) : config
8+
);

packages/example-bot/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PRIVATE_KEY=0x...

packages/example-bot/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.db
2+
.env

packages/example-bot/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "example-bot",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"build": "tsc",
8+
"start": "tsx src/bot.ts"
9+
},
10+
"license": "MIT",
11+
"devDependencies": {
12+
"@types/node": "^18.15.11",
13+
"@types/yargs": "^17.0.32",
14+
"tsx": "^4.16.2"
15+
},
16+
"dependencies": {
17+
"@franciscokloganb/local-storage-polyfill": "^0.1.0",
18+
"viem": "^2.29.1",
19+
"yargs": "^17.7.2"
20+
}
21+
}

packages/example-bot/src/bot.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import "dotenv/config";
2+
import { http, fallback, webSocket, createPublicClient } from "viem";
3+
import { Entity } from "../../client/src/utils/game/entityLib";
4+
import { LiveState } from "../../client/src/utils/sync";
5+
import { GameConfig } from "../../client/src/utils/game/configLib";
6+
7+
import { stash } from "../../client/src/mud/stash";
8+
import { Stash } from "@latticexyz/stash/internal";
9+
import { syncToStash } from "@latticexyz/store-sync/internal";
10+
import { ODYSSEY_CHAIN } from "../../client/src/utils/chains";
11+
import Worlds from "../../contracts/worlds.json";
12+
13+
const chain = ODYSSEY_CHAIN;
14+
const WORLD_ADDRESS = Worlds[chain.id]?.address as `0x${string}`;
15+
if (!WORLD_ADDRESS) throw new Error(`No world address found for chain ${chain.id}`);
16+
const START_BLOCK = Worlds[chain.id]!.blockNumber!;
17+
18+
const publicClient = createPublicClient({
19+
chain,
20+
transport: fallback([webSocket(), http()]),
21+
pollingInterval: 100,
22+
cacheTime: 100,
23+
});
24+
25+
function botDecision(
26+
bot: Entity,
27+
liveState: LiveState,
28+
gameConfig: GameConfig,
29+
decision: "vertical" | "horizontal",
30+
wadTime: bigint
31+
): [string, string] {
32+
// TODO
33+
return ["up", "right"];
34+
}
35+
36+
export async function main() {
37+
syncToStash({
38+
stash: stash as Stash,
39+
startSync: true,
40+
address: WORLD_ADDRESS,
41+
startBlock: BigInt(START_BLOCK ?? 0),
42+
publicClient: publicClient as any,
43+
indexerUrl: chain.indexerUrl,
44+
});
45+
}
46+
47+
console.log("Starting bot...", WORLD_ADDRESS, START_BLOCK);
48+
await main();

packages/example-bot/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"module": "ESNext",
5+
"moduleResolution": "Node",
6+
"esModuleInterop": true,
7+
"jsx": "react"
8+
},
9+
"include": ["src"]
10+
}

pnpm-lock.yaml

Lines changed: 41 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)