6

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??

Sander Smith
  • 1,371
  • 4
  • 20
  • 30

1 Answers1

6

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.

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • 2
    Thanks, this was a big help. Turns out I understood the generics correctly. What I didn't do is pass in true to ethGetBlockByNumber. The code I copied had the parm set to false - I guess that made all of the difference. – Sander Smith May 11 '18 at 23:06
  • 3
    How do I get the list of all `TransactionReceipt` per block? – db80 Oct 03 '18 at 21:07
  • 1
    {yourEthBlock}.getBlock().getTransactions().forEach( transactionResult -> {{ OptionaltransactionReceipt = web3j.ethGetTransactionReceipt(transactionResult.get().toString()).send().getTransactionReceipt(); }}); – user2606235 Jun 02 '22 at 02:40