Skip to content

writeContracts

Requests for the wallet to sign and broadcast a batch of write contract calls (transactions) to the network.

Read more.

Usage

example.ts
import { parseAbi } from 'viem'
import { account, walletClient } from './config'
 
const abi = parseAbi([
  'function approve(address, uint256) returns (bool)',
  'function transferFrom(address, address, uint256) returns (bool)',
])
 
const id = await walletClient.writeContracts({
  account,
  contracts: [
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'approve',
      args: [
        '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 
        100n
      ],
    },
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'transferFrom',
      args: [
        '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        '0x0000000000000000000000000000000000000000',
        100n
      ],
    },
  ],
})

Notes:

Account Hoisting

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

Learn more.

example.ts
import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi([
  'function approve(address, uint256) returns (bool)',
  'function transferFrom(address, address, uint256) returns (bool)',
])
 
const id = await walletClient.writeContracts({
  contracts: [
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'approve',
      args: [
        '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 
        100n
      ],
    },
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'transferFrom',
      args: [
        '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
        '0x0000000000000000000000000000000000000000',
        100n
      ],
    },
  ],
})

Returns

string

The identifier can be any arbitrary string. The only requirement is that for a given session, consumers should be able to call getCallsStatus with this identifier to retrieve a batch call status and call receipts.

Parameters

account

  • Type: Account | Address

The Account to sign & broadcast the call from.

Accepts a JSON-RPC Account.

import { parseAbi, mainnet } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  contracts: [
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'mint',
      args: [69n],
    },
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'mint',
      args: [420n],
    },
  ],
})

chain

  • Type: Chain
  • Default: walletClient.chain

The target chain to broadcast the calls.

import { parseAbi, mainnet } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  chain: mainnet, 
  contracts: [
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'mint',
      args: [69n],
    },
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      abi,
      functionName: 'mint',
      args: [420n],
    },
  ],
})

contracts

  • Type: MulticallContracts[]

An array of write contract calls to be signed and broadcasted.

import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  contracts: [ 
    { 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      abi, 
      functionName: 'mint', 
      args: [69n], 
    }, 
    { 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      abi, 
      functionName: 'mint', 
      args: [420n], 
    }, 
  ],
})

contracts.abi

  • Type: Abi

The contract's ABI.

import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  contracts: [
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      functionName: 'mint',
      args: [69n],
    },
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      functionName: 'mint',
      args: [420n],
    },
  ],
})

contracts.address

  • Type: Address

The contract address.

import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  contracts: [
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      abi,
      functionName: 'mint',
      args: [69n],
    },
    {
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      abi,
      functionName: 'mint',
      args: [420n],
    },
  ],
})

contracts.functionName

  • Type: string

A function to extract from the ABI.

import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  contracts: [
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      functionName: 'mint', 
      args: [69n],
    },
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      functionName: 'mint', 
      args: [420n],
    },
  ],
})

contracts.args

  • Type: Inferred from ABI.

Arguments to pass to function call.

import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  contracts: [
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      functionName: 'mint', 
      args: [69n], 
    },
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      functionName: 'mint', 
      args: [420n], 
    },
  ],
})

contracts.value

  • Type: number

Value in wei sent with this call.

import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  contracts: [
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      functionName: 'mint', 
      args: [69n],
    },
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      functionName: 'mint', 
      args: [420n],
      value: 69420n, 
    },
  ],
})

capabilities

  • Type: WalletCapabilities

Capability metadata for the calls (e.g. specifying a paymaster).

import { parseAbi } from 'viem'
import { walletClient } from './config'
 
const abi = parseAbi(['function mint(uint256)'])
 
const id = await walletClient.writeContracts({
  contracts: [
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', 
      functionName: 'mint', 
      args: [69n],
    },
    {
      abi, 
      address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
      functionName: 'mint', 
      args: [420n],
    },
  ],
  capabilities: { 
    paymasterService: { 
      url: 'https://...'
    } 
  } 
})