Skip to main content

Ethereum event decoder and topic generator

Project description

eth-event

Simple tools for Ethereum event decoding and topic generation.

Installation

pip install eth-event

Usage

The package includes five functions:

  • get_topics(abi): Given a contract ABI, returns a dictionary of {'event name': "encrypted topic"}.
>>> import eth_event

>>> abi = [{'name': 'Approval', 'anonymous': False, 'type': 'event', 'inputs': [{'name': 'owner', 'type': 'address', 'indexed': True}, {'name': 'spender', 'type': 'address', 'indexed': True}, {'name': 'value', 'type': 'uint256', 'indexed': False}]}, {'name': 'Transfer', 'anonymous': False, 'type': 'event', 'inputs': [{'name': 'from', 'type': 'address', 'indexed': True}, {'name': 'to', 'type': 'address', 'indexed': True}, {'name': 'value', 'type': 'uint256', 'indexed': False}]}]

>>> eth_event.get_topics(abi)
{'Transfer': '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', 'Approval': '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925'}
  • get_event_abi(abi): Given a contract ABI, returns a dictionary of {'encrypted topic': "ABI"}. Useful for stripping an ABI so that only event related data remains.
>>> event_abi = eth_event.get_event_abi(abi)
{'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef': {'name': 'Transfer', 'inputs': [{'name': 'from', 'type': 'address', 'indexed': True}, {'name': 'to', 'type': 'address', 'indexed': True}, {'name': 'value', 'type': 'uint256', 'indexed': False}]}, '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925': {'name': 'Approval', 'inputs': [{'name': 'owner', 'type': 'address', 'indexed': True}, {'name': 'spender', 'type': 'address', 'indexed': True}, {'name': 'value', 'type': 'uint256', 'indexed': False}]}}
  • decode_event(event, abi): Given a single event from a transaction log and an ABI, returns the decoded event. The ABI may supplied in the normal contract format, or as a dictionary value generated by get_event_abi
>>> tx = token.transfer(account[1], 100, {'from': account[0]})
<Transaction object '0xd0b9f575747eafdce3ba6ee8d6c16146bfff35cb86bec0a1909ab04fa94fc024'>

>>> log = tx.logs[0]
>>> eth_event.decode_event(log, token.abi)
{'name': 'Transfer', 'data': [{'name': 'from', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a'}, {'name': 'to', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True}, {'name': 'tokens', 'type': 'uint256', 'value': 100, 'decoded': True}]}

>>> eth_event.decode_event(log, event_abi[log['topics'][0]])
{'name': 'Transfer', 'data': [{'name': 'from', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a'}, {'name': 'to', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True}, {'name': 'tokens', 'type': 'uint256', 'value': 100, 'decoded': True}]}
  • decode_logs(logs, abi): Given an entire transaction log and an ABI, returns the decoded events. The ABI may supplied in the normal contract format, or as a dictionary value generated by get_event_abi
>>> tx = token.transfer(account[1], 100, {'from': account[0]})
<Transaction object '0x615a157e84715d5f960a38fe2a3ddb566c8393cfc71f15b06170a0eff74dfdde'>

>>> eth_event.decode_event(tx.logs, token.abi)
[{'name': 'Transfer', 'data': [{'name': 'from', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a'}, {'name': 'to', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True}, {'name': 'tokens', 'type': 'uint256', 'value': 100, 'decoded': True}]}]
  • decode_trace(trace, abi): Given the structLog from a debug_traceTransaction RPC call, returns a list of the decoded events. Useful for obtaining events when a transaction reverts.
>>> tx = token.transfer(account[1], 100, {'from': account[0]})
<Transaction object '0x71a7146996957b8a465a4e5dd41951d614254f8271fc9140b875c4fc55dde578'>

>>> trace = web3.providers[0].make_request("debug_traceTransaction", ['0x71a7146996957b8a465a4e5dd41951d614254f8271fc9140b875c4fc55dde578', {}])['result']['structLogs]
]
>>> eth_event.decode_trace(trace, token.abi)
[{'name': 'Transfer', 'data': [{'name': 'from', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a'}, {'name': 'to', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True}, {'name': 'tokens', 'type': 'uint256', 'value': 100, 'decoded': True}]}]

Limitations

If an array is indexed in an event, the topic is generated as a sha3 hash and so cannot be decrypted. In this case, the unencrypted topcic is returned and decoded is set to False.

Development

This project is still in development and should be considered an alpha. Comments, questions, criticisms and pull requests are welcomed.

License

This project is licensed under the MIT license.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

eth_event-0.1.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file eth_event-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: eth_event-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

File hashes

Hashes for eth_event-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 165676e949859afa83ea634b59b7f4f900bf88d6cf387b04b667daddf4828dec
MD5 d63c250dd308360d237a44ae2dd9eacc
BLAKE2b-256 d6d75002830f2eeb48c326057421c2431ed19dcba5e7fc3d3af79b83c7abb45a

See more details on using hashes here.

Supported by

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