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): mud init command #2853

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
66 changes: 66 additions & 0 deletions packages/cli/src/commands/generateWorlds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { CommandModule, InferredOptionTypes } from "yargs";
import { loadConfig } from "@latticexyz/config/node";
import { World as WorldConfig } from "@latticexyz/world";
import { worldToV1 } from "@latticexyz/world/config/v2";
import { getRpcUrl } from "@latticexyz/common/foundry";
import { Hex, createWalletClient, http } from "viem";
import chalk from "chalk";
import { getWorldDeploy } from "../deploy/getWorldDeploy";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { getChainId } from "viem/actions";
import { localChains } from "../runDeploy";

const verifyOptions = {
configPath: { type: "string", desc: "Path to the MUD config file" },
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 verifyOptions>;

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

describe: "Generate worlds.json from a given World address",

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

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

const configV2 = (await loadConfig(opts.configPath)) as WorldConfig;
const config = worldToV1(configV2);
if (opts.printConfig) {
console.log(chalk.green("\nResolved config:\n"), JSON.stringify(config, null, 2));
}

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

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

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

const deploymentInfo = {
worldAddress: worldDeploy.address,
blockNumber: Number(worldDeploy.deployBlock),
};

const chainId = await getChainId(client);

const deploys = existsSync(config.worldsFile) ? JSON.parse(readFileSync(config.worldsFile, "utf-8")) : {};
deploys[chainId] = {
address: deploymentInfo.worldAddress,
// We expect the worlds file to be committed and since local deployments are often
// a consistent address but different block number, we'll ignore the block number.
blockNumber: localChains.includes(chainId) ? undefined : deploymentInfo.blockNumber,
};
writeFileSync(config.worldsFile, JSON.stringify(deploys, null, 2));
},
};

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 generateWorlds from "./generateWorlds";

// 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,
generateWorlds,
];
3 changes: 2 additions & 1 deletion packages/cli/src/runDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { build } from "./build";
import { kmsKeyToAccount } from "@latticexyz/common/kms";
import { configToModules } from "./deploy/configToModules";

export const localChains = [1337, 31337];
yonadaaa marked this conversation as resolved.
Show resolved Hide resolved

export const deployOptions = {
configPath: { type: "string", desc: "Path to the MUD config file" },
printConfig: { type: "boolean", desc: "Print the resolved config" },
Expand Down Expand Up @@ -160,7 +162,6 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
writeFileSync(path.join(deploysDir, "latest.json"), JSON.stringify(deploymentInfo, null, 2));
writeFileSync(path.join(deploysDir, Date.now() + ".json"), JSON.stringify(deploymentInfo, null, 2));

const localChains = [1337, 31337];
const deploys = existsSync(config.worldsFile) ? JSON.parse(readFileSync(config.worldsFile, "utf-8")) : {};
deploys[chainId] = {
address: deploymentInfo.worldAddress,
Expand Down
Loading