Event Hub

Class Name EventHub
Extends Logger
Source event-hub.ts

The EventHub helper is wrapper for using contract events. These include - contract events (e.g. contract factory may trigger an event, announcing the address of the new contract) - global events (some contracts in the evan.network economy, like the MailBox use such global events)


constructor

new EventHub(options);

Creates a new EventHub instance.

Parameters

  1. options - EventHubOptions: options for EventHub constructor.
    • config - any: configuration object for the eventhub module
    • nameResolver - NameResolver: NameResolver instance
    • contractLoader - ContractLoader: ContractLoader instance
    • eventWeb3 - Web3 (optional): Web3 instance used for event handling (metamask web3 can’t handle events correct)
    • log - Function (optional): function to use for logging: (message, level) => {...}
    • logLevel - LogLevel (optional): messages with this level will be logged with log
    • logLog - LogLogInterface (optional): container for collecting log messages
    • logLogLevel - LogLevel (optional): messages with this level will be pushed to logLog

Returns

EventHub instance

Example

const eventHub = new EventHub({
    config,
    nameResolver,
    contractLoader,
  });

subscribe

eventHub.subscribe(contractName, contractAddress, eventName, filterFunction, onEvent, fromBlock);

subscribe to a contract event or a global EventHub event

Parameters

  1. contractName - string: target contract name (must be available within ContractLoader )
  2. contractAddress - string: target contract address
  3. eventName - string: name of the event to subscribe to
  4. filterFunction - function: a function that returns true or a Promise that resolves to true if onEvent function should be applied
  5. onEvent - function: executed when event was fired and the filter matches, gets the event as its parameter
  6. fromBlock - string (optional): get all events from this block, defaults to latest

Returns

Promise resolves to string: event subscription.

Example

// subscribe to the 'ContractEvent' at the EventHub located at '00000000000000000000000000000000deadbeef'
runtime.eventHub
          .subscribe(
            'EventHub',
            '00000000000000000000000000000000deadbeef',
            'ContractEvent',
            (event) => true,
            (event) => {
              console.dir(event)
            }
          )
          .then((result) => { subscription = result; })

once

eventHub.once(contractName, contractAddress, eventName, filterFunction, onEvent, fromBlock);

subscribe to a contract event or a global EventHub event, remove subscription when filterFunction matched

Parameters

  1. toRemove - any:
  2. contractAddress - string: target contract address
  3. eventName - string: name of the event to subscribe to
  4. filterFunction - function: a function that returns true or a Promise that resolves to true if onEvent function should be applied
  5. onEvent - function: executed when event was fired and the filter matches, gets the event as its parameter
  6. fromBlock - string (optional): get all events from this block, defaults to latest

Returns

Promise resolves to string: event subscription.

Example

// subscribe to the 'ContractEvent' at the EventHub located at '00000000000000000000000000000000deadbeef'
runtime.eventHub
          .once(
            'EventHub',
            '00000000000000000000000000000000deadbeef',
            'ContractEvent',
            (event) => true,
            (event) => {
              console.dir(event)
            }
          )
          .then((result) => { subscription = result; })

unsubscribe

eventHub.unsubscribe(toRemove);

unsubscribe an event subscription

Parameters

  1. contractName - string: target contract name (must be available within ContractLoader )
    • subscription - string: target guid for the subscription that should be removed
    • contractId - string: target contractId where all subscriptions should be removed (can be ‘all’)

Returns

Promise resolves to void: resolved when done.

Example

// subscribe to the 'ContractEvent' at the EventHub located at '00000000000000000000000000000000deadbeef'
runtime.eventHub
          .unsubscribe({
            subscription: 'f0315d39-5e03-4e82-b765-df1c03037b3a'
          })