• Core
  • Actions
  • readContract

readContract

Action for calling a read method on a Contract.

This is a wrapper around viem's readContract.

import { readContract } from '@wagmi/core'

Usage

import { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
})

Return Value

The data returned from invoking the contract method.

ReadContractResult<TAbi, TFunctionName>

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

Configuration

address

The address of the contract.

import { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
})

abi

The ABI of the contract.

import { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
})

functionName

The name of the function on the contract to call.

import { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
})

args (optional)

Arguments to pass to function call.

import { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'love',
  args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'],
})

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 { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'love',
  chainId: 1,
})

account (optional)

Account sender override.

import { readContract } from '@wagmi/core'
 
const { account } = await getWalletClient()
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
  account,
})

blockNumber (optional)

The block number to perform the read against.

import { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
  blockNumber: 15121123n,
})

blockTag (optional)

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

import { readContract } from '@wagmi/core'
 
const data = await readContract({
  address: '0xecb504d39723b0be0e3a9aa33d646642d1051ee1',
  abi: wagmigotchiABI,
  functionName: 'getHunger',
  blockTag: 'safe',
})