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

fix(cli): handle module already installed #1769

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion packages/cli/src/deploy/common.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import { Abi, Address, Hex, padHex } from "viem";
import storeConfig from "@latticexyz/store/mud.config.js";
import worldConfig from "@latticexyz/world/mud.config.js";
import IBaseWorldAbi from "@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json" assert { type: "json" };
import IModuleAbi from "@latticexyz/world-modules/out/IModule.sol/IModule.abi.json" assert { type: "json" };
import { Tables, configToTables } from "./configToTables";
import { StoreConfig, helloStoreEvent } from "@latticexyz/store";
import { WorldConfig, helloWorldEvent } from "@latticexyz/world";
@@ -14,7 +15,7 @@ export const worldTables = configToTables(worldConfig);

export const worldDeployEvents = [helloStoreEvent, helloWorldEvent] as const;

export const worldAbi = IBaseWorldAbi;
export const worldAbi = [...IBaseWorldAbi, ...IModuleAbi] as const;

export type WorldDeploy = {
readonly address: Address;
88 changes: 43 additions & 45 deletions packages/cli/src/deploy/ensureModules.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Client, Transport, Chain, Account, Hex } from "viem";
import { Client, Transport, Chain, Account, Hex, BaseError } from "viem";
import { writeContract } from "@latticexyz/common";
import { Module, WorldDeploy, worldAbi } from "./common";
import { debug } from "./debug";
import { wait } from "@latticexyz/common/utils";
import { isDefined, wait } from "@latticexyz/common/utils";
import pRetry from "p-retry";

export async function ensureModules({
@@ -17,49 +17,47 @@ export async function ensureModules({
if (!modules.length) return [];

debug("installing modules:", modules.map((mod) => mod.name).join(", "));
const installTxs = await Promise.all(
modules.map((mod) =>
mod.installAsRoot
? pRetry(
() =>
writeContract(client, {
chain: client.chain ?? null,
address: worldDeploy.address,
abi: worldAbi,
// TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645)
functionName: "installRootModule",
args: [mod.address, mod.installData],
}),
{
retries: 3,
onFailedAttempt: async (error) => {
const delay = error.attemptNumber * 500;
debug(`failed to install root module ${mod.name}, retrying in ${delay}ms...`);
await wait(delay);
},
return (
await Promise.all(
modules.map((mod) =>
pRetry(
async () => {
try {
return mod.installAsRoot
? await writeContract(client, {
chain: client.chain ?? null,
address: worldDeploy.address,
abi: worldAbi,
// TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645)
functionName: "installRootModule",
args: [mod.address, mod.installData],
})
: await writeContract(client, {
chain: client.chain ?? null,
address: worldDeploy.address,
abi: worldAbi,
// TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645)
functionName: "installModule",
args: [mod.address, mod.installData],
});
} catch (error) {
if (error instanceof BaseError && error.message.includes("Module_AlreadyInstalled")) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to put this in onFailedAttempt but there's no way to signal to p-retry that we should stop retrying and continue.

debug(`module ${mod.name} already installed`);
return;
}
throw error;
}
)
: pRetry(
() =>
writeContract(client, {
chain: client.chain ?? null,
address: worldDeploy.address,
abi: worldAbi,
// TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645)
functionName: "installModule",
args: [mod.address, mod.installData],
}),
{
retries: 3,
onFailedAttempt: async (error) => {
const delay = error.attemptNumber * 500;
debug(`failed to install module ${mod.name}, retrying in ${delay}ms...`);
await wait(delay);
},
}
)
},
{
retries: 3,
onFailedAttempt: async (error) => {
const delay = error.attemptNumber * 500;
debug(`failed to install module ${mod.name}, retrying in ${delay}ms...`);
await wait(delay);
},
}
)
)
)
);

return await Promise.all(installTxs);
).filter(isDefined);
}