<jupyter_start><jupyter_text>Event Monitor Tutorial  Monitor StructureDescription hereEvent Monitor* start_monitor* stop_monitor* get_monitor_status* monitorEvent Sync* start_sync* stop_sync* watch_sync* get_sync_status* syncEvent Handler* start_event_handler* stop_event_handler* get_event_handler_status* event_handler* handle_eventHelpers* get_events_df* get_events_list* get_queue_size* get_contract_events  Setup The event monitor requires a blockexplorer for initialization. The purpose of this blockexplorer is to get the contract source code so that contract interactions (Data & Logs) can be decoded into human readable events.<jupyter_code>from messari.blockexplorers import Etherscan

API_KEY = 'YOUR_API_KEY'
ES = Etherscan(api_key=API_KEY)<jupyter_output><empty_output><jupyter_text>The event monitor also requires the endpoint for an RPC. This should belong to the same network as the blockexplorer.<jupyter_code>from messari.eventmonitor import EventMonitor

rpc_url = 'YOUR_RPC_URL'<jupyter_output><empty_output><jupyter_text>Finally the event monitor must be initialized with the adress to monitor for events.  NOTE: The source code for this contract must be verified on the selected blockexplorer or else the event monitor will not be able to decode events for the given contract<jupyter_code>DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F'<jupyter_output><empty_output><jupyter_text>Initalize with contract(s), blockexplorer, & RPC<jupyter_code>dai_monitor = EventMonitor(DAI, ES, rpc_url)<jupyter_output><empty_output><jupyter_text>Event Monitor  start_monitor Start the event monitor in a dedicated thread. The monitor thread will continue to run until `stop_monitor()` is called. The monitor thread can be accessed with `EventMonitor.monitor_thread`.<jupyter_code>dai_monitor.start_monitor()<jupyter_output><empty_output><jupyter_text>stop_monitor Stop the event monitor thread. This function will set an internal flag in the `EventMonitor` which tells the monitor thread to exit its loop. The monitor thread will not stop until it finishes the current loop and reaches the top again.<jupyter_code>dai_monitor.stop_monitor()<jupyter_output><empty_output><jupyter_text>get_monitor_status Returns the operating status of the event monitor- RUNNING: event monitor is running- STOPPING: event monitor is stopping. If the monitor takes too long to stop then something is likely wrong- CRASH: event monitor thread crashed.- DEAD: event monitor is dead/stopped.<jupyter_code>monitor_status = dai_monitor.get_monitor_status()
monitor_status<jupyter_output><empty_output><jupyter_text>monitor This function runs the main loop for live event monitoringWARNING: It is highly recomended that you only interact with this function through the `start_monitor()` and `stop_monitor()` functions, not directly. However if you are inclined, feel free to read the source code for the EventMonitor class and experiment with this function in your own flow/script.<jupyter_code># dai_monitor.monitor()<jupyter_output><empty_output><jupyter_text>Event Sync  start_sync Start the event sync in a dedicated thread. The sync thread will find and submit all events in the given block range to the event handler. The sync thread will continue to run until `stop_sync()` is called or the sync is finished. The sync thread can be accessed with `self.sync_thread`. The parameters for this function are:* start: starting block, default is 0* end: ending block, default is 'latest'<jupyter_code>dai_monitor.start_sync()<jupyter_output><empty_output><jupyter_text>stop_sync Stop the event sync thread. This function will set an internal flag in the `EventMonitor` which tells the sync thread to exit its loop. The sync thread will not stop until it finishes the current loop and reaches the top again.<jupyter_code>dai_monitor.stop_sync()<jupyter_output><empty_output><jupyter_text>watch_sync This function can be used to watch the status of any given sync. Every 10 seconds this function will print an update on the progress of a sync.NOTE: This is a a blocking function meaning it will be run on the main thread until concluding. ie if there is 10 minutes left in a sync and you call this function, you'll be watching this function for the next 10 minutes<jupyter_code>dai_monitor.watch_sync()<jupyter_output>Contract: 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5, Topic: 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f, Range: (5697662 - 10144606), Goal: 13896705, Increment: 4446944
Contract: 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5, Topic: 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f, Range: (7921135 - 9022420), Goal: 13896705, Increment: 1101285
Contract: 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5, Topic: 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f, Range: (7921135 - 8471777), Goal: 13896705, Increment: 550642
Contract: 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5, Topic: 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f, Range: (8471778 - 8976000), Goal: 13896705, Increment: 504222
Contract: 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5, Topic: 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f, Range: (8976001 - 9412808), Goal: 13896705, Increment: 436807
Contract: 0x4Ddc2D193948926D02f9B1fE9e1daa[...]<jupyter_text>get_sync_status Returns the operating status of the event sync- RUNNING: event sync is running- STOPPING: event sync is stopping. If the sync takes too long to stop then something is likely wrong- CRASH: event sync thread crashed.- DEAD: event sync is dead/stopped.<jupyter_code>sync_status = dai_monitor.get_sync_status()
sync_status<jupyter_output><empty_output><jupyter_text>sync This function runs the main loop for syncing WARNING: It is highly recomended that you only interact with this function through the `start_sync()` and `stop_sync()` functions, not directly. However if you are inclined, feel free to read the source code for the EventMonitor class and experiment with this function in your own flow/script.<jupyter_code># dai_monitor.sync(0, 100)<jupyter_output><empty_output><jupyter_text>Event Handler  start_event_handler Start the event handler in a dedicated thread. The handler thread will continue to run until `stop_handler()` is called. The handler thread can be accessed with `self.handler_thread`. NOTE: this is automatically called with the `start_monitor()` and `start_sync()` functions. You should not have to call this function<jupyter_code>dai_monitor.start_event_handler()<jupyter_output><empty_output><jupyter_text>stop_event_handler Stop the event handler thread. This function will set an internal flag in the `EventMonitor` which tells the handler thread to exit its loop. The sync thread will not stop until it finishes the current loop and reaches the top again. NOTE: The event handler thread will run until all events have been handled, you should be able to let this thread run until completion<jupyter_code>dai_monitor.stop_event_handler()<jupyter_output><empty_output><jupyter_text>get_event_handler_status Returns the operating status of the event handler - RUNNING: event handler is running- STOPPING: event handler is stopping. If the handler takes too long to stop then something is likely wrong- CRASH: event handler thread crashed.- DEAD: event handler is dead/stopped.<jupyter_code>event_handler_status = dai_monitor.get_event_handler_status()
event_handler_status<jupyter_output><empty_output><jupyter_text>event_handler This function runs the main loop for event handling WARNING: It is highly recomended that you only interact with this function through the `start_event_handler()` and `stop_event_handler()` functions, not directly. However if you are inclined, feel free to read the source code for the EventMonitor class and experiment with this function in your own flow/script.<jupyter_code># dai_monitor.event_handler()<jupyter_output><empty_output><jupyter_text>handle_event WARNING: It is highly recomended that you do not interact with this function. However if you so chose, this function could be overridden to define custom behavior for handling events NOTE: expecting returns from functions called on a w3.eth.filter class<jupyter_code># dai_monitor.handle_event(event)<jupyter_output><empty_output><jupyter_text>Helpers  get_events_df Returns processed events as a DataFrame<jupyter_code>events_df = dai_monitor.get_events_df()
events_df<jupyter_output><empty_output><jupyter_text>get_events_list Returns process events as a List<jupyter_code>events_list = dai_monitor.get_events_list()
events_list[:2]<jupyter_output><empty_output><jupyter_text>get_queue_size Returns the size of the event handler queue. This number represents the amounts of unprocessed events which will be processed so long as `EventMonitor.get_handler_status() == 'RUNNING'`<jupyter_code>event_queue_size = dai_monitor.get_queue_size()
event_queue_size<jupyter_output><empty_output><jupyter_text>get_contract_events This function is used internally for mapping ethereum event topics (HEX) to human readable event names. It can be interesting to see the kinds of events for contracts used to initalize the `EventMonitor` class<jupyter_code>contract_events = dai_monitor.get_contract_events()
contract_events<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05755782678859602, 'fraction_numerical': 0.05766541151156536, 'mean_word_length': 7.405063291139241, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '257737', 'license': 'MIT', 'n_tokens_mistral': 3129, 'n_tokens_neox': 2813, 'n_words': 978, 'path': 'docs/build/doctrees/nbsphinx/notebooks/Event Monitor Tutorial.ipynb', 'repo_name': 'messari/messari-python-api-private', 'size': '9295'}