From 95aa3bb07df284a374e982ccea53d24df4d61219 Mon Sep 17 00:00:00 2001 From: Karolis Ramanauskas Date: Wed, 25 Sep 2024 16:04:41 +0300 Subject: [PATCH] feat(explorer): local indexer inside explorer (#3229) Co-authored-by: Kevin Ingersoll --- .changeset/silent-hairs-heal.md | 19 +++++++++++ examples/local-explorer/mprocs.yaml | 8 ----- packages/explorer/package.json | 1 + packages/explorer/src/bin/explorer.ts | 47 +++++++++++++++++++++------ pnpm-lock.yaml | 3 ++ templates/phaser/mprocs.yaml | 8 ----- templates/react-ecs/mprocs.yaml | 8 ----- templates/react/mprocs.yaml | 8 ----- templates/threejs/mprocs.yaml | 8 ----- templates/vanilla/mprocs.yaml | 8 ----- 10 files changed, 60 insertions(+), 58 deletions(-) create mode 100644 .changeset/silent-hairs-heal.md diff --git a/.changeset/silent-hairs-heal.md b/.changeset/silent-hairs-heal.md new file mode 100644 index 0000000000..acb192bd21 --- /dev/null +++ b/.changeset/silent-hairs-heal.md @@ -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" +``` diff --git a/examples/local-explorer/mprocs.yaml b/examples/local-explorer/mprocs.yaml index d9e4b7ba0c..c3a8d4d594 100644 --- a/examples/local-explorer/mprocs.yaml +++ b/examples/local-explorer/mprocs.yaml @@ -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 diff --git a/packages/explorer/package.json b/packages/explorer/package.json index 5f2d5fd3fa..80e7d1a151 100644 --- a/packages/explorer/package.json +++ b/packages/explorer/package.json @@ -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", diff --git a/packages/explorer/src/bin/explorer.ts b/packages/explorer/src/bin/explorer.ts index 6e5c551162..2eb358dd94 100755 --- a/packages/explorer/src/bin/explorer.ts +++ b/packages/explorer/src/bin/explorer.ts @@ -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"; @@ -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) { @@ -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"); @@ -117,9 +144,10 @@ async function readWorldsJson() { } async function restartExplorer() { - if (explorerProcess) { - explorerProcess.kill(); - } + indexerProcess?.kill(); + explorerProcess?.kill(); + + await startStoreIndexer(); await startExplorer(); } @@ -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() { @@ -162,6 +188,7 @@ async function main() { watchWorldsJson(); } + await startStoreIndexer(); await startExplorer(); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f41c62f5a1..571ea0d785 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -465,6 +465,9 @@ importers: '@latticexyz/store': specifier: workspace:* version: link:../store + '@latticexyz/store-indexer': + specifier: workspace:* + version: link:../store-indexer '@latticexyz/store-sync': specifier: workspace:* version: link:../store-sync diff --git a/templates/phaser/mprocs.yaml b/templates/phaser/mprocs.yaml index 3e2fada672..fa8c42c046 100644 --- a/templates/phaser/mprocs.yaml +++ b/templates/phaser/mprocs.yaml @@ -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 diff --git a/templates/react-ecs/mprocs.yaml b/templates/react-ecs/mprocs.yaml index 3e2fada672..fa8c42c046 100644 --- a/templates/react-ecs/mprocs.yaml +++ b/templates/react-ecs/mprocs.yaml @@ -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 diff --git a/templates/react/mprocs.yaml b/templates/react/mprocs.yaml index 3e2fada672..fa8c42c046 100644 --- a/templates/react/mprocs.yaml +++ b/templates/react/mprocs.yaml @@ -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 diff --git a/templates/threejs/mprocs.yaml b/templates/threejs/mprocs.yaml index 3e2fada672..fa8c42c046 100644 --- a/templates/threejs/mprocs.yaml +++ b/templates/threejs/mprocs.yaml @@ -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 diff --git a/templates/vanilla/mprocs.yaml b/templates/vanilla/mprocs.yaml index 3e2fada672..fa8c42c046 100644 --- a/templates/vanilla/mprocs.yaml +++ b/templates/vanilla/mprocs.yaml @@ -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