clawchain/wagmi/site/react/api/hooks/useDeployContract.md

2.8 KiB

title, description
title description
useDeployContract Hook for deploying a contract to the network, given bytecode & constructor arguments.

useDeployContract

Hook for deploying a contract to the network, given bytecode, and constructor arguments.

Import

import { useDeployContract } from 'wagmi'

Usage

::: code-group

import { useDeployContract } from 'wagmi'
import { parseEther } from 'viem'
import { wagmiAbi } from './abi'

function App() {
  const deployContract = useDeployContract()

  return (
    <button
      onClick={() =>
        deployContract.mutate({
          abi: wagmiAbi,
          bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
        })
      }
    >
      Deploy Contract
    </button>
  )
}
export const wagmiAbi = [
  ...
  {
    inputs: [],
    stateMutability: "nonpayable",
    type: "constructor",
  },
  ...
] as const

<<< @/snippets/react/config.ts[config.ts] :::

Deploying with Constructor Args

::: code-group

import { useDeployContract } from 'wagmi'
import { parseEther } from 'viem'
import { wagmiAbi } from './abi'

function App() {
  const deployContract = useDeployContract()

  return (
    <button
      onClick={() =>
        deployContract.mutate({
          abi: wagmiAbi,
          args: [69420],
          bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
        })
      }
    >
      Deploy Contract
    </button>
  )
}
export const wagmiAbi = [
  ...
  {
    inputs: [{ name: "x", type: "uint32" }],
    stateMutability: "nonpayable",
    type: "constructor",
  },
  ...
] as const;

<<< @/snippets/react/config.ts[config.ts] :::

Parameters

import { type useDeployContractParameters } from 'wagmi'

config

Config | undefined

Config to use instead of retrieving from the nearest WagmiProvider.

::: code-group

import { useDeployContract } from 'wagmi'
import { config } from './config' // [!code focus]

function App() {
  const deployContract = useDeployContract({
    config, // [!code focus]
  })
}

<<< @/snippets/react/config.ts[config.ts] :::

Return Type

import { type useDeployContractReturnType } from 'wagmi'

Action