ContractEvents
Listen to Contract events in real time
class ContractEvents<TContract extends BaseContract> {}
function constructor( contractWrapper: ContractWrapper<TContract>,
Subscribe to contract events
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);});
function addEventListener( eventName: (string & {}) | keyof TContract["filters"],): () => void;
the callback function that will be called on every new event
Subscribe to transactions in this contract.
Will emit an "event" object containing the transaction status ('submitted' and 'completed') and hash
contract.events.addTransactionListener((event) => { console.log(event);}
function addTransactionListener( listener: ListenerFn<Array<any>>,): void;
Get All Events
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 retrievedconst filters = { fromBlock: 0, toBlock: 1000000,};const events = await contract.events.getAllEvents(filters);console.log(events[0].eventName);console.log(events[0].data);
function getAllEvents(
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 Events
Get a list of the events of a specific type emitted from this contract during the specified time period
EventQueryOptions
// The name of the event to get logs forconst eventName = "Transfer"; // Optionally pass in options to limit the blocks from which events are retrievedconst 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);
function getEvents( eventName: string,
Specify the from and to block numbers to get events for, defaults to all blocks.
The requested event objects with event data
Listen to all events emitted from this contract
contract.events.listenToAllEvents((event) => { console.log(event.eventName) // the name of the emitted event console.log(event.data) // event payload}
function listenToAllEvents(): () => void;
the callback function that will be called on every new event
Remove an event listener from this contract
Remove a listener that was added with addEventListener
contract.events.removeEventListener("TokensMinted", (event) => { console.log(event);});
function removeEventListener( eventName: (string & {}) | keyof TContract["filters"], listener: Listener,): void;
Remove a transaction listener
Remove a listener that was added with addTransactionListener
contract.events.removeTransactionListener((event) => { console.log(event);}
function removeTransactionListener( listener: ListenerFn<Array<any>>,): void;