Skip to content

Commit

Permalink
feat(cli): quieter automine (#3212)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Sep 20, 2024
1 parent a08ba5e commit 58f101e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-bulldogs-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/cli": patch
---

Reduced the log noise from enabling/disabling automine on non-Anvil chains.
4 changes: 2 additions & 2 deletions packages/cli/src/runDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
console.log("Deploying from", client.account.address);

// Attempt to enable automine for the duration of the deploy. Noop if automine is not available.
const { reset: resetMiningMode } = await enableAutomine(client);
const automine = await enableAutomine(client);

const startTime = Date.now();
const worldDeploy = await deploy({
Expand All @@ -165,7 +165,7 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {
}

// Reset mining mode after deploy
await resetMiningMode();
await automine?.reset();

console.log(chalk.green("Deployment completed in", (Date.now() - startTime) / 1000, "seconds"));

Expand Down
31 changes: 14 additions & 17 deletions packages/cli/src/utils/enableAutomine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAutomine, getBlock, setAutomine, setIntervalMining } from "viem/actions";
import { debug, error } from "../debug";
import { debug } from "../debug";
import { Client } from "viem";
import { getAction } from "viem/utils";

Expand All @@ -12,24 +12,21 @@ type MiningMode =
blockTime: number;
};

export type EnableAutomineResult = { reset: () => Promise<void> };
export type EnableAutomineResult = undefined | { reset: () => Promise<void> };

export async function enableAutomine(client: Client): Promise<EnableAutomineResult> {
try {
debug("Enabling automine");
const prevMiningMode = await getMiningMode(client);
await setMiningMode(client, { type: "automine" });
return {
reset: () => {
debug("Disabling automine");
return setMiningMode(client, prevMiningMode);
},
};
} catch (e) {
debug("Skipping automine");
error(e);
return { reset: async () => void 0 };
}
const miningMode = await getMiningMode(client).catch(() => undefined);
// doesn't support automine or is already in automine
if (!miningMode || miningMode.type === "automine") return;

debug("Enabling automine");
await setMiningMode(client, { type: "automine" });
return {
reset: () => {
debug("Disabling automine");
return setMiningMode(client, miningMode);
},
};
}

async function getMiningMode(client: Client): Promise<MiningMode> {
Expand Down

0 comments on commit 58f101e

Please sign in to comment.