Skip to content

Commit

Permalink
Updates create2 command to generate broadcast artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
nitaliano committed Jan 14, 2025
1 parent 5a6bee0 commit c06e6fe
Showing 1 changed file with 79 additions and 4 deletions.
83 changes: 79 additions & 4 deletions packages/cli/src/deploy-create2/DeployCreate2Command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Hex,
zeroAddress,
} from 'viem';
import {useConfig, useWaitForTransactionReceipt, useWriteContract} from 'wagmi';
import {useConfig, useTransaction, useWaitForTransactionReceipt, useWriteContract} from 'wagmi';
import {useForgeArtifact} from '@/queries/forgeArtifact';
import {ForgeArtifact} from '@/forge/readForgeArtifact';
import {CREATEX_ADDRESS, createXABI} from '@/contracts/createx/constants';
Expand All @@ -35,6 +35,9 @@ import {useCodeForChains} from '@/deploy-create2/useCodeForChains';
import {useRefetchCodeOnReceipt} from '@/deploy-create2/useRefetchCodeOnReceipt';
import {VerifyCommandInner} from '@/commands/verify';
import {useGasEstimation} from '@/hooks/useGasEstimation';
import { useBroadcastStore } from '@/stores/broadcastStore';
import { writeMultichainBroadcast } from '@/utils/broadcasts';
import { fromFoundryArtifactPath } from '@/forge/foundryProject';

// Prepares any required data or loading state if waiting
export const DeployCreate2Command = ({
Expand All @@ -57,7 +60,11 @@ export const DeployCreate2Command = ({
return <Spinner />;
}

const chains = options.chains.map(
// TODO: Fix option formatting between wizard and command
// Wizards = [ 'op', 'base' ]
// Command = [ 'op, base' ]
const flattenedChains = options.chains.flatMap(chain => chain.split(','));
const chains = flattenedChains.map(
chain => chainByIdentifier[`${options.network}/${chain}`]!,
);

Expand All @@ -79,6 +86,8 @@ const DeployCreate2CommandInner = ({
forgeArtifact: ForgeArtifact;
options: DeployCreateXCreate2Params;
}) => {
const { createBroadcast } = useBroadcastStore();

const [executionOption, setExecutionOption] =
useState<ExecutionOption | null>(
options.privateKey
Expand All @@ -104,6 +113,19 @@ const DeployCreate2CommandInner = ({
chainIds: chains.map(chain => chain.id),
});

useEffect(() => {
(async () => {
const { foundryProject, contractFileName } = await fromFoundryArtifactPath(options.forgeArtifactPath)

createBroadcast({
contractAddress: deterministicAddress,
contractName: contractFileName,
foundryProjectRoot: foundryProject.baseDir,
contractArguments: options.constructorArgs?.split(','),
});
})()
}, []);

return (
<Box flexDirection="column" gap={1}>
<Box flexDirection="column" gap={1}>
Expand Down Expand Up @@ -202,6 +224,21 @@ const CompletedOrVerify = ({
contractAddress: Address;
forgeArtifact: ForgeArtifact;
}) => {
const { broadcasts } = useBroadcastStore();
const [isGeneratingBroadcastArtifacts, setIsGeneratingBroadcastArtifacts] = useState(true);

useEffect(() => {
const multichainBroadcast = broadcasts[contractAddress];
if (multichainBroadcast) {
writeMultichainBroadcast(multichainBroadcast);
}
setIsGeneratingBroadcastArtifacts(false);
}, [broadcasts]);

if (isGeneratingBroadcastArtifacts) {
return <Spinner label="Generating broadcast artifacts" />;
}

if (!shouldVerify) {
return (
<Box>
Expand Down Expand Up @@ -409,12 +446,19 @@ const PrivateKeyExecution = ({
data: transactionHash,
} = useWriteContract();

const { addTransaction } = useBroadcastStore();

const {data: receipt, isLoading: isReceiptLoading} =
useWaitForTransactionReceipt({
hash: transactionHash,
chainId: chain.id,
});

const { data: transaction, isLoading: isTransactionLoading } = useTransaction({
hash: transactionHash,
chainId: chain.id,
});

useRefetchCodeOnReceipt(deterministicAddress, chain.id, receipt);

useEffect(() => {
Expand All @@ -428,6 +472,18 @@ const PrivateKeyExecution = ({
});
}, []);

useEffect(() => {
if (receipt && transaction) {
addTransaction({
chainId: chain.id,
hash: transaction.hash,
transaction: transaction,
receipt: receipt,
contractAddress: deterministicAddress,
});
}
}, [receipt, transaction]);

if (isPending) {
return <Spinner label="Deploying contract" />;
}
Expand All @@ -436,7 +492,7 @@ const PrivateKeyExecution = ({
return <Text>Error deploying contract: {error.message}</Text>;
}

if (isReceiptLoading) {
if (isReceiptLoading || isTransactionLoading) {
return <Spinner label="Waiting for receipt" />;
}

Expand All @@ -460,6 +516,7 @@ const ExternalSignerExecution = ({
baseSalt: Hex;
deterministicAddress: Address;
}) => {
const { addTransaction } = useBroadcastStore();
const encodedData = encodeFunctionData({
abi: createXABI,
args: [baseSalt, initCode],
Expand Down Expand Up @@ -488,9 +545,27 @@ const ExternalSignerExecution = ({
chainId: chain.id,
});

const { data: transaction, isLoading: isTransactionLoading } = useTransaction({
hash: taskEntryById[taskId]?.hash,
chainId: chain.id,
});

useRefetchCodeOnReceipt(deterministicAddress, chain.id, receipt);

if (isReceiptLoading) {

useEffect(() => {
if (receipt && transaction) {
addTransaction({
chainId: chain.id,
hash: transaction.hash,
transaction: transaction,
receipt: receipt,
contractAddress: deterministicAddress,
});
}
}, [receipt, transaction]);

if (isReceiptLoading || isTransactionLoading) {
return <Spinner label="Waiting for receipt" />;
}

Expand Down

0 comments on commit c06e6fe

Please sign in to comment.