From 481b7f52c66403ac31c7989f7b0d3fa22bd81cb3 Mon Sep 17 00:00:00 2001 From: kibagateaux Date: Tue, 24 Jan 2023 02:46:17 -0500 Subject: [PATCH] refactor for less code change --- boa/vyper/contract.py | 19 ++----------------- boa/vyper/event.py | 27 ++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/boa/vyper/contract.py b/boa/vyper/contract.py index 728c23ee..7f958dfb 100644 --- a/boa/vyper/contract.py +++ b/boa/vyper/contract.py @@ -609,25 +609,10 @@ def decode_log(self, e): ) tuple_typ = TupleType(arg_typs) - args = abi_decode(tuple_typ.abi_type.selector_name(), data) - # align the evm topic + args lists with the way they appear in the source - # ex. Transfer(indexed address, address, indexed address) - t_i = 0 - a_i = 0 - decoded_values = [] - for is_topic, k in zip( - event_t.indexed, event_t.arguments.keys() - ): - if is_topic: - decoded_values.append((k, decoded_topics[t_i])) - t_i += 1 - else: - decoded_values.append((k, args[a_i])) - a_i += 1 + args = abi_decode(tuple_typ.abi_type.selector_name(), data) - event_name = ", ".join(f"{k}={v}" for k, v in decoded_values) - return Event(log_id, self.address, event_t, event_name, decoded_topics, args) + return Event(log_id, self.address, event_t, decoded_topics, args) def marshal_to_python(self, computation, vyper_typ): self._computation = computation # for further inspection diff --git a/boa/vyper/event.py b/boa/vyper/event.py index 6bde5328..31efc3a0 100644 --- a/boa/vyper/event.py +++ b/boa/vyper/event.py @@ -1,4 +1,5 @@ import functools +import re from dataclasses import dataclass from typing import Any, List, Dict @@ -9,17 +10,33 @@ class Event: log_id: int # internal py-evm log id, for ordering purposes address: str # checksum address event_type: Any # vyper.semantics.types.user.Event - event_name: str # human readable output topics: List[Any] # list of decoded topics args: List[Any] # list of decoded args def __repr__(self): - return self.event_name + t_i = 0 + a_i = 0 + b = [] + # align the evm topic + args lists with the way they appear in the source + # ex. Transfer(indexed address, address, indexed address) + for is_topic, k in zip( + self.event_type.indexed, self.event_type.arguments.keys() + ): + if is_topic: + b.append((k, self.topics[t_i])) + t_i += 1 + else: + b.append((k, self.args[a_i])) + a_i += 1 + + args = ", ".join(f"{k}={v}" for k, v in b) + return f"{self.event_type.name}({args})" @functools.cached_property - def args_map(self): - event_values = self.event_name.split(', ') - return dict([(topic.split('=')) for topic in event_values]) + def args_map(self) -> Dict[str, Any]: + # get values from inside event pareentheses + event_values = dict(re.findall(r"(\w+)=([\w\d]+)", self.__repr__())) + return event_values class RawEvent: event_data: Any \ No newline at end of file