Skip to content

Commit

Permalink
feat(cli): quieter automine
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Sep 20, 2024
1 parent a08ba5e commit becbe49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion 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 resetMiningMode = await enableAutomine(client);

const startTime = Date.now();
const worldDeploy = await deploy({
Expand Down
29 changes: 13 additions & 16 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 = () => 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 async () => {};
}

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

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

0 comments on commit becbe49

Please sign in to comment.