Skip to content
Docs
Deprecated Hooks
useDeprecatedContractWrite

useDeprecatedContractWrite

💡

This hook was previously named useContractWrite. It is now deprecated and will be removed in v1.

Hook for calling a ethers Contract write method.

import { useDeprecatedContractWrite } from 'wagmi'

Usage

The following examples use the wagmigotchi contract.

import { useDeprecatedContractWrite } from 'wagmi'

function App() {
  const { data, isError, isLoading, write } = useDeprecatedContractWrite({
    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 { useDeprecatedContractWrite } from 'wagmi'

function App() {
  const contractWrite = useDeprecatedContractWrite({
    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 { useDeprecatedContractWrite } from 'wagmi'

function App() {
  const contractWrite = useDeprecatedContractWrite({
    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 { useDeprecatedContractWrite } from 'wagmi'

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

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

functionName

Name of function to call.

import { useDeprecatedContractWrite } from 'wagmi'

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

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

args (optional)

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

import { useDeprecatedContractWrite } from 'wagmi'

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

overrides (optional)

Overrides to pass to function call.

import { useDeprecatedContractWrite } from 'wagmi'

function App() {
  const contractWrite = useDeprecatedContractWrite({
    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 { useDeprecatedContractWrite } from 'wagmi'

function App() {
  const contractWrite = useDeprecatedContractWrite({
    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 { useDeprecatedContractWrite } from 'wagmi'

function App() {
  const contractWrite = useDeprecatedContractWrite({
    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 { useDeprecatedContractWrite } from 'wagmi'

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

onSuccess (optional)

Function to invoke when write is successful

import { useDeprecatedContractWrite } from 'wagmi'

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