Skip to content
Docs
Hooks
useContractWrite

useContractWrite

Hook for calling a ethers Contract write method.

import { useContractWrite } from 'wagmi'

Usage

The following examples use the wagmigotchi contract.

import { useContractWrite } from 'wagmi'

function App() {
  const { data, isError, isLoading, write } = useContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
  })

  return <button onClick={() => write()}>Feed</button>
}

Return Value

{
  data?: TransactionResponse
  error?: Error
  isError: boolean
  isIdle: boolean
  isLoading: boolean
  isSuccess: boolean
  write: (config?: WriteContractConfig) => void
  writeAsync: (config?: WriteContractConfig) => Promise<TransactionResponse>
  reset: () => void
  status: 'idle' | 'error' | 'loading' | 'success'
}

Configuration

addressOrName

Contract address or ENS name.

import { useContractWrite } from 'wagmi'

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

  return <button onClick={() => write()}>Feed</button>
}

chainId (optional)

Checks the current chain to make sure it is the same as chainId. If chainId is not the current chain, the connector attempts to switch to it before sending the transaction.

import { useContractWrite } from 'wagmi'

function App() {
  const contractWrite = useContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    chainId: 1,
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
  })

  return <button onClick={() => write()}>Feed</button>
}

contractInterface

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

import { useContractWrite } from 'wagmi'

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

  return <button onClick={() => write()}>Feed</button>
}

functionName

Name of function to call.

import { useContractWrite } from 'wagmi'

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

  return <button onClick={() => write()}>Feed</button>
}

args (optional)

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

import { useContractWrite } from 'wagmi'

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

overrides (optional)

Overrides to pass to function call.

import { useContractWrite } from 'wagmi'

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

onError (optional)

Function to invoke when an error is thrown while attempting to write.

import { useContractWrite } from 'wagmi'

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

onMutate (optional)

Function fires before write function and is passed same variables write function would receive. Value returned from this function will be passed to both onError and onSettled functions in event of a write failure.

import { useContractWrite } from 'wagmi'

function App() {
  const contractWrite = useContractWrite({
    addressOrName: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
    contractInterface: wagmigotchiABI,
    functionName: 'feed',
    onMutate({ args, overrides }) {
      console.log('Mutate', { args, overrides })
    },
  })
}

onSettled (optional)

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

import { useContractWrite } from 'wagmi'

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

onSuccess (optional)

Function to invoke when write is successful

import { useContractWrite } from 'wagmi'

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