Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web3j: How to get Ethereum transaction information (Java not JS)

Tags:

I've just started using Web3j and am having some basic trouble.

I've successfully figured out how to get an EthBlock and retrieve all of the information inside of it. I'd like to see the list of transactions in the block, but I can't figure out how.

I can call

 List<TransactionResult> transactions = ethBlock.getBlock().getTransactions();

I should be able to look through this list and get information about each transaction. But all I can seem to do with a TransactionResult is cast it to the very unuseful TransactionHash. What I'd like is a TransactionObject from which I can extract lots of information.

How do I get at the real transaction data?

And on another note: is there any reason why there doesn't seem to be any Web3j JavaDoc??

like image 489
Sander Smith Avatar asked May 11 '18 20:05

Sander Smith


1 Answers

It's in there, it's just confusing on how to get to it because of how they used generics. Example below will output the sender of each transaction in the LATEST block:

List<EthBlock.TransactionResult> txs = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, true).send().getBlock().getTransactions();
txs.forEach(tx -> {
  EthBlock.TransactionObject transaction = (EthBlock.TransactionObject) tx.get();

  System.out.println(transaction.getFrom());
});

Keep in mind that this is the TransactionObject (the tx sent), not the resulting TransactionReceipt that contains the result of the tx being mined.

like image 74
Adam Kipnis Avatar answered Sep 28 '22 18:09

Adam Kipnis