Skip to content

Commit

Permalink
Merge pull request #25 from Akkarine/master
Browse files Browse the repository at this point in the history
Add additional log entries to decode_log/s (#24)
  • Loading branch information
iamdefinitelyahuman authored Jan 31, 2024
2 parents 4946980 + eac58bd commit 1482c8e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ All notable changes to this project are documented in this file.
This changelog format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/iamdefinitelyahuman/eth-event)
## [Unreleased](https://github.com/iamdefinitelyahuman/eth-event)\
### Added
- Include `logIndex`, `blockNumber`, `transactionIndex` in decoded event logs

## [1.2.3](https://github.com/iamdefinitelyahuman/eth-event/releases/tag/v1.2.3) - 2021-04-16
### Fixed
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ The public API is well documented within the docstrings. The following example m
{'name': 'to', 'type': 'address', 'value': '0xbd4940951bfa463f8fb6db762e55686f6cfdb73a', 'decoded': True},
{'name': 'tokens', 'type': 'uint256', 'value': 100, 'decoded': True}
],
'logIndex': 0,
'blockNumber': 0,
'transactionIndex': 0
}]

# decoding a structLog from Geth's debug_traceTransaction endpoint
Expand Down
24 changes: 22 additions & 2 deletions eth_event/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class UnknownEvent(Exception):
pass


ADD_LOG_ENTRIES = ['logIndex', 'blockNumber', 'transactionIndex']


def get_log_topic(event_abi: Dict) -> str:
"""
Generate an encoded event topic for an event.
Expand Down Expand Up @@ -107,6 +110,9 @@ def decode_log(log: Dict, topic_map: Dict) -> Dict:
}, ...]
}
And additional entries: 'logIndex', 'blockNumber', 'transactionIndex', if
they are present in log.
Arguments
---------
log : Dict
Expand All @@ -128,12 +134,14 @@ def decode_log(log: Dict, topic_map: Dict) -> Dict:
abi = topic_map[key]

try:
return {
event = {
"name": abi["name"],
"data": _decode(abi["inputs"], log["topics"][1:], log["data"]),
"decoded": True,
"address": to_checksum_address(log["address"]),
}
event = append_additional_log_data(log, event, ADD_LOG_ENTRIES)
return event
except (KeyError, TypeError):
raise EventError("Invalid event")

Expand All @@ -153,6 +161,9 @@ def decode_logs(logs: List, topic_map: Dict, allow_undecoded: bool = False) -> L
'address: "", # address where the event was emitted
}
And additional entries: 'logIndex', 'blockNumber', 'transactionIndex', if
they are present in log.
Arguments
---------
logs : List
Expand Down Expand Up @@ -183,15 +194,24 @@ def decode_logs(logs: List, topic_map: Dict, allow_undecoded: bool = False) -> L
"decoded": False,
"address": to_checksum_address(item["address"]),
}
event = append_additional_log_data(item, event, ADD_LOG_ENTRIES)
else:
event = decode_log(item, topic_map)

events.append(event)

return events


def append_additional_log_data(log: Dict, event: Dict, log_entries: List[str]):
for log_entry in log_entries:
if log_entry in log:
event[log_entry] = log[log_entry]
return event


def decode_traceTransaction(
struct_logs: List, topic_map: Dict, allow_undecoded: bool = False, initial_address: str = None
struct_logs: List, topic_map: Dict, allow_undecoded: bool = False, initial_address: str = None
) -> List:
"""
Extract and decode a list of event logs from a transaction traceback.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_decode_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def test_decode_logs_no_topic(anon_a_log, topic_map):
"data": "0x00000000000000000000000066ab6d9362d4f35596279692f0251db635165871",
"topics": [],
"decoded": False,
"logIndex": 0,
"blockNumber": 2,
"transactionIndex": 0,
}
]

Expand Down

0 comments on commit 1482c8e

Please sign in to comment.