flow-scanner.md
November 22, 2021 ยท View on GitHub
FlowScanner Class Reference
The FlowScanner constructor takes two arguments:
constructor (private readonly eventTypes: string[], providers: ProviderOptions)
eventTypes is an array of the Cadence event types that the FlowScanner should monitor, and providers is an object containing functions that will be used to create any classes/functions that FlowScanner depends on. the ProviderOptions is defined as:
type ProviderOptions = {
settingsServiceProvider: SettingsServiceProvider
eventBroadcasterProvider: EventBroadcasterProvider
metricServiceProvider?: MetricServiceProvider
logProvider?: LogProvider
flowServiceProvider?: FlowServiceProvider
eventBusProvider?: EventBusProvider
configProvider: ConfigProvider
}
SettingsServiceProvider will be used to retrieve/store data that should be persisted to storage. This includes the currently processed block height (how far the scanner has monitored events on the blockchain). You can read more about it in the SettingsService Class Reference
EventBroadcasterProvider will be used to create the class responsible for broadcasting events. You can read more about it in the EventBroadcaster Class Reference
MetricServiceProvider will be used to create the class responsible for reporting metrics to an external metric service. You can read more about it in the MetricService Class Reference
LogProvider will be used to create the logging service. You can read more about it in the Logger Class Reference
FlowServiceProvider will be used to create the service that is used to communicate with the Flow network. You can learn more about it in the FlowService Class Reference
EventBusProvider will be used to create the event bus used for communication between the different components of the Flow Scanner. You can learn more about it in the EventBus Class Reference
ConfigProvider will be used to create the interface to access configuration values. You can read more about it in the Config Class Reference
FlowScanner Methods
start(): Promise<void>
The start() method is used to start the FlowScanner instance. It will create (and start) one BlockHeightScanner instance, and for each monitored event type it will create (and start) an EventScanner instance. It will also trigger the first run of the process() method, which will continually run itself using setTimeout() until the stop() method has been called to stop the FlowScanner.
It will also attempt to read the starting block height from the SettingsService to resume scanning after the last block that was processed. If the SettingsService does not provide a value of the last processed block, it will fall back to the DEFAULT_START_BLOCK_HEIGHT value provided by the ConfigProvider.
stop(): Promise<void>
The stop() method will stop the BlockHeightScanner and all EventScanner instances, and will set an internal flag so that the process() method will not schedule itself using setTimeout() on the next call.
private process(): Promise<void>
The process() method is called by start(), and then continually schedules itself using setTimeout() to continue processing until stop() has been called.
This method will check if all of the monitored event types are available for the next block that should be processed. If this is true, then all events for the block will be sent to the EventBroadcaster and the processed block height will be persisted to the SettingsService.
FlowScanner Events
FlowScanner listens for the following events on the EventBus:
FlowEventsFetched - When an EventScanner has retrieved events for a block, it will emit this event containing the block height and the events that were contained in that block. If no events were contained in the block, this event will still be emitted with an empty events array.
FlowScanner emits the following events on the EventBus:
ProcessedBlockHeightUpdated - When a block has been processed, this event is emitted so that the EventScanner instances will continue to read blocks from the Flow blockchain.