Skip to content

Commit

Permalink
add script to fetch worlds.json config
Browse files Browse the repository at this point in the history
  • Loading branch information
karooolis committed Aug 14, 2024
1 parent 63c0c87 commit 7f8cb2c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/local-explorer/mprocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ procs:
FOLLOW_BLOCK_TAG: "latest"
SQLITE_FILENAME: "indexer.db"
explorer-watcher:
shell: bash explorer-watcher.sh
shell: node explorer-watcher.mjs
env:
PORT: "13690"
MODE: "development"
2 changes: 1 addition & 1 deletion examples/local-explorer/packages/contracts/worlds.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"31337": {
"address": "0x8d8b6b8414e1e3dcfd4168561b9be6bd3bf6ec4b"
}
}
}
11 changes: 10 additions & 1 deletion packages/explorer/src/app/utils/server/getWorldAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { Hex, isAddress } from "viem";

export function getWorldAddress(): Hex {
const headersList = headers();
const worldAddress = headersList.get("x-world-address") || process.env.NEXT_PUBLIC_WORLD_ADDRESS;

let worldAddress = headersList.get("x-world-address") || process.env.NEXT_PUBLIC_WORLD_ADDRESS;
if (!worldAddress) {
const worldsConfig = process.env.NEXT_PUBLIC_WORLDS_CONFIG;
if (worldsConfig) {
const chainId = headersList.get("x-chain-id") || process.env.NEXT_PUBLIC_CHAIN_ID || 31337;
const worlds = JSON.parse(worldsConfig);
worldAddress = worlds[chainId]?.address;
}
}

if (!worldAddress) {
throw new Error("World address not found");
Expand Down
11 changes: 10 additions & 1 deletion packages/explorer/src/hooks/useWorldAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { Hex, isAddress } from "viem";

export function useWorldAddress(): Hex {
const searchParams = useSearchParams();
const worldAddress = searchParams.get("worldAddress") || process.env.NEXT_PUBLIC_WORLD_ADDRESS;

let worldAddress = searchParams.get("worldAddress") || process.env.NEXT_PUBLIC_WORLD_ADDRESS;
if (!worldAddress) {
const worldsConfig = process.env.NEXT_PUBLIC_WORLDS_CONFIG;
if (worldsConfig) {
const chainId = searchParams.get("chainId") || process.env.NEXT_PUBLIC_CHAIN_ID || 31337;
const worlds = JSON.parse(worldsConfig);
worldAddress = worlds[chainId]?.address;
}
}

if (!worldAddress) {
throw new Error("World address not found");
Expand Down
12 changes: 10 additions & 2 deletions packages/explorer/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export function middleware(request: NextRequest) {
const worldAddress = request.nextUrl.searchParams.get("worldAddress") || process.env.NEXT_PUBLIC_WORLD_ADDRESS;
const chainId = request.nextUrl.searchParams.get("chainId");
const worldAddress = request.nextUrl.searchParams.get("worldAddress");
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-world-address", worldAddress as string);

if (chainId) {
requestHeaders.set("x-chain-id", chainId);
}

if (worldAddress) {
requestHeaders.set("x-world-address", worldAddress as string);
}

return NextResponse.next({
request: {
Expand Down

0 comments on commit 7f8cb2c

Please sign in to comment.