Skip to content
Docs
Prepare Hooks
usePrepareContractWrite

usePrepareContractWrite

Hook for preparing a contract write to be sent via useContractWrite.

Eagerly fetches the parameters required for sending a contract write transaction such as the gas estimate.

import { usePrepareContractWrite } from 'wagmi'

Usage

usePrepareContractWrite gives back a "prepared config" to be sent through to useContractWrite.

import { usePrepareContractWrite, useContractWrite } from 'wagmi'

function App() {
  const { config, error } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
  })
  const { write } = useContractWrite(config)

  return (
    <>
      <button disabled={!write} onClick={() => write?.()}>
        Feed
      </button>
      {error && (
        <div>An error occurred preparing the transaction: {error.message}</div>
      )}
    </>
  )
}

Note: The write function will be undefined if the config has not been prepared (still in-flight or errored).

Return value

{
  data?: PrepareWriteContractResult
  error?: Error
  isIdle: boolean
  isLoading: boolean
  isFetching: boolean
  isSuccess: boolean
  isError: boolean
  isFetched: boolean
  isRefetching: boolean
  refetch: (options: {
    throwOnError: boolean
    cancelRefetch: boolean
  }) => Promise<PrepareWriteContractResult>
  status: 'idle' | 'error' | 'loading' | 'success'
}

Configuration

addressOrName

Contract address or ENS name.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
  })
}

contractInterface

Contract ABI in JSON or JS object format. An ethers Interface is also allowed.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
  })
}

functionName

Name of function to call.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
  })
}

args (optional)

Arguments to pass to function call. Accepts any | any[].

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    args: [],
  })
}

overrides (optional)

Overrides to pass to function call.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    overrides: {
      from: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
      value: ethers.utils.parseEther('0.01'),
    },
  })
}

cacheTime (optional)

Time (in ms) which the prepared config should remain in the cache.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    cacheTime: 2_000,
  })
}

enabled (optional)

Set this to false to disable this query from automatically running. Defaults to true.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    enabled: false,
  })
}

staleTime (optional)

Time (in ms) after prepared config is considered stale. If set to Infinity the data will never be considered stale.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    staleTime: 2_000,
  })
}

suspense (optional)

Set this to true to enable suspense mode.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    suspense: true,
  })
}

onSuccess (optional)

Function to invoke when fetching is successful.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    onSuccess(data) {
      console.log('Success', data)
    },
  })
}

onError (optional)

Function to invoke when an error is thrown while fetching new data.

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    onError(error) {
      console.log('Error', error)
    },
  })
}

onSettled (optional)

Function to invoke when fetching is settled (either successfully fetched, or an error has thrown).

import { usePrepareContractWrite } from 'wagmi'

function App() {
  const { config } = usePrepareContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    onSettled(data, error) {
      console.log('Settled', { data, error })
    },
  })
}