Skip to content

writeContract

Executes a write function on a contract, with EIP712 transaction support.

Usage

example.ts
import { account, walletClient } from './config'
 
const hash = await walletClient.writeContract({ 
  account,
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
})
// '0x...'

Account Hoisting

If you do not wish to pass an account to every sendTransaction, you can also hoist the Account on the Wallet Client (see config.ts).

Learn more.

example.ts
import { walletClient } from './config'
 
const hash = await walletClient.writeContract({ 
  account,
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
})
// '0x...'

Returns

Hash

The Transaction hash.

Parameters

address

The contract address.

await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420]
})

abi

The contract's ABI.

await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi, 
  functionName: 'mint',
  args: [69420]
})

functionName

  • Type: string

A function to extract from the ABI.

await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint', 
  args: [69420]
})

account

  • Type: Account | Address

The Account to send the transaction from.

Accepts a JSON-RPC Account or Local Account (Private Key, etc).

await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420]
})

accessList (optional)

The access list.

const hash = await walletClient.writeContract({
  accessList: [ 
    {
      address: '0x1',
      storageKeys: ['0x1'],
    },
  ],
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420]
})

chain (optional)

  • Type: Chain
  • Default: walletClient.chain

The target chain. If there is a mismatch between the wallet's current chain & the target chain, an error will be thrown.

import { zksync } from 'viem/chains'
 
const hash = await walletClient.writeContract({
  chain: zksync, 
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420]
})

data (optional)

  • Type: 0x${string}

A contract hashed method call with encoded args.

const hash = await walletClient.writeContract({
  data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420]
})

gasPrice (optional)

  • Type: bigint

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

const hash = await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420],
  gasPrice: parseGwei('20'), 
})

nonce (optional)

  • Type: number

Unique number identifying this transaction.

const hash = await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420],
  nonce: 69
})

value (optional)

  • Type: bigint

Value in wei sent with this transaction.

const hash = await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420],
  value: parseEther('1'), 
})

gasPerPubdata (optional)

  • Type: bigint

The amount of gas for publishing one byte of data on Ethereum.

const hash = await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420],
  gasPerPubdata: 50000, 
})

factoryDeps (optional)

  • Type: [0x${string}]

Contains bytecode of the deployed contract.

const hash = await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420],
  factoryDeps: ['0xcde...'], 
})

paymaster (optional)

  • Type: Account | Address

Address of the paymaster account that will pay the fees. The paymasterInput field is required with this one.

const hash = await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420],
  paymaster: '0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021', 
  paymasterInput: '0x8c5a...'
})

paymasterInput (optional)

  • Type: 0x${string}

Input data to the paymaster. The paymaster field is required with this one.

const hash = await walletClient.writeContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: wagmiAbi,
  functionName: 'mint',
  args: [69420],
  paymaster: '0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021', 
  paymasterInput: '0x8c5a...'
})