event_reader.logresult

Documentation for eth_defi.event_reader.logresult Python module.

Event reader output types and decoding.

Functions

decode_log(evt)

Decodes a single raw log result using the attached ABI.

Classes

LogContext

An abstract context class you can pass around for the log results.

LogResult

One emitted Solidity event.

class LogContext

Bases: object

An abstract context class you can pass around for the log results.

Subclass this and add your own data / methods.

See scripts/read-uniswap-v2-pairs-and-swaps.py for an example.

class LogResult

Bases: TypedDict

One emitted Solidity event.

  • Type mappings for a raw Python dict object

  • Designed for high performance at the cost of readability and usability

  • The values are untranslated hex strings to maximize the reading speed of events

  • See decode_event() how to turn to ABI converted data

  • See eth_defi.event_reader.reader for more information

Example data (PancakeSwap swap):

{
    'address': '0xc91cd2b9c9aafe494cf3ccc8bee7795deb17231a',
    'blockHash': '0x3bc60abea8fca30516f48b0374542b9c8fa554061c8802d7bcd4211fffbf6caf',
    'blockNumber': 30237147,
    'chunk_id': 30237147,
    'context': None,
    'data': '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de90d34e1f2e65c0000000000000000000000000000000000000000000018e627902bfb974416f90000000000000000000000000000000000000000000000000000000000000000',
    'event': <class 'web3._utils.datatypes.Swap'>,
    'logIndex': '0x3',
    'removed': False,
    'timestamp': 1690184818,
    'topics': ['0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822',
            '0x00000000000000000000000064c425b6fa04873ea460cda049b340d79cf859d7',
            '0x000000000000000000000000ad1fedfb04377c4b849cef6ef9627bca41955fa0'],
    'transactionHash': '0xf2287653559f01d8afba9ae00386d453b731699b784851f7a8504d41dee7503b',
    'transactionIndex': '0x1'
}
context: eth_defi.event_reader.logresult.LogContext

User passed context for the event reader

event: web3.contract.contract.ContractEvent

Contract event matches for this raw log

To use web3.py helpers to decode this log.

This event instance is just a class reference and does not contain any bound data.

address: str

Smart contract address

blockHash: str

Block where the event was

blockNumber: int

Block number as hex string

timestamp: Optional[int]

UNIX timestamp of the block number. Synthesized by block reader code, not present in the receipt. May be None if timestamp fetching is disabled for the speed reasons.

transactionHash: str

Transaction where the event occred

logIndex: str

Log index as a hex number

topics: List[str]

Topics in this receipt. topics[0] is always the event signature.

TODO: Whether these are strings or HexBytes depends on the EVM backend and Web3 version. Resolve this so that results are normalised to one type.

See eth_defi.reader.conversion how to get Python values out of this.

removed: bool

Block reorg helper

data: str

Data related to the event

As raw hex dump from the JSON-RPC.

See eth_defi.reader.conversion.decode_data() to split to args.

__init__(*args, **kwargs)
__new__(**kwargs)
clear() None.  Remove all items from D.
copy() a shallow copy of D
fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E is present and has a .keys() method, then does: for k in E.keys(): D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values
decode_log(evt)

Decodes a single raw log result using the attached ABI.

See LogResult for more information.

Example usage:

# Decode a Swap event from PancakeSwap
decoded = decode_log(evt)

Example output - see decoded ‘args’:

Parameters

evt (eth_defi.event_reader.logresult.LogResult) – A raw log event from the event reader.

Returns

Human-readable dict.

Return type

web3.types.EventData