getContractEvents
Action for fetching a list of contract event logs matching the provided parameters.
Import
import { getContractEvents } from '@wagmi/core'Usage
By default, getContractEvents returns all matched events on the ABI. In practice, you must use scoping to filter for specific events.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
// Fetch event logs for every event on every ERC-20 contract.
const logs = getContractEvents(config, { abi })export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})Parameters
import { type GetContractEventsParameters } from '@wagmi/core'abi
Abi
The contract's ABI. Check out the TypeScript docs for how to set up ABIs for maximum type inference and safety.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})address
Address | undefined
The contract's address.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
abi,
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})args
object | readonly unknown[] | undefined
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
eventName: 'Transfer',
args: {
from: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B',
},
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})Only indexed arguments in event are candidates for args.
An argument can also be an array to indicate that other values can exist in the position:
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
eventName: 'Transfer',
args: {
from: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
to: [
'0xd2135CfB216b74109775236E36d4b433F1DF507B',
'0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
'0xa152f8bb749c55e9943a3a0a3111d18ee2b3f94e',
],
},
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})blockHash
`0x${string}` | undefined
- Block hash to include logs from. Mutually exclusive with
fromBlock/toBlock.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
blockHash: '0x4ca7ee652d57678f26e887c149ab0735f41de37bcad58c9f6d3ed5824f15b74d',
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})chainId
config['chains'][number]['id'] | undefined
ID of chain to use when fetching data.
import { getContractEvents } from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
chainId: mainnet.id,
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})eventName
string | undefined
- Event to filter for the contract.
- Inferred from
abi.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
eventName: 'Approval',
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})fromBlock
bigint | 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized' | undefined
- Block to start including logs from. Mutually exclusive with
blockHash.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
fromBlock: 69420n,
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})strict
boolean | undefined
- Flag to only return logs that conform to the indexed & non-indexed arguments on the
event, meaning thatargswill always be defined. Defaults tofalse.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
strict: true,
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})toBlock
bigint | 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized' | undefined
- Block to stop including logs from. Mutually exclusive with
blockHash.
import { getContractEvents } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'
const logs = getContractEvents(config, {
abi,
toBlock: 70120n,
})export const abi = [
{
type: 'event',
name: 'Approval',
inputs: [
{ indexed: true, name: 'owner', type: 'address' },
{ indexed: true, name: 'spender', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
{
type: 'event',
name: 'Transfer',
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' },
],
},
] as constimport { createConfig, http } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})Return Type
import { type GetContractEventsReturnType } from '@wagmi/core'A list of event logs.
Type Inference
With abi setup correctly, TypeScript will infer the correct types for eventName, args, and onLogs parameters. See the Wagmi TypeScript docs for more information.
Error
import { type GetContractEventsError } from '@wagmi/core'TanStack Query
import {
type GetContractEventsData,
type GetContractEventsOptions,
type GetContractEventsQueryFnData,
type GetContractEventsQueryKey,
getContractEventsQueryKey,
getContractEventsQueryOptions,
} from '@wagmi/core/query'