Edmund Edgar
Edmund Edgar

Reputation: 124

How do I get event log information from pyethereum?

I've got some tests that create contracts with pyethereum and do various things with them, but I'm puzzled over how to get information about events they log.

A simplified example:

from ethereum import tester as t

s = t.state()
code = """contract LogTest {
            event LogMyNumber(uint);

            function LogTest() {
            }

            function logSomething() {
                LogMyNumber(4); 
            }
        }"""

logtest = t.state().abi_contract(code, language='solidity', sender=t.k0)
logtest.logSomething()

#number_i_logged = WHAT DO I DO HERE?
#print "You logged the number %d" % (number_i_logged)

I run this and get:

No handlers could be found for logger "eth.pow"
{'': 4, '_event_type': 'LogMyNumber'}

That json that's getting printed is the information I want, but can someone explain, or point me to an example, of how I might capture it and load it into a variable in python so that I can check it and do something with it? There seems to be something called log_listener that you can pass into abi_contract that looks like it's related but I couldn't figure out what to do with it.

Upvotes: 2

Views: 410

Answers (1)

4gn3s
4gn3s

Reputation: 218

I know you've been waiting for an answer for quite a long time, but in case someone else is wondering, here is goes: log_listeners you mentioned is the way to go. You can find some sample code using it in pyethereum's tests, and here is your fixed code:

from ethereum import tester as t

s = t.state()
code = """contract LogTest {
            event LogMyNumber(uint loggedNumber);

            function LogTest() {
            }

            function logSomething() {
                LogMyNumber(4);
            }
        }"""

c = s.abi_contract(code, language='solidity', sender=t.k0)
o = []
s.block.log_listeners.append(lambda x: o.append(c._translator.listen(x)))

c.logSomething()

assert len(o) == 1
assert o == [{"_event_type": 'LogMyNumber', "loggedNumber": 4}]

number_i_logged = o[0]["loggedNumber"]
print "You logged the number %d" % (number_i_logged)

Upvotes: 3

Related Questions