Class ContractEvents<TContract>

Listen to Contract events in real time

Type Parameters

  • TContract extends BaseContract

Hierarchy

  • ContractEvents

Constructors

Properties

contractWrapper: ContractWrapper<TContract>

Methods

  • Subscribe to contract events

    Type Parameters

    • TEvent extends Record<string, any>

    Parameters

    • eventName: string & {} | keyof TContract["filters"]

      the event name as defined in the contract

    • listener: ((event) => void)

      the callback function that will be called on every new event

    Returns (() => void)

    a function to un-subscribe from the event

      • (): void
      • Returns void

    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:

    Example

    contract.events.addEventListener("TokensMinted", (event) => {
    console.log(event);
    });
  • Subscribe to transactions in this contract.

    Parameters

    • listener: ListenerFn<any[]>

      the callback function that will be called on every transaction

    Returns void

    Remarks

    Will emit an "event" object containing the transaction status ('submitted' and 'completed') and hash

    Example

    contract.events.addTransactionListener((event) => {
    console.log(event);
    }
  • Get All Events

    Type Parameters

    • TEvent extends Record<string, any>

    Parameters

    • filters: Omit<EventQueryOptions<Record<string, any>>, "filters"> = ...

      Specify the from and to block numbers to get events for, defaults to all blocks

    Returns Promise<ContractEvent<TEvent>[]>

    The event objects of the events emitted with event names and data for each event

    Remarks

    Get a list of all the events emitted from this contract during the specified time period

    Example

    // 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

    Type Parameters

    • TEvent extends Record<string, any> = Record<string, any>

    • TFilter extends Record<string, any> = Record<string, any>

    Parameters

    • eventName: string

      The name of the event to get logs for

    • options: EventQueryOptions<TFilter> = ...

      Specify the from and to block numbers to get events for, defaults to all blocks.

    Returns Promise<ContractEvent<TEvent>[]>

    The requested event objects with event data

    Remarks

    Get a list of the events of a specific type emitted from this contract during the specified time period

    Example

    // 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);

    See

    EventQueryOptions

  • Listen to all events emitted from this contract

    Type Parameters

    • TEvent extends Record<string, any>

    Parameters

    • listener: ((event) => void)

      the callback function that will be called on every new event

    Returns (() => void)

    A function that can be called to stop listening to events

      • (): void
      • Returns void

    Example

    contract.events.listenToAllEvents((event) => {
    console.log(event.eventName) // the name of the emitted event
    console.log(event.data) // event payload
    }
  • Remove an event listener from this contract

    Parameters

    • eventName: string & {} | keyof TContract["filters"]

      the event name as defined in the contract

    • listener: Listener

      the listener to unregister

    Returns void

    Remarks

    Remove a listener that was added with addEventListener

    Example

    contract.events.removeEventListener("TokensMinted", (event) => {
    console.log(event);
    });
  • Remove a transaction listener

    Parameters

    • listener: ListenerFn<any[]>

      the callback function to remove

    Returns void

    Remarks

    Remove a listener that was added with addTransactionListener

    Example

    contract.events.removeTransactionListener((event) => {
    console.log(event);
    }

Generated using TypeDoc