How is the address of an Ethereum contract computed?

  • How is the address of an Ethereum contract computed? What use cases are there for knowing a contract's address in advance?

    In addition to @eth answer it should be noted that you take last 20 bytes.

  • eth

    eth Correct answer

    5 years ago

    EDIT April 2019: CREATE2 information added.

    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). The sender and nonce are RLP encoded and then hashed with Keccak-256.

    From pyethereum:

    def mk_contract_address(sender, nonce):
        return sha3(rlp.encode([normalize_address(sender), nonce]))[12:]

    Per @erwaman comment, in Solidity ^0.6.0:

    nonce0= address(uint160(uint256(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), _origin, byte(0x80))))))
    nonce1= address(uint160(uint256(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), _origin, byte(0x01))))))

    Example with some discussion:

    For sender 0x6ac7ea33f8831ea9dcc53393aaa88b25a785dbf0, the contract addresses that it will create are the following:

    nonce0= "0xcd234a471b72ba2f1ccf0a70fcaba648a5eecd8d"
    nonce1= "0x343c43a37d37dff08ae8c4a11544c718abb4fcf8"
    nonce2= "0xf778b86fa74e846c4f0a1fbd1335fe81c00a0c91"
    nonce3= "0xfffd933a0bc612844eaf0c6fe3e5b8e9b6c1d19c"

    In Java with Web3j:

    private String calculateContractAddress(String address, long nonce){
        byte[] addressAsBytes = Numeric.hexStringToByteArray(address);
        byte[] calculatedAddressAsBytes =
                        new RlpList(
        calculatedAddressAsBytes = Arrays.copyOfRange(calculatedAddressAsBytes,
                12, calculatedAddressAsBytes.length);
        String calculatedAddressAsHex = Numeric.toHexString(calculatedAddressAsBytes);
        return calculatedAddressAsHex;

    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.


    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:]

    Note that senderAddress can be either an externally-owned account (EOA) or a contract.

    More information will be added here and for the meantime see EIP-1014.

    For the usecase part, you could mention something about prefunding contracts

    What if the creator is a contract itself? Has the address also a nonce? Is it increased by every call it makes or only with the creation of new contacts? Or is the address and nonce of tx.origin relevant?

    @TjadenHess Thanks, upvoted instead of explaining prefunding more.

    @mKoeppelmann: Same computation if creator is a contract; nonce increases every single transaction an account makes (new contract/account would start at nonce 0), and the address and nonce of earlier senders (such as tx.origin) do not affect address of new contract.

    @eth: "nonce increases every single transaction" this is what confuses me, because in the definition in the yellowpaper only human controlled accounts can make transactions. A transaction is something that is signed with a private key. Contracts can only make calls that are triggered by transactions. So I wonder what "calls" increase the nonce of a contract. All calls or only calls that create a new contract?

    I think the question I am asking is getting a little bit of topic so I created a new question here:

    What happens in the case of a contract creating a contract? Does a contract also have a sequence number?

    @StevenRoose Yes, contracts have nonces. A nonce of a contract is only incremented when that contract creates another contract. It's in separate question Martin asked :)

    0xd6, 0x94, 0x80 and 0x01, what are those magic numbers?

    They are part of the RLP encoding

    This should be updated to include new CREATE2 mechanism:

    @tayvano Thanks, some basic information added for now. Will try clarify more later, and afterwards will clean up our comments.

    Here is online smart contract address calculator with full CREATE2 support:

    @MikhailVladimirov Thanks, if you have time to post a separate answer, explaining the different fields / how to use your calculator, I will update my CREATE2 answer to point to yours.

    For Solidity ^0.6.0, I had to use `address(uint160(uint256(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), _origin, byte(0x80))))))` (for nonce0).

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM