ape icon indicating copy to clipboard operation
ape copied to clipboard

Print event logs in a formatted way

Open Aviksaikat opened this issue 1 year ago • 2 comments

Overview

Thanks to salparadi at discord for providing the following code.

from rich.tree import Tree
from rich.text import Text
from rich import print as rich_print
from ape.utils.trace import TraceStyles
            
def add_event_to_tree(event, tree: Tree):
    contract = ape.chain.contracts.get(event.contract_address)
    contract_name = getattr(contract, 'name', None)
    contract_address_styled = Text(event.contract_address, style="#D17706 italic")
    
    if contract_name:
        contract_name_styled = Text(contract_name, style=TraceStyles.CONTRACTS)
        contract_tree = tree.add(contract_name_styled + ' ' + contract_address_styled)
    else:
        contract_tree = tree.add(contract_address_styled)

    event_name_styled = Text(event.event_name)
    event_tree = contract_tree.add(event_name_styled)

    event_arguments = event.event_arguments.items()
    for arg, value in event_arguments:
        if isinstance(value, bytes):
            value = value.hex()
        
        event_arg_styled = Text(arg)
        event_value_styled = Text(str(value), style=TraceStyles.VALUE)
        event_tree.add(event_arg_styled + ': ' + event_value_styled)

tree = Tree("Transaction Events for " + tx.txn_hash)

for event in tx.events:
    add_event_to_tree(event, tree)

rich_print(tree)

Dependencies

  • Rich

Aviksaikat avatar Dec 16 '23 19:12 Aviksaikat

this could probably be added to ReceiptAPI.show_trace()

fubuloubu avatar Dec 17 '23 01:12 fubuloubu

this could probably be added to ReceiptAPI.show_trace()

we need to adjust evm-trace so that events show up in the right spot in the call tree. also we have this tracked here https://github.com/ApeWorX/ape/issues/834

for this issue, we could add a receipts.show_events() method or something...

antazoey avatar Jun 24 '24 18:06 antazoey