-
Notifications
You must be signed in to change notification settings - Fork 189
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
Changes from 5 commits
7e3509d
819dbfa
eea6e64
2a136d2
636e4e1
c8face9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { CommandModule, InferredOptionTypes } from "yargs"; | ||
import { loadConfig } from "@latticexyz/config/node"; | ||
import { World as WorldConfig } from "@latticexyz/world"; | ||
import { getRpcUrl } from "@latticexyz/common/foundry"; | ||
import { Hex, createWalletClient, http } from "viem"; | ||
import { getWorldDeploy } from "../deploy/getWorldDeploy"; | ||
import { writeDeploymentResult } from "../utils/writeDeploymentResult"; | ||
|
||
const verifyOptions = { | ||
configPath: { type: "string", desc: "Path to the MUD config file" }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the expected behavior if you already have a config? I thought the goal of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds like this is mostly just setting up the scaffolding for the either way, we should prob remove this config option since we're expecting to generate this from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, the config will ultimately be generated as part of this! |
||
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: "init", | ||
|
||
describe: "Populates the local project with the MUD artefacts for a given World.", | ||
|
||
builder(yargs) { | ||
return yargs.options(verifyOptions); | ||
}, | ||
|
||
async handler(opts) { | ||
const profile = opts.profile ?? process.env.FOUNDRY_PROFILE; | ||
|
||
const config = (await loadConfig(opts.configPath)) as WorldConfig; | ||
|
||
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), | ||
}; | ||
|
||
writeDeploymentResult({ client, config, deploymentInfo }); | ||
}, | ||
}; | ||
|
||
export default commandModule; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { World as WorldConfig } from "@latticexyz/world"; | ||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; | ||
import path from "path"; | ||
import { WalletClient } from "viem"; | ||
import { getChainId } from "viem/actions"; | ||
import chalk from "chalk"; | ||
|
||
const localChains = [1337, 31337]; | ||
|
||
export async function writeDeploymentResult({ | ||
client, | ||
config, | ||
deploymentInfo, | ||
}: { | ||
client: WalletClient; | ||
config: WorldConfig; | ||
deploymentInfo: { | ||
worldAddress: `0x${string}`; | ||
blockNumber: number; | ||
}; | ||
}) { | ||
const chainId = await getChainId(client); | ||
const deploysDir = path.join(config.deploy.deploysDirectory, chainId.toString()); | ||
mkdirSync(deploysDir, { recursive: true }); | ||
writeFileSync(path.join(deploysDir, "latest.json"), JSON.stringify(deploymentInfo, null, 2)); | ||
writeFileSync(path.join(deploysDir, Date.now() + ".json"), JSON.stringify(deploymentInfo, null, 2)); | ||
|
||
const deploys = existsSync(config.deploy.worldsFile) | ||
? JSON.parse(readFileSync(config.deploy.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.deploy.worldsFile, JSON.stringify(deploys, null, 2)); | ||
|
||
console.log( | ||
chalk.bgGreen( | ||
chalk.whiteBright(`\n Deployment result (written to ${config.deploy.worldsFile} and ${deploysDir}): \n`), | ||
), | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prob need a changeset for this (and in a future PR, can edit/expand the changeset to include new behavior)