4

I know that in the transaction receipt I can get the contract address by getting a transaction receipt from a generated transaction.

But I am wondering since smart contract addresses are deterministic in some sense if there is a way to predict the address from the transaction its self, not the receipt.

Moe Elsharif
  • 482
  • 4
  • 10

3 Answers3

4

Ok, I actually found the answer my self sadly things are not updated in the documentation at least so far, however, there is a creates field in every transaction.

So when you, for example, do getBlock and get all blocks including transactions you can find if it was a contract creation transaction first by checking if to is zero which is mentioned in the documentation and creates as the address.

Moe Elsharif
  • 482
  • 4
  • 10
4

@Nulik has the answer that is the clearest.

The only caveat is the actual command is eth.getTransactionReceipt(hash).contractAddress

If you are using Web3, use web3.eth.getTransactionReceipt(hash).contractAddress.

(Info on how to use Web3 in Python)

Daniel Connelly
  • 383
  • 2
  • 9
  • 1
    The whole point is getting the address without getTransactionReceipt so its not the right answer at all

    "I know that in the transaction receipt I can get the contract address by getting a transaction receipt from a generated transaction."

    Read the question clearly next time

    – Moe Elsharif Sep 04 '19 at 11:37
  • 1
    @MoeElsharif Sorry...you are right. If you know the transaction creator address and the next nonce used (would have to be non-random), you can predict the next contract address. There is a CTF at this link -- https://blockchain-ctf.securityinnovation.com/#/ -- called TrustFund whose challenge is just this. But see https://slack-files.com/T0J0350JF-FK5NTH41J-d83c8daf88 for an example of how to see this information in Etherscan. – Daniel Connelly Sep 04 '19 at 18:57
  • 1
    @MoeElsharif By the way, I think what was confused me was your best answer to your question, which doesn't seem to answer your question either. In your question you are asking "[is there] a way to predict the address from the transaction its self" (i.e., predicting addresses). Then in your answer, you are explaining how you can find the contract address of an already existing contract through the getBlocks function... Perhaps you forgot (both before and now) what your question really was. – Daniel Connelly Sep 04 '19 at 19:32
2

You can actually get the contract address before a transaction is generated. A contract's address is determined by the address and nonce of the contract creator.

From the source code:

func CreateAddress(b common.Address, nonce uint64) common.Address {
    data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
    return common.BytesToAddress(Keccak256(data)[12:])
}
Kingmoz
  • 141
  • 6