Skip to content

Commit

Permalink
update event tests to use actual contract event emissions
Browse files Browse the repository at this point in the history
  • Loading branch information
kibagateaux committed Jul 5, 2023
1 parent 1a2fc06 commit 0798330
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 55 deletions.
13 changes: 6 additions & 7 deletions boa/vyper/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import vyper.semantics.namespace as vy_ns
from eth.codecs import abi
from eth.exceptions import VMError
from eth_abi.grammar import TupleType
from eth_typing import Address
from eth_utils import to_canonical_address, to_checksum_address
from vyper.ast.utils import parse_to_ast
Expand Down Expand Up @@ -653,13 +652,15 @@ def decode_log(self, e):
abi.decode(typ.abi_type.selector_name(), encoded_topic)
)

tuple_typ = TupleType(arg_typs)
tuple_typ = TupleT(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
print("exampe event_t", event_t.__dict__)
print("exampe args/params", args, address, event_hash)
decoded_values = []
for is_topic, k in zip(event_t.indexed, event_t.arguments.keys()):
if is_topic:
Expand All @@ -669,11 +670,9 @@ def decode_log(self, e):
decoded_values.append((k, args[a_i]))
a_i += 1

event_name = ", ".join(f"{k}={v}" for k, v in decoded_values)
args_map = dict([(k, v) for k, v in decoded_values])
return Event(
log_id, self.address, event_t, event_name, decoded_topics, args, args_map
)
print("decoded topics", decoded_topics)
print("decoded args", 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
Expand Down
4 changes: 2 additions & 2 deletions boa/vyper/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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_type: Any # vyper.semantics.types.user.EventT
topics: List[Any] # list of decoded topics
args: List[Any] # list of decoded args

Expand All @@ -26,7 +26,7 @@ def ordered_args(self) -> List[Tuple[str, str | int]]:
# 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()
self.event_type.indexed, self.event_type.arguments.keys()
):
if is_topic:
b.append((k, self.topics[t_i]))
Expand Down
66 changes: 20 additions & 46 deletions tests/unitary/test_event.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,43 @@
from boa.interpret import load
from boa.vyper.event import Event

param_types = "bool,uint256,address,bytes32".split(",")
event_type = {
"indexed": [False, False, False, False],
"arguments": {idx: value for idx, value in enumerate(param_types)},
}
me = "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"
token = load("examples/ERC20.vy", "Test", "TEST", 18, 0)
token.mint(me, 100)
event = token.get_logs()[0]


def test_topics_saved_to_event():
event_type["indexed"] = [True, True, True, True]
event = Event("0x0", "0x0", event_type, param_types, [])
assert event.topics == param_types
assert event.args == []
print(Event)
assert len(event.ordered_args()) == len(param_types)
assert len(event.topics) == 2
assert event.topics[0] == "0x0000000000000000000000000000000000000000"
assert event.topics[1] == me


def test_args_saved_to_event():
event_type["indexed"] = [False, False, False, False]
event = Event("0x0", "0x0", event_type, [], param_types)
assert event.args == param_types
assert event.topics == []
assert len(event.ordered_args()) == len(param_types)
assert len(event.args) == 1
assert event.args[0] == 100


def test_args_ordered_to_event_param_sequence():
# separate indexed from non-indexed in Event state
# to make sure they get merged properly
event_type["indexed"] = [False, True, True, False]
topics = [param_types[1], param_types[2]]
args = [param_types[0], param_types[3]]
args = event.ordered_args()
assert args[0][0] == "sender"
assert args[1][0] == "receiver"
assert args[2][0] == "value"

event = Event("0x0", "0x0", event_type, topics, args)
assert event.args == args
assert event.topics == topics
assert len(event.ordered_args()) == len(param_types)

orderSequence = list(event_type["arguments"].keys())
for idx, [key, value] in enumerate(event.ordered_args()):
assert key == orderSequence[idx]
assert value == event_type["arguments"][key]
def test_args_map_match_event_params():
args = event.args_map
assert args["sender"] == "0x0000000000000000000000000000000000000000"
assert args["receiver"] == me
assert args["value"] == 100


def test_args_map_values_match_ordered_args():
event_type["indexed"] = [False, False, False, False]
event = Event("0x0", "0x0", event_type, [], param_types)
for idx, k in enumerate(event_type["arguments"].keys()):
assert event.args_map[k] == param_types[idx]


def test_event_is_emitted():
me = "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
token = load("examples/ERC20.vy", "test", "TEST", 18, 0)
token.mint(me, 100)
mint_events = token.get_logs()
assert len(mint_events) == 1
assert mint_events[0].args == (100)
assert mint_events[0].topics == ("0x0000000000000000000000000000000000000000", me)
assert mint_events[0].args_map == {
assert event.args_map == {
"sender": "0x0000000000000000000000000000000000000000",
"receiver": me,
"value": 100,
}
assert mint_events[0].ordered_args() == [
assert event.ordered_args() == [
("sender", "0x0000000000000000000000000000000000000000"),
("receiver", me),
("value", 100),
Expand Down

0 comments on commit 0798330

Please sign in to comment.