Skip to content

Commit

Permalink
refactor(cli): use config namespaces for tables (#2965)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Jul 23, 2024
1 parent 6189540 commit 2da9e48
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-books-reflect.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/cli/src/commands/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
5 changes: 0 additions & 5 deletions packages/cli/src/deploy/common.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/deploy/ensureNamespaceOwner.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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];
Expand Down
29 changes: 20 additions & 9 deletions packages/cli/src/deploy/getFunctions.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,6 +10,7 @@ import {
getSchemaTypes,
getValueSchema,
} from "@latticexyz/protocol-parser/internal";
import worldConfig from "@latticexyz/world/mud.config";

export async function getFunctions({
client,
Expand All @@ -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);
Expand All @@ -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,
];
}),
);
Expand Down
17 changes: 13 additions & 4 deletions packages/cli/src/deploy/getResourceAccess.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
),
)
)
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/deploy/getResourceIds.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/deploy/getSystems.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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";
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,
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/deploy/getTables.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<Table, "label">;
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/runDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {

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,
Expand Down
3 changes: 2 additions & 1 deletion packages/store/ts/codegen/tableOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function getTableOptions(
config: StoreConfig,
solidityUserTypes: Record<string, SolidityUserDefinedType>,
): 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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 })),
Expand Down

0 comments on commit 2da9e48

Please sign in to comment.