• 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 { request } = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
})
const { hash } = await writeContract(request)

Return value

{
  chainId: number
  mode: 'prepared'
  request: WriteContractParameters
  result: ContractFunctionResult
}

Configuration

address

Contract address.

import { prepareWriteContract } from '@wagmi/core'
 
const { request } = 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 { request } = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
})

functionName

Name of function to call.

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

args

Arguments to pass to function call.

import { prepareWriteContract } from '@wagmi/core'
 
const { request } = 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 { request } = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'feed',
  chainId: 1,
})

account (optional)

The Account to write to the contract from.

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

gas (optional)

Gas limit for transaction execution.

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

gasPrice (optional)

The price (in wei) to pay per gas. Only applies to Legacy Transactions.

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

maxFeePerGas (optional)

Total fee per gas (in wei), inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions

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

maxPriorityFeePerGas (optional)

Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions

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

nonce (optional)

Unique number identifying this transaction.

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

value (optional)

Value in wei sent with this transaction.

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