• Core
  • Actions
  • prepareWriteContract

prepareWriteContract

Action for preparing a contract write to be sent via writeContract.

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

import { prepareWriteContract } from '@wagmi/core'

Usage

prepareWriteContract gives back a "prepared config" to be sent through to writeContract.

import { prepareWriteContract, writeContract } from '@wagmi/core'
 
const config = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
})
const { hash } = await writeContract(config)

Return value

{
  abi: Abi
  address: Address
  chainId: number
  functionName: string
  mode: 'prepared'
  request: PopulatedTransaction
}

Configuration

address

Contract address.

import { prepareWriteContract } from '@wagmi/core'
 
const config = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
})

abi

Contract ABI.

By defining inline or adding a const assertion to abi, TypeScript will infer the correct types for functionName and args. See the wagmi TypeScript docs for more information.

import { prepareWriteContract } from '@wagmi/core'
 
const config = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
})

functionName

Name of function to call.

import { prepareWriteContract } from '@wagmi/core'
 
const config = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
})

args

Arguments to pass to function call.

import { prepareWriteContract } from '@wagmi/core'
 
const config = await prepareWriteContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: [
    {
      name: 'mint',
      type: 'function',
      stateMutability: 'nonpayable',
      inputs: [{ internalType: 'uint32', name: 'tokenId', type: 'uint32' }],
      outputs: [],
    },
  ],
  functionName: 'mint',
  args: [69],
})

chainId (optional)

Chain ID used to validate if the user is connected to the target chain.

import { prepareWriteContract } from '@wagmi/core'
 
const { config } = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
  chainId: 1,
})

overrides (optional)

Overrides to pass to function call. If the function is payable, you can pass a value here.

import { prepareWriteContract } from '@wagmi/core'
 
const { config } = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
  overrides: {
    from: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    value: ethers.utils.parseEther('0.01'),
  },
})