• Core
  • Actions
  • readContracts

readContracts

Action for calling multiple read methods on a Contract.

This is a wrapper around viem's multicall.

import { readContracts } from '@wagmi/core'

Usage

The following examples use the wagmigotchi & more loot contracts.

import { readContracts } from '@wagmi/core'
 
const wagmigotchiContract = {
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
}
const mlootContract = {
  address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
  abi: mlootABI,
}
 
const data = await readContracts({
  contracts: [
    {
      ...wagmigotchiContract,
      functionName: 'getAlive',
    },
    {
      ...wagmigotchiContract,
      functionName: 'getBoredom',
    },
    {
      ...mlootContract,
      functionName: 'getChest',
      args: [69],
    },
    {
      ...mlootContract,
      functionName: 'getWaist',
      args: [69],
    },
  ],
})

Return Value

ReadContractResult[]

ReadContractResult provides an inferred type from the outputs on functionName in the ABI (ie. the return type of the contract method). Learn more.

Configuration

contracts

address

Contract address.

import { readContracts } from '@wagmi/core'
 
const data = await readContracts({
  contracts: [
    {
      address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      abi: wagmigotchiABI,
      functionName: 'getAlive',
    },
    {
      address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      abi: wagmigotchiABI,
      functionName: 'getBoredom',
    },
  ],
})

abi

Contract ABI.

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

import { readContracts } from '@wagmi/core'
 
const data = await readContracts({
  contracts: [
    {
      address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      abi: wagmigotchiABI,
      functionName: 'getAlive',
    },
    {
      address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      abi: wagmigotchiABI,
      functionName: 'getBoredom',
    },
  ],
})

functionName

Name of function to call.

import { readContracts } from '@wagmi/core'
 
const data = await readContracts({
  contracts: [
    {
      address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      abi: wagmigotchiABI,
      functionName: 'getAlive',
    },
    {
      address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
      abi: wagmigotchiABI,
      functionName: 'getBoredom',
    },
  ],
})

args

Arguments to pass to function call.

import { readContracts } from '@wagmi/core'
 
const data = await readContracts({
  contracts: [
    {
      address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
      abi: mlootABI,
      functionName: 'getChest',
      args: [69],
    },
    {
      address: '0x1dfe7ca09e99d10835bf73044a23b73fc20623df',
      abi: mlootABI,
      functionName: 'getWaist',
      args: [69],
    },
  ],
})

chainId (optional)

Force a specific chain id for the request. The wagmi Client's publicClient must be set up as a chain-aware function for this to work correctly.

import { readContracts } from '@wagmi/core'
import { optimism } from '@wagmi/core/chains'
 
const data = await readContracts({
  contracts: [
    {
      ...wagmigotchiContract,
      functionName: 'getAlive',
      chainId: optimism.id,
    },
    {
      ...wagmigotchiContract,
      functionName: 'getBoredom',
      chainId: optimism.id,
    },
  ],
})

Note: The above example is using the optimism chain from @wagmi/core/chains.

allowFailure (optional)

If a contract read fails while fetching, it will fail silently and not throw an error.

import { readContracts } from '@wagmi/core'
 
const data = await readContracts({
  contracts: [
    {
      ...wagmigotchiContract,
      functionName: 'getAlive',
    },
    {
      ...wagmigotchiContract,
      functionName: 'getBoredom',
    },
  ],
  allowFailure: false,
})

blockNumber (optional)

The block number to perform the read against.

import { readContracts } from '@wagmi/core'
 
const data = await readContracts({
  contracts: [
    {
      ...wagmigotchiContract,
      functionName: 'getAlive',
    },
    {
      ...wagmigotchiContract,
      functionName: 'getBoredom',
    },
  ],
  blockNumber: 15121123n,
})

blockTag (optional)

The block tag to perform the read against. Accepts: 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized'

import { readContracts } from '@wagmi/core'
 
const data = await readContracts({
  contracts: [
    {
      ...wagmigotchiContract,
      functionName: 'getAlive',
    },
    {
      ...wagmigotchiContract,
      functionName: 'getBoredom',
    },
  ],
  blockTag: 'safe',
})