useContractEvents

Hook for reading events emitted by a smart contract, including new events as they are emitted (optional).

By default, it reads all events emitted by the smart contract.

Example

import { useContractEvents, useContract } from "@thirdweb-dev/react";
function App() {
const { contract } = useContract(contractAddress);
const { data, isLoading, error } = useContractEvents(contract);
}
function useContractEvents(
contract: RequiredParam<ValidContractInstance>,
eventName?: string,
options: {
queryFilter?: EventQueryOptions<Record<string, any>>;
subscribe?: boolean;
},
): UseQueryResult<Array<ContractEvent<Record<string, any>>>, unknown>;

Parameters

The contract instance of the contract to listen to events for

Type

let contract: RequiredParam<ValidContractInstance>;

The name of the event to query for.

For example, if your smart contract emits an event called MyEvent , you would pass "MyEvent" to this parameter.

Omit this parameter or provide undefined to query for all events emitted by the smart contract.

Type

let eventName: string;

An object containing options to filter the events being queried.

Available options include

  • queryFilter to refine which events you want to read,

  • a boolean subscribe flag to subscribe to new events as they are emitted.

Example

import {
useContractEvents,
useContract,
Web3Button,
} from "@thirdweb-dev/react";
function App() {
const { contract } = useContract(contractAddress);
const { data, isLoading, error } = useContractEvents(
contract,
"MyEvent",
{
queryFilter: {
filters: {
tokenId: 123, // e.g. Only events where tokenId = 123
},
fromBlock: 0, // Events starting from this block
toBlock: 100, // Events up to this block
order: "asc", // Order of events ("asc" or "desc")
},
subscribe: true, // Subscribe to new events
},
);

Type

let options: {
queryFilter?: EventQueryOptions<Record<string, any>>;
subscribe?: boolean;
};

Returns

let returnType: UseQueryResult<
Array<ContractEvent<Record<string, any>>>,
unknown
>;

The hook's data property, once loaded, contains an array of ContractEvent objects