From f0fea5af2b35e4b91182d6dff61d7043b0471fd2 Mon Sep 17 00:00:00 2001 From: yonada Date: Wed, 24 Apr 2024 14:21:44 +0100 Subject: [PATCH] refactor(cli): add getDeployer (#2717) --- packages/cli/src/deploy/ensureDeployer.ts | 18 ++++++------------ packages/cli/src/deploy/getDeployer.ts | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 packages/cli/src/deploy/getDeployer.ts diff --git a/packages/cli/src/deploy/ensureDeployer.ts b/packages/cli/src/deploy/ensureDeployer.ts index 86f59ccd18..fca96d0a04 100644 --- a/packages/cli/src/deploy/ensureDeployer.ts +++ b/packages/cli/src/deploy/ensureDeployer.ts @@ -1,21 +1,15 @@ -import { Account, Address, Chain, Client, Transport, sliceHex } from "viem"; -import { getBalance, getBytecode, sendRawTransaction, sendTransaction, waitForTransactionReceipt } from "viem/actions"; +import { Account, Address, Chain, Client, Transport } from "viem"; +import { getBalance, sendRawTransaction, sendTransaction, waitForTransactionReceipt } from "viem/actions"; import deployment from "./create2/deployment.json"; import { debug } from "./debug"; +import { getDeployer } from "./getDeployer"; const deployer = `0x${deployment.address}` as const; export async function ensureDeployer(client: Client): Promise
{ - const bytecode = await getBytecode(client, { address: deployer }); - if (bytecode) { - debug("found CREATE2 deployer at", deployer); - // check if deployed bytecode is the same as the expected bytecode (minus 14-bytes creation code prefix) - if (bytecode !== sliceHex(`0x${deployment.creationCode}`, 14)) { - console.warn( - `\n ⚠️ Bytecode for deployer at ${deployer} did not match the expected CREATE2 bytecode. You may have unexpected results.\n`, - ); - } - return deployer; + const existingDeployer = await getDeployer(client); + if (existingDeployer !== undefined) { + return existingDeployer; } // There's not really a way to simulate a pre-EIP-155 (no chain ID) transaction, diff --git a/packages/cli/src/deploy/getDeployer.ts b/packages/cli/src/deploy/getDeployer.ts new file mode 100644 index 0000000000..03f22eda3a --- /dev/null +++ b/packages/cli/src/deploy/getDeployer.ts @@ -0,0 +1,20 @@ +import { Address, Chain, Client, Transport, sliceHex } from "viem"; +import { getBytecode } from "viem/actions"; +import deployment from "./create2/deployment.json"; +import { debug } from "./debug"; + +const deployer = `0x${deployment.address}` as const; + +export async function getDeployer(client: Client): Promise
{ + const bytecode = await getBytecode(client, { address: deployer }); + if (bytecode) { + debug("found CREATE2 deployer at", deployer); + // check if deployed bytecode is the same as the expected bytecode (minus 14-bytes creation code prefix) + if (bytecode !== sliceHex(`0x${deployment.creationCode}`, 14)) { + console.warn( + `\n ⚠️ Bytecode for deployer at ${deployer} did not match the expected CREATE2 bytecode. You may have unexpected results.\n`, + ); + } + return deployer; + } +}