15

When does a smart contract's address first become valid? Is the address during the execution of the contract, or only after it's been fully instantiated and initialized?

In other words, is a contract's address valid in its own constructor?

2 Answers 2

14

Yes, a contract constructor can access it's own address. This code works just fine to both log the address, and set it in storage:

 pragma solidity ^0.4.0;

contract Test {
  address public thisAddress;
  event LogAddr(address);

  function Test()  {
    thisAddress = address(this);
    LogAddr(address(this));
  }
}

This makes sense per this answer:

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.

0
9

The detail of contract creation are in Yellow Paper section 7, but it is too technical to quote here. But in section 7.1. Subtleties is more quotable

Note that while the initialisation code is executing, the newly created address exists but with no intrinsic body code.

Constructor is executed in the context of the new address, so the address can be used and passed around, but the contract cannot be called because the body is empty and the call will fail.

1
  • Very nice answer. This is actually what I was asking. So, I understand. It exists. It’s useable. But not for every use. Commented Nov 19, 2017 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.