How is the address of an Ethereum contract computed? What use cases are there for knowing a contract's address in advance?
[Ethereum] How is the address of an Ethereum contract computed
addressescontract-deploymentcreatecreate2
addressescontract-deploymentcreatecreate2
How is the address of an Ethereum contract computed? What use cases are there for knowing a contract's address in advance?
Best Answer
EDIT April 2019:
CREATE2
information added.EDIT January 2022: Updated Solidity syntax to ^0.8.0.
The address for an Ethereum contract is deterministically computed from the address of its creator (
sender
) and how many transactions the creator has sent (nonce
). Thesender
andnonce
are RLP encoded and then hashed with Keccak-256.From pyethereum:
In Solidity:
Example with some discussion:
For sender 0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0, the contract addresses that it will create are the following:
In Java with Web3j:
Note: As per EIP 161 A Specification contract accounts are initiated with nonce = 1 (in the mainnet). So the first contract address, created by another contract, will be computed with non-zero nonce.
CREATE2
A new opcode,
CREATE2
was added in EIP-1014 that is another way that a contract can be created.For contract created by
CREATE2
its address will be:keccak256( 0xff ++ senderAddress ++ salt ++ keccak256(init_code))[12:]
More information will be added here and for the meantime see EIP-1014.