• Core
  • Actions
  • readContract

readContract

Action for calling an ethers Contract read-only method.

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

overrides (optional)

Overrides to pass to function call.

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