Skip to content

Commit

Permalink
init custom world
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Sep 8, 2024
1 parent e81680a commit 37cc822
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/custom-world/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pnpm-lock.yaml

# Ignore MUD deploy artifacts
deploys/**/*.json
worlds.json
5 changes: 0 additions & 5 deletions examples/custom-world/worlds.json

This file was deleted.

30 changes: 26 additions & 4 deletions packages/cli/src/deploy/deployCustomWorld.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Account, Chain, Client, Hex, Transport, concatHex, encodeDeployData, getCreate2Address, isHex } from "viem";
import { waitForTransactionReceipt } from "viem/actions";
import { sendTransaction } from "@latticexyz/common";
import { resourceToHex, sendTransaction, writeContract } from "@latticexyz/common";
import { debug } from "./debug";
import { logsToWorldDeploy } from "./logsToWorldDeploy";
import { WorldDeploy, salt } from "./common";
import { WorldDeploy, salt, worldAbi } from "./common";
import { getWorldContracts } from "./getWorldContracts";
import { ensureContractsDeployed } from "./ensureContractsDeployed";
import { ContractArtifact, ReferenceIdentifier } from "@latticexyz/world/node";
import { World } from "@latticexyz/world";
import { waitForTransactions } from "./waitForTransactions";

function findArtifact(ref: ReferenceIdentifier, artifacts: readonly ContractArtifact[]): ContractArtifact {
const artifact = artifacts.find((a) => a.sourcePath === ref.sourcePath && a.name === ref.name);
Expand Down Expand Up @@ -76,7 +77,7 @@ export async function deployCustomWorld({

// Deploy custom world without deterministic deployer for now
debug("deploying custom world");
const tx = await sendTransaction(client, {
const deployTx = await sendTransaction(client, {
chain: client.chain ?? null,
data: encodeDeployData({
abi: worldArtifact.abi,
Expand All @@ -86,7 +87,7 @@ export async function deployCustomWorld({
});

debug("waiting for custom world deploy");
const receipt = await waitForTransactionReceipt(client, { hash: tx });
const receipt = await waitForTransactionReceipt(client, { hash: deployTx });
if (receipt.status !== "success") {
console.error("world deploy failed", receipt);
throw new Error("world deploy failed");
Expand All @@ -95,5 +96,26 @@ export async function deployCustomWorld({
const deploy = logsToWorldDeploy(receipt.logs);
debug("deployed custom world to", deploy.address, "at block", deploy.deployBlock);

const initTxs = await Promise.all([
// initialize world via init module
writeContract(client, {
chain: client.chain ?? null,
address: deploy.address,
abi: worldAbi,
functionName: "initialize",
args: [contracts.InitModule.address],
}),
// transfer root namespace to deployer
writeContract(client, {
chain: client.chain ?? null,
address: deploy.address,
abi: worldAbi,
functionName: "transferOwnership",
args: [resourceToHex({ type: "namespace", namespace: "", name: "" }), client.account.address],
}),
]);

await waitForTransactions({ client, hashes: initTxs, debugLabel: "world init" });

return { ...deploy, stateBlock: deploy.deployBlock };
}

0 comments on commit 37cc822

Please sign in to comment.