Protected
contractSubscribe to contract events
the event name as defined in the contract
the callback function that will be called on every new event
a function to un-subscribe from the event
You can add a listener for any contract event to run a function when the event is emitted. For example, if you wanted to listen for a "TokensMinted" event, you could do the following:
contract.events.addEventListener("TokensMinted", (event) => {
console.log(event);
});
Subscribe to transactions in this contract.
the callback function that will be called on every transaction
Will emit an "event" object containing the transaction status ('submitted' and 'completed') and hash
contract.events.addTransactionListener((event) => {
console.log(event);
}
Get All Events
Specify the from and to block numbers to get events for, defaults to all blocks
The event objects of the events emitted with event names and data for each event
Get a list of all the events emitted from this contract during the specified time period
// 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);
Get Events
The name of the event to get logs for
Specify the from and to block numbers to get events for, defaults to all blocks.
The requested event objects with event data
Get a list of the events of a specific type emitted from this contract during the specified time period
// 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);
EventQueryOptions
Listen to all events emitted from this contract
the callback function that will be called on every new event
A function that can be called to stop listening to events
contract.events.listenToAllEvents((event) => {
console.log(event.eventName) // the name of the emitted event
console.log(event.data) // event payload
}
Private
parseRemove an event listener from this contract
the event name as defined in the contract
the listener to unregister
Remove a listener that was added with addEventListener
contract.events.removeEventListener("TokensMinted", (event) => {
console.log(event);
});
Remove a transaction listener
the callback function to remove
Remove a listener that was added with addTransactionListener
contract.events.removeTransactionListener((event) => {
console.log(event);
}
Private
toGenerated using TypeDoc
Listen to Contract events in real time