Skip to main content

Contract Events

You can listen to events that are emitted by a contract.

This is useful when you want real-time updates of events happening on the blockchain.

The names of the events are the same as the events emitted from the smart contract.

For prebuilt contracts, you can inspect the source code to see the events that are emitted in each function call.

Below are some examples:

Event NameDescriptionSource Code
TokensClaimedToken claimed by a wallet from a Drop contractSmart Contract Code
TokensMintedToken minted by a wallet into a NFT / Token contractSmart Contract Code
ListingAddedNew listing created in a marketplace contractSmart Contract Code

Get All Past Events

Fetch all historical events emitted by the contract.

// Optionally pass in filters to limit the blocks from which events are retrieved
const filters = {
fromBlock: 0,
toBlock: 1000000,
}
const events = await contract.events.getAllEvents(filters);
console.log(events[0].eventName);
console.log(events[0].data);
View in React SDK Documentation

Listen to a Specific Event

Fetch specific historical events emitted by the contract.

// The name of the event to get logs for
const eventName = "Transfer";

// Optionally pass in options to limit the blocks from which events are retrieved
const options = {
fromBlock: 0,
toBlock: 1000000, // can also pass "latest"
order: "desc",
// Configure event filters (filter on indexed event parameters)
filters: {
from: "0x...",
to: "0x..."
}
};

const events = await contract.events.getEvents(eventName, options);
console.log(events[0].eventName);
console.log(events[0].data);
View in React SDK Documentation

Listen to All Events

Listen to all events emitted by the contract.

contract.events.listenToAllEvents((event) => {
console.log(event.eventName) // the name of the emitted event
console.log(event.data) // event payload
}
View in React SDK Documentation

Listen to a Specific Event

Listen to all occurrences of a specific event.

contract.events.addEventListener("TokensMinted", (event) => {
console.log(event);
});
View in React SDK Documentation

Listen to Transactions

Listen to all transactions in their raw form.

contract.events.addTransactionListener((event) => {
console.log(event);
}
View in React SDK Documentation

Remove All Listeners

contract.events.removeAllListeners();
View in React SDK Documentation

Remove an Event Listener

contract.events.removeEventListener("TokensMinted", (event) => {
console.log(event);
});
View in React SDK Documentation

Remove a Transaction Listener

contract.events.removeTransactionListener((event) => {
console.log(event);
}
View in React SDK Documentation