Skip to main content

ContractEvents.addEventListener() method

Subscribe to contract events

Example

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

Signature:

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

Parameters

ParameterTypeDescription
eventNamekeyof TContract["filters"] | (string & {})the event name as defined in the contract
listener(event: ContractEvent<TEvent>) => 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: