• Core
  • Actions
  • readContracts

readContracts

Action for calling multiple ethers Contract read-only methods.

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 ethers provider 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,
})

overrides (optional)

Overrides to pass to function call.

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