• React
  • Hooks
  • useContract

useContract

Hook for declaratively creating a type-safe viem Contract Instance.

import { useContract } from 'wagmi'
đź’ˇ

If needing to invoke a read or write method from a contract, it is recommended to use useContractRead or useContractWrite instead of imperatively calling the function.

Usage

The following examples use the ENS Registry contract.

import { useContract } from 'wagmi'
 
function App() {
  const contract = useContract({
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
  })
}

Return Value

GetContractReturnType | null

Configuration

address (optional)

Contract address. If address is not defined, response is null.

import { useContract } from 'wagmi'
 
function App() {
  const contract = useContract({
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
  })
}

abi (optional)

Contract ABI. If abi is not defined, response is null.

By defining inline or adding a const assertion to abi, TypeScript will infer the correct types for properties and methods on the contract object. See the wagmi TypeScript docs for more information.

import { useContract } from 'wagmi'
 
function App() {
  const contract = useContract({
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
  })
}

walletClient (optional)

A viem Wallet Client with a connected account.

import { useContract, useWalletClient } from 'wagmi'
 
function App() {
  const { data: walletClient } = useWalletClient()
  const contract = useContract({
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    walletClient,
  })
}