Package: django-ethereum-events
Ethereum Contract Event Log monitoring in DjangoOverviewThis package provides an easy way to monitor an ethereum blockchain for transactions that invokeContract Eventsthat are of particular interest.The main concept was inspired by the following project:https://github.com/gnosis/django-eth-eventsPackage versions0.2.x+supportweb3 v4.If you want python 2.7 compatibility and/orweb3 v3support, use version0.1.xof this package.InstallationInstall using pip:pip install django-ethereum-eventsMake sure to include'django_ethereum_events'in yourINSTALLED_APPSINSTALLED_APPS+=('django_ethereum_events')if you are using theadmin backend, also includesoloin yourINSTALLED_APPS.Make necessary migrationspythonmanage.pymigratedjango_ethereum_eventsUsageIn yoursettingsfile, specify the following settingsETHEREUM_NODE_URI='http://localhost:8545'Create a new MonitoredEventcontract_abi="""
The whole contract abi goes here
"""event="MyEvent"# the emitted event nameevent_receiver="myapp.event_receivers.CustomEventReceiver"contract_address="0x10f683d9acc908cA6b7A34726271229B846b0292"# the address of the contract emitting the eventMonitoredEvent.object.register_event(event_name=event,contract_address=contract_address,contract_abi=contract_abi,event_receiver=event_receiver)Create an appropriate event receiverfromdjango_ethereum_events.chaineventsimportAbstractEventReceiverclassCustomEventReceiver(AbsractEventReceiver):defsave(self,decoded_event):# custom logic goes hereThedecoded_eventparameter is the decoded log as provided fromweb3.utils.events.get_event_datamethod.To start monitoring the blockchain, either run the celery taskdjango_ethereum_events.tasks.event_listeneror better, usecelerybeatto run it as a periodical taskfromcelery.beatimportcrontabCELERYBEAT_SCHEDULE={'ethereum_events':{'task':'django_ethereum_events.tasks.event_listener','schedule':crontab(minute='*/1')# run every minute}}You can also set the optionalETHEREUM_LOGS_BATCH_SIZEsetting which limits the maximum amount of the blocks that can be read at a time from the celery task.Using event filtersIf your Ethereum Node supports log filters, you can activate it in the Django settings and it will use filters instead of iterating thru all blocks and all transactions.ETHEREUM_LOGS_FILTER_AVAILABLE=TrueMore about the event receiversIt is advisable that the code inside the custom event receiver to be simple since it is run synchronously while theevent_listenertask is running. If that is not the case, pass the argumentdecoded_eventto a celery task of your own# inside the CustomEventReceiver.save methodfromdjango_ethereum_events.utilsimportHexJsonEncoderdecoded_event_data=json.dumps(decoded_event,cls=HexJsonEncoder)my_custom_task.delay(decoded_event_data)If an unhandled exception is raised inside the event receiver, theevent_listenertask logs the error and creates
a new instance of thedjango_ethereum_events.models.FailedEventLogcontaining all the relevant event information.The event listener doesnotattempt to rerunFailedEventLogs. That is up to the client implementation.Resetting the internal stateBlocks are processed only once. The last block processed is stored in the.models.Daemonentry.To reset the number of blocks processed, run thereset_block_daemoncommand optionally specifying the block number (-b, –block) to reset to (defaults to zero). If you reset it to zero, the next time theevent_listeneris fired, it will start processing blocks from the genesis block.TheDaemonentry can also be changed from the django admin backend.Proof-of-Authority NetworksTo use this package onRinkebyor any other private network that uses the Proof-of-Authority consensus engine (also named clique), set the optionalETHEREUM_GETH_POAsetting toTrue.