• Core
  • Actions
  • writeContract

writeContract

Action for calling a Contract write method.

This is a wrapper around viem's writeContract.

import { writeContract } from '@wagmi/core'

Usage

The following examples use the wagmigotchi contract.

import { writeContract } from '@wagmi/core'
 
const { hash } = await writeContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'claim',
  args: [69],
})

Prepared Usage

import { prepareWriteContract, writeContract } from '@wagmi/core'
 
const config = await prepareWriteContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'claim',
  args: [69],
})
 
...
 
const { hash } = await writeContract(config)

Return Value

{
  hash: `0x${string}`
}

Configuration

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

address

Contract address.

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

functionName

Name of function to call.

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

args (optional)

Arguments to pass to function call.

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

chainId (optional)

Checks the current chain to make sure it is the same as chainId. If chainId is not the current chain, the Action will throw an error.

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

account (optional)

The Account to write to the contract from.

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

gas (optional)

Gas limit for transaction execution.

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

maxFeePerGas (optional)

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

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

maxPriorityFeePerGas (optional)

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

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

nonce (optional)

Unique number identifying this transaction.

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

value (optional)

Value in wei sent with this transaction.

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