Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): generate mud config from world address #2854

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/cli/src/commands/generateConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { formatAndWriteTypescript } from "@latticexyz/common/codegen";
import { getRpcUrl } from "@latticexyz/common/foundry";
import { Hex, createWalletClient, http } from "viem";
import type { CommandModule, InferredOptionTypes } from "yargs";
import { getWorldDeploy } from "../deploy/getWorldDeploy";
import { getWorldInput } from "../utils/getWorldInput";

const generateConfigOptions = {
worldAddress: { type: "string", required: true, desc: "Verify an existing World at the given address" },
profile: { type: "string", desc: "The foundry profile to use" },
rpc: { type: "string", desc: "The RPC URL to use. Defaults to the RPC url from the local foundry.toml" },
} as const;

type Options = InferredOptionTypes<typeof generateConfigOptions>;

const commandModule: CommandModule<Options, Options> = {
command: "generate-config",

describe: "Generate a MUD config given a world address",

builder(yargs) {
return yargs.options(generateConfigOptions);
},

async handler(opts) {
const profile = opts.profile ?? process.env.FOUNDRY_PROFILE;

const rpc = opts.rpc ?? (await getRpcUrl(profile));

const client = createWalletClient({
transport: http(rpc),
});

const worldDeploy = await getWorldDeploy(client, opts.worldAddress as Hex);

const config = await getWorldInput({ client, worldDeploy });

formatAndWriteTypescript(
`
import { defineWorld } from "@latticexyz/world";

export default defineWorld(
${JSON.stringify(config)}
);
`,
"world.config.ts",
"generating MUD config from world",
);
},
};

export default commandModule;
2 changes: 2 additions & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import test from "./test";
import trace from "./trace";
import devContracts from "./dev-contracts";
import verify from "./verify";
import generateConfig from "./generateConfig";

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Each command has different options
export const commands: CommandModule<any, any>[] = [
Expand All @@ -30,4 +31,5 @@ export const commands: CommandModule<any, any>[] = [
devContracts,
abiTs,
verify,
generateConfig,
];
34 changes: 34 additions & 0 deletions packages/cli/src/utils/getWorldInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getTables } from "../deploy/getTables";
import { Table } from "../deploy/configToTables";
import { getSystems } from "../deploy/getSystems";
import { SystemsInput, WorldInput } from "@latticexyz/world/ts/config/v2/input";
import { TableInput } from "@latticexyz/store/config/v2";
import { Client } from "viem";
import { WorldDeploy } from "../deploy/common";

function tableToV2({ keySchema, valueSchema }: Table): Omit<TableInput, "namespace" | "name"> {
return {
schema: { ...keySchema, ...valueSchema },
key: Object.keys(keySchema),
};
}

export async function getWorldInput({
client,
worldDeploy,
}: {
readonly client: Client;
readonly worldDeploy: WorldDeploy;
}): Promise<WorldInput> {
const tables: {
[key: string]: Omit<TableInput, "namespace" | "name">;
} = {};
const worldTables = await getTables({ client, worldDeploy });
worldTables.forEach((table) => (tables[table.name] = tableToV2(table)));

const systems: SystemsInput = {};
const worldSystems = await getSystems({ client, worldDeploy });
worldSystems.forEach((system) => (systems[system.name] = { name: system.name, openAccess: system.allowAll }));

return { systems, tables };
}
Loading