From 189050bd2c61ba645325d45a6a4040153d361412 Mon Sep 17 00:00:00 2001 From: Andy Cernera Date: Thu, 25 Apr 2024 18:49:34 +0900 Subject: [PATCH] feat(cli): manually fetch gas price from rpc before PostDeploy runs (#2638) Co-authored-by: Kevin Ingersoll --- .changeset/forty-rocks-rescue.md | 5 ++++ packages/cli/src/utils/postDeploy.ts | 40 +++++++++++++++++++++------- 2 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 .changeset/forty-rocks-rescue.md diff --git a/.changeset/forty-rocks-rescue.md b/.changeset/forty-rocks-rescue.md new file mode 100644 index 0000000000..75cf10feb1 --- /dev/null +++ b/.changeset/forty-rocks-rescue.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/cli": patch +--- + +Deploy will now fetch and set the gas price during execution of PostDeploy script. This should greatly reduce the fees paid for L2s. diff --git a/packages/cli/src/utils/postDeploy.ts b/packages/cli/src/utils/postDeploy.ts index a03eb1e2fa..72f276460b 100644 --- a/packages/cli/src/utils/postDeploy.ts +++ b/packages/cli/src/utils/postDeploy.ts @@ -2,6 +2,8 @@ import { existsSync } from "fs"; import path from "path"; import chalk from "chalk"; import { getScriptDirectory, forge } from "@latticexyz/common/foundry"; +import { execSync } from "child_process"; +import { formatGwei } from "viem"; export async function postDeploy( postDeployScript: string, @@ -9,17 +11,35 @@ export async function postDeploy( rpc: string, profile: string | undefined, ): Promise { - // Execute postDeploy forge script const postDeployPath = path.join(await getScriptDirectory(), postDeployScript + ".s.sol"); - if (existsSync(postDeployPath)) { - console.log(chalk.blue(`Executing post deploy script at ${postDeployPath}`)); - await forge( - ["script", postDeployScript, "--sig", "run(address)", worldAddress, "--broadcast", "--rpc-url", rpc, "-vvv"], - { - profile: profile, - }, - ); - } else { + if (!existsSync(postDeployPath)) { console.log(`No script at ${postDeployPath}, skipping post deploy hook`); + return; } + + // TODO: replace this with a viem call + const gasPrice = BigInt(execSync(`cast gas-price --rpc-url ${rpc}`, { encoding: "utf-8" }).trim()); + + console.log(chalk.blue(`Executing post deploy script at ${postDeployPath}`)); + console.log(chalk.blue(` using gas price of ${formatGwei(gasPrice)} gwei`)); + + await forge( + [ + "script", + postDeployScript, + "--broadcast", + "--sig", + "run(address)", + worldAddress, + // TODO: also set priority fee? + "--with-gas-price", + gasPrice.toString(), + "--rpc-url", + rpc, + "-vvv", + ], + { + profile: profile, + }, + ); }