Skip to content

Commit

Permalink
feat(explorer): local indexer inside explorer (#3229)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
karooolis and holic authored Sep 25, 2024
1 parent 2f2e63a commit 95aa3bb
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 58 deletions.
19 changes: 19 additions & 0 deletions .changeset/silent-hairs-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@latticexyz/explorer": patch
"create-mud": patch
---

Explorer now automatically starts a local indexer when using Anvil as the target chain.

If you previously had an `indexer` entry in your `mprocs.yaml` file, it can now be removed.

```diff
- indexer:
- cwd: packages/contracts
- shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
- env:
- DEBUG: mud:*
- RPC_HTTP_URL: "http://127.0.0.1:8545"
- FOLLOW_BLOCK_TAG: "latest"
- SQLITE_FILENAME: "indexer.db"
```
8 changes: 0 additions & 8 deletions examples/local-explorer/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ procs:
anvil:
cwd: packages/contracts
shell: anvil --base-fee 0 --block-time 2
indexer:
cwd: packages/contracts
shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
env:
DEBUG: mud:*
RPC_HTTP_URL: "http://127.0.0.1:8545"
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
explorer:
cwd: packages/contracts
shell: pnpm explorer --dev
1 change: 1 addition & 0 deletions packages/explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@latticexyz/protocol-parser": "workspace:*",
"@latticexyz/schema-type": "workspace:*",
"@latticexyz/store": "workspace:*",
"@latticexyz/store-indexer": "workspace:*",
"@latticexyz/store-sync": "workspace:*",
"@latticexyz/world": "workspace:*",
"@radix-ui/react-checkbox": "^1.1.1",
Expand Down
47 changes: 37 additions & 10 deletions packages/explorer/src/bin/explorer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
import { watchFile } from "fs";
import { readFile } from "fs/promises";
import { readFile, rm } from "fs/promises";
import path from "path";
import process from "process";
import { fileURLToPath } from "url";
import { anvil } from "viem/chains";
import yargs from "yargs";
import { ChildProcess, spawn } from "child_process";
import { validateChainId } from "../common";
Expand Down Expand Up @@ -63,15 +64,18 @@ const argv = yargs(process.argv.slice(2))
.parseSync();

const { port, hostname, chainId, indexerDatabase, worldsFile, dev } = argv;
const indexerDatabasePath = path.join(packageRoot, indexerDatabase);

let worldAddress = argv.worldAddress;
let explorerProcess: ChildProcess;
let indexerProcess: ChildProcess;

async function startExplorer() {
const env = {
...process.env,
CHAIN_ID: chainId.toString(),
WORLD_ADDRESS: worldAddress?.toString(),
INDEXER_DATABASE: path.join(process.cwd(), indexerDatabase),
INDEXER_DATABASE: indexerDatabasePath,
};

if (dev) {
Expand All @@ -97,6 +101,29 @@ async function startExplorer() {
}
}

async function startStoreIndexer() {
if (chainId !== anvil.id) {
console.log("Skipping SQLite indexer for non-anvil chain ID", chainId);
return;
}

await rm(indexerDatabasePath, { recursive: true, force: true });

console.log("Running SQLite indexer for anvil...");
indexerProcess = spawn("sh", ["node_modules/.bin/sqlite-indexer"], {
cwd: packageRoot,
stdio: "inherit",
env: {
...process.env,
DEBUG: "mud:*",
RPC_HTTP_URL: "http://127.0.0.1:8545",
FOLLOW_BLOCK_TAG: "latest",
SQLITE_FILENAME: indexerDatabase,
STORE_ADDRESS: worldAddress,
},
});
}

async function readWorldsJson() {
try {
const data = await readFile(worldsFile, "utf8");
Expand All @@ -117,9 +144,10 @@ async function readWorldsJson() {
}

async function restartExplorer() {
if (explorerProcess) {
explorerProcess.kill();
}
indexerProcess?.kill();
explorerProcess?.kill();

await startStoreIndexer();
await startExplorer();
}

Expand All @@ -139,11 +167,9 @@ function watchWorldsJson() {
});
}

process.on("SIGINT", () => {
if (explorerProcess) {
explorerProcess.kill();
}
process.exit();
process.on("exit", () => {
indexerProcess?.kill();
explorerProcess?.kill();
});

async function main() {
Expand All @@ -162,6 +188,7 @@ async function main() {
watchWorldsJson();
}

await startStoreIndexer();
await startExplorer();
}

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions templates/phaser/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ procs:
anvil:
cwd: packages/contracts
shell: anvil --base-fee 0 --block-time 2
indexer:
cwd: packages/contracts
shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
env:
DEBUG: mud:*
RPC_HTTP_URL: "http://127.0.0.1:8545"
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
explorer:
cwd: packages/contracts
shell: pnpm explorer
8 changes: 0 additions & 8 deletions templates/react-ecs/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ procs:
anvil:
cwd: packages/contracts
shell: anvil --base-fee 0 --block-time 2
indexer:
cwd: packages/contracts
shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
env:
DEBUG: mud:*
RPC_HTTP_URL: "http://127.0.0.1:8545"
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
explorer:
cwd: packages/contracts
shell: pnpm explorer
8 changes: 0 additions & 8 deletions templates/react/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ procs:
anvil:
cwd: packages/contracts
shell: anvil --base-fee 0 --block-time 2
indexer:
cwd: packages/contracts
shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
env:
DEBUG: mud:*
RPC_HTTP_URL: "http://127.0.0.1:8545"
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
explorer:
cwd: packages/contracts
shell: pnpm explorer
8 changes: 0 additions & 8 deletions templates/threejs/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ procs:
anvil:
cwd: packages/contracts
shell: anvil --base-fee 0 --block-time 2
indexer:
cwd: packages/contracts
shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
env:
DEBUG: mud:*
RPC_HTTP_URL: "http://127.0.0.1:8545"
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
explorer:
cwd: packages/contracts
shell: pnpm explorer
8 changes: 0 additions & 8 deletions templates/vanilla/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ procs:
anvil:
cwd: packages/contracts
shell: anvil --base-fee 0 --block-time 2
indexer:
cwd: packages/contracts
shell: shx rm -rf $SQLITE_FILENAME && pnpm sqlite-indexer
env:
DEBUG: mud:*
RPC_HTTP_URL: "http://127.0.0.1:8545"
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
explorer:
cwd: packages/contracts
shell: pnpm explorer

0 comments on commit 95aa3bb

Please sign in to comment.