Skip to main content

Django Ethereum Events

Project description

Ethereum Contract Event Log monitoring in Django

https://travis-ci.org/artemistomaras/django-ethereum-events.svg?branch=master https://img.shields.io/pypi/v/django-ethereum-events.svg

Overview

This package provides an easy way to monitor an ethereum blockchain for transactions that invoke Contract Events that are of particular interest.

The main concept was inspired by the following project:

Package versions 0.2.x+ support web3 v4.

If you want python 2.7 compatibility and/or web3 v3 support, use version 0.1.x of this package.

Installation

  1. Install using pip:

    pip install django-ethereum-events
  2. Make sure to include 'django_ethereum_events' in your INSTALLED_APPS

    INSTALLED_APPS += ('django_ethereum_events')

    if you are using the admin backend, also include solo in your INSTALLED_APPS.

  3. Make necessary migrations

    python manage.py migrate django_ethereum_events

Usage

  1. In your settings file, specify the following settings

    ETHEREUM_NODE_URI = 'http://localhost:8545'
  2. Create a new MonitoredEvent

    contract_abi = """
    The whole contract abi goes here
    """
    
    event = "MyEvent"  # the emitted event name
    event_receiver = "myapp.event_receivers.CustomEventReceiver"
    contract_address = "0x10f683d9acc908cA6b7A34726271229B846b0292"  # the address of the contract emitting the event
    
    MonitoredEvent.object.register_event(
        event_name=event,
        contract_address=contract_address,
        contract_abi=contract_abi,
        event_receiver=event_receiver
    )
  3. Create an appropriate event receiver

    from django_ethereum_events.chainevents import AbstractEventReceiver
    
    class CustomEventReceiver(AbsractEventReceiver):
        def save(self, decoded_event):
            # custom logic goes here

    The decoded_event parameter is the decoded log as provided from web3.utils.events.get_event_data method.

  4. To start monitoring the blockchain, either run the celery task django_ethereum_events.tasks.event_listener or better, use celerybeat to run it as a periodical task

    from celery.beat import crontab
    
    CELERYBEAT_SCHEDULE = {
    'ethereum_events': {
        'task': 'django_ethereum_events.tasks.event_listener',
        'schedule': crontab(minute='*/1')  # run every minute
        }
    }

    You can also set the optional ETHEREUM_LOGS_BATCH_SIZE setting which limits the maximum amount of the blocks that can be read at a time from the celery task.

More about the event receivers

It is advisable that the code inside the custom event receiver to be simple since it is run synchronously while the event_listener task is running. If that is not the case, pass the argument decoded_event to a celery task of your own

# inside the CustomEventReceiver.save method
from django_ethereum_events.utils import HexJsonEncoder
decoded_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, the event_listener task logs the error and creates a new instance of the django_ethereum_events.models.FailedEventLog containing all the relevant event information.

The event listener does not attempt to rerun FailedEventLogs. That is up to the client implementation.

Resetting the internal state

Blocks are processed only once. The last block processed is stored in the .models.Daemon entry.

To reset the number of blocks processed, run the reset_block_daemon command optionally specifying the block number (-b, –block) to reset to (defaults to zero). If you reset it to zero, the next time the event_listener is fired, it will start processing blocks from the genesis block.

The Daemon entry can also be changed from the django admin backend.

Proof-of-Authority Networks

To use this package on Rinkeby or any other private network that uses the Proof-of-Authority consensus engine (also named clique), set the optional ETHEREUM_GETH_POA setting to True.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ethereum-events-3.0.1.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ethereum_events-3.0.1-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file django-ethereum-events-3.0.1.tar.gz.

File metadata

  • Download URL: django-ethereum-events-3.0.1.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for django-ethereum-events-3.0.1.tar.gz
Algorithm Hash digest
SHA256 e79c99407a32e27d6face8d1c576eaae6cd4012334ad0a5ef0a0ef46889b91bd
MD5 6eb2f402023c002bc0b6f2bc5043dc5a
BLAKE2b-256 349a4c357d83b5b4e33c87ff69dd06bac4299b80535a68078efcc1d083ee270a

See more details on using hashes here.

File details

Details for the file django_ethereum_events-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_ethereum_events-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for django_ethereum_events-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d392f9b401e7656a3440cc20cb5690181c8355b35ee58c8b5f27e2e09271f07
MD5 f0ef2f2e4abb62fa0a7e111337389c43
BLAKE2b-256 90e5f57874624e297a005ce015a6655d86d7495a6de9abd6694df9b8644efe25

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page