useContract
Hook for declaratively creating a type-safe 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
ethers.Contract | nullConfiguration
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,
})
}signerOrProvider (optional)
import { useContract, useProvider } from 'wagmi'
function App() {
const provider = useProvider()
const contract = useContract({
address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
abi: ensRegistryABI,
signerOrProvider: provider,
})
}