67

Can anyone explain to me what address(0) is in Solidity? I found the following in the docs but it doesn't really make sense to me:

If the target account is the zero-account (the account with the address 0), the transaction creates a new contract. As already mentioned, the address of that contract is not the zero address but an address derived from the sender and its number of transactions sent (the “nonce”). The payload of such a contract creation transaction is taken to be EVM bytecode and executed. The output of this execution is permanently stored as the code of the contract. This means that in order to create a contract, you do not send the actual code of the contract, but in fact code that returns that code.

http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html?highlight=address(0)#index-8

pizzarob
  • 11,711
  • 6
  • 48
  • 69

3 Answers3

48

Within an Ethereum transaction, the zero-account is just a special case used to indicate that a new contract is being deployed. It is literally '0x0' set to the to field in the raw transaction.

Every Ethereum transaction, whether it's a transfer between two external accounts, a request to execute contract code, or a request to deploy a new contract, are encoded in the same way. A raw transaction object will look something like this:

transaction = {
  nonce: '0x0', 
  gasLimit: '0x6acfc0', // 7000000
  gasPrice: '0x4a817c800', // 20000000000
  to: '0x0',
  value: '0x0',
  data: '0xfffff'
};

If to is set to something other than '0x0', this request will result in transferring ether to the address (if value is non-zero), and execute the function encoded in the data field. Remember, the address can either be a contract or an external account.

When the to address is the zero-address, a new contract will be created by executing the code in data (this is what is meant by "code that returns the code"). The address of the newly created contract is technically known beforehand as it's based on the address of the sender and it's current nonce. That address becomes the official address of the contract after mining.

For a pretty good read on Ethereum transactions, check out this blog post.

Note: There is also the actual Solidity code statement address(0) which is the initial value of a variable of type address. The documentation you posted, however, is referring to specifically when the to account address in a transaction is set to '0x0'.

Pang
  • 9,564
  • 146
  • 81
  • 122
Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • 15
    ```address(0)``` is also the address that you send 'burned' tokens to. You can see this in the BurnableToken.sol contract in openzeppelin. – cipherz May 23 '18 at 14:30
  • Do you think making arbitrary call on `address(0)` will lead to contract deploy? `address(0).call(data)` – k06a Sep 08 '18 at 15:44
  • Using the Solidity code statement `address(0)` is different. I haven't tried it, but no I don't think that would work (seems weird to try it this way). If I wanted to deploy a contract within Solidity using bytecode, I would use assembly. – Adam Kipnis Sep 09 '18 at 18:02
  • 1
    All the stuff about the `to` field is wrong. This answer needs a substantial revision. – C S Oct 24 '22 at 18:25
11

It's not actually true that a contract creation transaction has a "to" field set to the zero address (meaning 0x00...000). This is an easy mistake to make (and I've made it too) as it is described that way in many resources.

The passage you cite from the Solidity docs were updated to state this:

If the target account is not set (the transaction does not have a recipient or the recipient is set to null), the transaction creates a new contract. As already mentioned, the address of that contract is not the zero address but an address derived from the sender and its number of transactions sent (the “nonce”).

So you can see they realized at some point that the recipient field should be empty. I've actually looked at serialized creation transactions and found 0x80 there instead of an RLP-ed zero address.

In fact, 0x80 is the RLP encoding of an empty byte array, which is what the Yellow Paper states is the recipient for a contract creation:

The address hash $T_t$ is slightly different: it is either a 20-byte address hash or, in the case of being a contract-creation transaction (and thus formally equal to ∅), it is the RLP empty byte sequence and thus the member of $B_0$

As I said, this is a common source of confusion. In that vein, this GitHub PR rolling back a mistakenly "fixed" test is amusing. It has the comment:

RLP encoding of 0 is encoding of empty byte array, so 0x80 is correct.

0x00 is encoding of byte array of length 1 containing one byte 0, not encoding of integer 0.

C S
  • 1,363
  • 1
  • 17
  • 26
0

Address-zero(0) represents the zero address or the null address. It's an address value that denotes the absence of a valid Ethereum address and is used as the initial/default value of addresses in solidity. It looks like this: 0x0000000000000000000000000000000000000000.