0

I'm currently running a full node and am trying to loop through some transactions. Whenever I do so with Web3j and use the EthBlock.TransactionObject and I get to a transaction that contains a contract (Like this one) and use the .getTo() function it returns a null value. Is there any way for me to retrieve the contract address?

EDIT: This is the code im using to get the transaction

EthBlock.Block block = web3.ethGetBlockByNumber(DefaultBlockParameter.valueOf(BigInteger.valueOf(1397553)), true).send().getBlock();
List<EthBlock.TransactionResult> transactionResults = block.getTransactions();
transactionResults.forEach(tx -> {
   EthBlock.TransactionObject transaction = (EthBlock.TransactionObject) tx.get();
   System.out.println(transaction.getCreates());
});

This returns a "null"

TylerH
  • 20,799
  • 66
  • 75
  • 101
Rodarg
  • 11
  • 3

1 Answers1

0

When a transaction creates a contract, the to field is empty by design (of the Ethereum network).

To get the contract address that the transaction creates, use the getCreates() method.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • This also returned Null for me. both the getCreates and getTo return null. Is this maybe because the node is a full one and not an archive one? – Rodarg Apr 06 '21 at 15:55
  • I have updated the post so you can see the code i used – Rodarg Apr 06 '21 at 18:23
  • @Rodarg I'm not that well versed in the Java version of `web3` so I'm not able to debug it in more depth. In my experience, in the **JavaScript** `web3js` you'd use the property `contractAddress` that returns the contract address every time when a contract is created (and the `to` field is empty). But hopefully someone else is going to have a better answer. – Petr Hejda Apr 06 '21 at 22:05
  • One more idea. It is possible that the transaction has been reverted and no contract was created from this particular tx. So the `to` field is `Null`, but also no contract was created - which would possibly make the value of `creates` property `Null` as well. – Petr Hejda Apr 06 '21 at 22:14
  • Well looking at the transaction i linked it seems like the transaction is valid so dont really know what im doing wrong here – Rodarg Apr 06 '21 at 22:43