From 2da9e48cd4bb8e3dafecf6c37799929b7bbbc39d Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 23 Jul 2024 09:59:05 +0100 Subject: [PATCH] refactor(cli): use config namespaces for tables (#2965) --- .changeset/nine-books-reflect.md | 5 ++++ packages/cli/src/commands/trace.ts | 2 +- packages/cli/src/deploy/common.ts | 5 ---- .../cli/src/deploy/ensureNamespaceOwner.ts | 5 ++-- packages/cli/src/deploy/getFunctions.ts | 29 +++++++++++++------ packages/cli/src/deploy/getResourceAccess.ts | 17 ++++++++--- packages/cli/src/deploy/getResourceIds.ts | 5 ++-- packages/cli/src/deploy/getSystems.ts | 5 ++-- packages/cli/src/deploy/getTables.ts | 12 +++++--- packages/cli/src/runDeploy.ts | 4 ++- packages/store/ts/codegen/tableOptions.ts | 3 +- ...ist-tables-from-store-and-world-configs.ts | 5 +++- 12 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 .changeset/nine-books-reflect.md diff --git a/.changeset/nine-books-reflect.md b/.changeset/nine-books-reflect.md new file mode 100644 index 0000000000..0805011a91 --- /dev/null +++ b/.changeset/nine-books-reflect.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/cli": patch +--- + +Refactored CLI commands to use tables from config namespaces output. This is a precursor for supporting multiple namespaces. diff --git a/packages/cli/src/commands/trace.ts b/packages/cli/src/commands/trace.ts index 6117bc2f88..2e1366fcbf 100644 --- a/packages/cli/src/commands/trace.ts +++ b/packages/cli/src/commands/trace.ts @@ -11,7 +11,7 @@ import { World as WorldConfig } from "@latticexyz/world"; import { resolveSystems } from "@latticexyz/world/node"; import { worldAbi } from "../deploy/common"; -const systemsTableId = worldConfig.tables.world__Systems.tableId; +const systemsTableId = worldConfig.namespaces.world.tables.Systems.tableId; function getWorldAddress(worldsFile: string, chainId: number): Hex { if (!fs.existsSync(worldsFile)) { diff --git a/packages/cli/src/deploy/common.ts b/packages/cli/src/deploy/common.ts index b1d38daefe..4714d7a934 100644 --- a/packages/cli/src/deploy/common.ts +++ b/packages/cli/src/deploy/common.ts @@ -1,6 +1,4 @@ import { Abi, Address, Hex, padHex } from "viem"; -import storeConfig from "@latticexyz/store/mud.config"; -import worldConfig from "@latticexyz/world/mud.config"; import IBaseWorldAbi from "@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json" assert { type: "json" }; import { helloStoreEvent } from "@latticexyz/store"; import { helloWorldEvent } from "@latticexyz/world"; @@ -10,9 +8,6 @@ export const salt = padHex("0x", { size: 32 }); // https://eips.ethereum.org/EIPS/eip-170 export const contractSizeLimit = parseInt("6000", 16); -export const storeTables = storeConfig.tables; -export const worldTables = worldConfig.tables; - export const worldDeployEvents = [helloStoreEvent, helloWorldEvent] as const; export const worldAbi = IBaseWorldAbi; diff --git a/packages/cli/src/deploy/ensureNamespaceOwner.ts b/packages/cli/src/deploy/ensureNamespaceOwner.ts index b90acc1f0f..cd9a1aa267 100644 --- a/packages/cli/src/deploy/ensureNamespaceOwner.ts +++ b/packages/cli/src/deploy/ensureNamespaceOwner.ts @@ -1,9 +1,10 @@ import { Account, Chain, Client, Hex, Transport, getAddress } from "viem"; -import { WorldDeploy, worldAbi, worldTables } from "./common"; +import { WorldDeploy, worldAbi } from "./common"; import { hexToResource, resourceToHex, writeContract } from "@latticexyz/common"; import { getResourceIds } from "./getResourceIds"; import { getTableValue } from "./getTableValue"; import { debug } from "./debug"; +import worldConfig from "@latticexyz/world/mud.config"; export async function ensureNamespaceOwner({ client, @@ -35,7 +36,7 @@ export async function ensureNamespaceOwner({ const { owner } = await getTableValue({ client, worldDeploy, - table: worldTables.world__NamespaceOwner, + table: worldConfig.namespaces.world.tables.NamespaceOwner, key: { namespaceId: resourceToHex({ type: "namespace", namespace, name: "" }) }, }); return [namespace, owner]; diff --git a/packages/cli/src/deploy/getFunctions.ts b/packages/cli/src/deploy/getFunctions.ts index 5d24a0a3f2..19993a7cb1 100644 --- a/packages/cli/src/deploy/getFunctions.ts +++ b/packages/cli/src/deploy/getFunctions.ts @@ -1,5 +1,5 @@ import { Client, parseAbiItem } from "viem"; -import { WorldDeploy, WorldFunction, worldTables } from "./common"; +import { WorldDeploy, WorldFunction } from "./common"; import { debug } from "./debug"; import { storeSetRecordEvent } from "@latticexyz/store"; import { getLogs } from "viem/actions"; @@ -10,6 +10,7 @@ import { getSchemaTypes, getValueSchema, } from "@latticexyz/protocol-parser/internal"; +import worldConfig from "@latticexyz/world/mud.config"; export async function getFunctions({ client, @@ -26,13 +27,19 @@ export async function getFunctions({ toBlock: worldDeploy.stateBlock, address: worldDeploy.address, event: parseAbiItem(storeSetRecordEvent), - args: { tableId: worldTables.world__FunctionSelectors.tableId }, + args: { tableId: worldConfig.namespaces.world.tables.FunctionSelectors.tableId }, }); const selectors = selectorLogs.map((log) => { return { - ...decodeValueArgs(getSchemaTypes(getValueSchema(worldTables.world__FunctionSelectors)), log.args), - ...decodeKey(getSchemaTypes(getKeySchema(worldTables.world__FunctionSelectors)), log.args.keyTuple), + ...decodeValueArgs( + getSchemaTypes(getValueSchema(worldConfig.namespaces.world.tables.FunctionSelectors)), + log.args, + ), + ...decodeKey( + getSchemaTypes(getKeySchema(worldConfig.namespaces.world.tables.FunctionSelectors)), + log.args.keyTuple, + ), }; }); debug("found", selectors.length, "function selectors for", worldDeploy.address); @@ -45,16 +52,20 @@ export async function getFunctions({ toBlock: worldDeploy.stateBlock, address: worldDeploy.address, event: parseAbiItem(storeSetRecordEvent), - args: { tableId: worldTables.world__FunctionSignatures.tableId }, + args: { tableId: worldConfig.namespaces.world.tables.FunctionSignatures.tableId }, }); const selectorToSignature = Object.fromEntries( signatureLogs.map((log) => { return [ - decodeKey(getSchemaTypes(getKeySchema(worldTables.world__FunctionSignatures)), log.args.keyTuple) - .functionSelector, - decodeValueArgs(getSchemaTypes(getValueSchema(worldTables.world__FunctionSignatures)), log.args) - .functionSignature, + decodeKey( + getSchemaTypes(getKeySchema(worldConfig.namespaces.world.tables.FunctionSignatures)), + log.args.keyTuple, + ).functionSelector, + decodeValueArgs( + getSchemaTypes(getValueSchema(worldConfig.namespaces.world.tables.FunctionSignatures)), + log.args, + ).functionSignature, ]; }), ); diff --git a/packages/cli/src/deploy/getResourceAccess.ts b/packages/cli/src/deploy/getResourceAccess.ts index e35a50c9d2..825ee4351c 100644 --- a/packages/cli/src/deploy/getResourceAccess.ts +++ b/packages/cli/src/deploy/getResourceAccess.ts @@ -1,10 +1,11 @@ import { Client, parseAbiItem, Hex, Address, getAddress } from "viem"; -import { WorldDeploy, worldTables } from "./common"; +import { WorldDeploy } from "./common"; import { debug } from "./debug"; import { storeSpliceStaticDataEvent } from "@latticexyz/store"; import { getLogs } from "viem/actions"; import { decodeKey, getKeySchema, getSchemaTypes } from "@latticexyz/protocol-parser/internal"; import { getTableValue } from "./getTableValue"; +import worldConfig from "@latticexyz/world/mud.config"; export async function getResourceAccess({ client, @@ -26,18 +27,26 @@ export async function getResourceAccess({ // our usage of `ResourceAccess._set(...)` emits a splice instead of set record // TODO: https://github.com/latticexyz/mud/issues/479 event: parseAbiItem(storeSpliceStaticDataEvent), - args: { tableId: worldTables.world__ResourceAccess.tableId }, + args: { tableId: worldConfig.namespaces.world.tables.ResourceAccess.tableId }, }); const keys = logs.map((log) => - decodeKey(getSchemaTypes(getKeySchema(worldTables.world__ResourceAccess)), log.args.keyTuple), + decodeKey(getSchemaTypes(getKeySchema(worldConfig.namespaces.world.tables.ResourceAccess)), log.args.keyTuple), ); const access = ( await Promise.all( keys.map( async (key) => - [key, await getTableValue({ client, worldDeploy, table: worldTables.world__ResourceAccess, key })] as const, + [ + key, + await getTableValue({ + client, + worldDeploy, + table: worldConfig.namespaces.world.tables.ResourceAccess, + key, + }), + ] as const, ), ) ) diff --git a/packages/cli/src/deploy/getResourceIds.ts b/packages/cli/src/deploy/getResourceIds.ts index d019b25d47..4286d671b2 100644 --- a/packages/cli/src/deploy/getResourceIds.ts +++ b/packages/cli/src/deploy/getResourceIds.ts @@ -1,9 +1,10 @@ import { Client, parseAbiItem, Hex, HttpRequestError } from "viem"; import { getLogs } from "viem/actions"; import { storeSpliceStaticDataEvent } from "@latticexyz/store"; -import { WorldDeploy, storeTables } from "./common"; +import { WorldDeploy } from "./common"; import { debug } from "./debug"; import pRetry from "p-retry"; +import storeConfig from "@latticexyz/store/mud.config"; export async function getResourceIds({ client, @@ -24,7 +25,7 @@ export async function getResourceIds({ fromBlock: worldDeploy.deployBlock, toBlock: worldDeploy.stateBlock, event: parseAbiItem(storeSpliceStaticDataEvent), - args: { tableId: storeTables.store__ResourceIds.tableId }, + args: { tableId: storeConfig.namespaces.store.tables.ResourceIds.tableId }, }), { retries: 3, diff --git a/packages/cli/src/deploy/getSystems.ts b/packages/cli/src/deploy/getSystems.ts index 43454d16e9..07b14974a7 100644 --- a/packages/cli/src/deploy/getSystems.ts +++ b/packages/cli/src/deploy/getSystems.ts @@ -1,4 +1,4 @@ -import { DeployedSystem, WorldDeploy, worldTables } from "./common"; +import { DeployedSystem, WorldDeploy } from "./common"; import { Client } from "viem"; import { getResourceIds } from "./getResourceIds"; import { hexToResource, resourceToLabel } from "@latticexyz/common"; @@ -6,6 +6,7 @@ import { getTableValue } from "./getTableValue"; import { debug } from "./debug"; import { getFunctions } from "./getFunctions"; import { getResourceAccess } from "./getResourceAccess"; +import worldConfig from "@latticexyz/world/mud.config"; export async function getSystems({ client, @@ -27,7 +28,7 @@ export async function getSystems({ const { system: address, publicAccess } = await getTableValue({ client, worldDeploy, - table: worldTables.world__Systems, + table: worldConfig.namespaces.world.tables.Systems, key: { systemId: system.resourceId }, }); const systemFunctions = functions.filter((func) => func.systemId === system.resourceId); diff --git a/packages/cli/src/deploy/getTables.ts b/packages/cli/src/deploy/getTables.ts index 259f95d07d..27d56f3449 100644 --- a/packages/cli/src/deploy/getTables.ts +++ b/packages/cli/src/deploy/getTables.ts @@ -1,6 +1,6 @@ import { Client, parseAbiItem, decodeAbiParameters, parseAbiParameters } from "viem"; import { hexToResource } from "@latticexyz/common"; -import { WorldDeploy, storeTables } from "./common"; +import { WorldDeploy } from "./common"; import { debug } from "./debug"; import { storeSetRecordEvent } from "@latticexyz/store"; import { getLogs } from "viem/actions"; @@ -13,6 +13,7 @@ import { hexToSchema, } from "@latticexyz/protocol-parser/internal"; import { Schema, Table } from "@latticexyz/config"; +import storeConfig from "@latticexyz/store/mud.config"; // TODO: add label once we register it onchain type DeployedTable = Omit; @@ -37,14 +38,17 @@ export async function getTables({ toBlock: worldDeploy.stateBlock, address: worldDeploy.address, event: parseAbiItem(storeSetRecordEvent), - args: { tableId: storeTables.store__Tables.tableId }, + args: { tableId: storeConfig.namespaces.store.tables.Tables.tableId }, }); // TODO: combine with store-sync logToTable and export from somewhere const tables = logs.map((log): DeployedTable => { - const { tableId } = decodeKey(getSchemaTypes(getKeySchema(storeTables.store__Tables)), log.args.keyTuple); + const { tableId } = decodeKey( + getSchemaTypes(getKeySchema(storeConfig.namespaces.store.tables.Tables)), + log.args.keyTuple, + ); const { type, namespace, name } = hexToResource(tableId); - const value = decodeValueArgs(getSchemaTypes(getValueSchema(storeTables.store__Tables)), log.args); + const value = decodeValueArgs(getSchemaTypes(getValueSchema(storeConfig.namespaces.store.tables.Tables)), log.args); const solidityKeySchema = hexToSchema(value.keySchema); const solidityValueSchema = hexToSchema(value.valueSchema); diff --git a/packages/cli/src/runDeploy.ts b/packages/cli/src/runDeploy.ts index 9ef63357c9..1bf116e10e 100644 --- a/packages/cli/src/runDeploy.ts +++ b/packages/cli/src/runDeploy.ts @@ -129,13 +129,15 @@ export async function runDeploy(opts: DeployOptions): Promise { console.log("Deploying from", client.account.address); + const tables = Object.values(config.namespaces).flatMap((namespace) => Object.values(namespace.tables)); + const startTime = Date.now(); const worldDeploy = await deploy({ deployerAddress: opts.deployerAddress as Hex | undefined, salt, worldAddress: opts.worldAddress as Hex | undefined, client, - tables: Object.values(config.tables), + tables, systems, libraries, modules, diff --git a/packages/store/ts/codegen/tableOptions.ts b/packages/store/ts/codegen/tableOptions.ts index d35bb38f20..d0959361c1 100644 --- a/packages/store/ts/codegen/tableOptions.ts +++ b/packages/store/ts/codegen/tableOptions.ts @@ -29,7 +29,8 @@ export function getTableOptions( config: StoreConfig, solidityUserTypes: Record, ): TableOptions[] { - const options = Object.values(config.tables).map((table): TableOptions => { + const tables = Object.values(config.namespaces).flatMap((namespace) => Object.values(namespace.tables)); + const options = tables.map((table): TableOptions => { const keySchema = getKeySchema(table); const valueSchema = getValueSchema(table); diff --git a/packages/world/ts/scripts/list-tables-from-store-and-world-configs.ts b/packages/world/ts/scripts/list-tables-from-store-and-world-configs.ts index 6b0a8e19d7..128d7fc39f 100644 --- a/packages/world/ts/scripts/list-tables-from-store-and-world-configs.ts +++ b/packages/world/ts/scripts/list-tables-from-store-and-world-configs.ts @@ -1,9 +1,12 @@ import storeConfig from "@latticexyz/store/mud.config"; import worldConfig from "../../mud.config"; +const storeTables = Object.values(storeConfig.namespaces).flatMap((namespace) => Object.values(namespace.tables)); +const worldTables = Object.values(worldConfig.namespaces).flatMap((namespace) => Object.values(namespace.tables)); + console.log( JSON.stringify( - [...Object.values(storeConfig.tables), ...Object.values(worldConfig.tables)] + [...storeTables, ...worldTables] // Skip generic tables .filter((table) => !table.codegen.tableIdArgument) .map((table) => ({ name: table.name, id: table.tableId })),