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¶
options-EventHubOptions: options for EventHub constructor.config-any: configuration object for the eventhub modulenameResolver-NameResolver:NameResolverinstancecontractLoader-ContractLoader:ContractLoaderinstanceeventWeb3-Web3(optional):Web3instance 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 withloglogLog-LogLogInterface(optional): container for collecting log messageslogLogLevel-LogLevel(optional): messages with this level will be pushed tologLog
Returns¶
EventHub instance
subscribe¶
eventHub.subscribe(contractName, contractAddress, eventName, filterFunction, onEvent, fromBlock);
subscribe to a contract event or a global EventHub event
Parameters¶
contractName-string: target contract name (must be available withinContractLoader)contractAddress-string: target contract addresseventName-string: name of the event to subscribe tofilterFunction-function: a function that returns true or a Promise that resolves to true if onEvent function should be appliedonEvent-function: executed when event was fired and the filter matches, gets the event as its parameterfromBlock-string(optional): get all events from this block, defaults tolatest
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¶
toRemove-any:contractAddress-string: target contract addresseventName-string: name of the event to subscribe tofilterFunction-function: a function that returns true or a Promise that resolves to true if onEvent function should be appliedonEvent-function: executed when event was fired and the filter matches, gets the event as its parameterfromBlock-string(optional): get all events from this block, defaults tolatest
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¶
contractName-string: target contract name (must be available withinContractLoader)subscription-string: target guid for the subscription that should be removedcontractId-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'
})