Skip to main content

ContractEvents.addEventListener() method

Subscribe to contract events

Example

contract.events.addEventListener("TokensMinted", (event) => {
console.log(event);
});

Signature:

addEventListener(eventName: keyof TContract["filters"] | (string & {}), listener: (event: Record<string, any>) => void): () => void;

Parameters

ParameterTypeDescription
eventNamekeyof TContract["filters"] | (string & {})the event name as defined in the contract
listener(event: Record<string, any>) => voidthe callback function that will be called on every new event

Returns:

() => void

a function to un-subscribe from the event

Remarks

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: