2

Accessing event logs from javascript is easy and straight forward, however I am trying to access the logs from the Java client. Any example code would help

1

You can use the JsonRpc class to do a filter on the logs of a block. You can get it from an instance of EthereumImpl like so:

JsonRpc jsonrpc = ethereum.getApplicationContext().getBean(JsonRpc.class);

You can then pass it a FilterRequest object to tell it what to search for:

FilterRequest fr = new FilterRequest();
fr.fromBlock = "latest";
fr.toBlock = "latest";
fr.address = ""; //String or String[] - addresses to match
fr.topics = ...//String or String[] of topics to match
               //Hash of event signature is first, then indexed parameters
               //I believe you can give it a String[][] also, so for each param in the event you can filter for multiple options
Object[] logs = jsonrpc.eth_getLogs(fr);
for(Object logObject : logs) {
    LogFilterElement log = (LogFilterElement)logObject;
    //do stuff with the log of the event
}

The LogFilterElement has the address, topics, and data from the event, but it's still in hex format, so you have to decode it. Indexed values from the event are only found in topics and variable length data that is indexed (like string or bytes) is only stored as a hash in the topics.

I wrote an EthereumEventManager class to handle this stuff for myself. You can take a look at it here. You can add listeners for filter requests, then it listens for new blocks and notifies when those filter requests have been found. Feel free to use it, modify it, or learn from it as you see fit. It's not well documented, but if you have any questions, I'd be happy to answer them.

  • how can we get the instance of EthereumImpl? – Abhishek Ranjan Aug 11 '16 at 7:37
1

You can listen to the onTransactionExecuted() from EthereumListner interface and use the TransactionExecutionSummary object to retrieve the log information, see Event tracking and decoding from ethereumj.

See the provided answer for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.