2

So from my understanding when creating a contract the two variables that are used in determining what the address of the contract will be are the msg.sender and the nonce value. So if I create two contracts in the same transaction such as I did with this code https://ropsten.etherscan.io/address/0xcb7d7e99e56406a675173f0ddbde7d8cc3392e5e#code

Why did it generate two contracts at two different addresses, what I though would happen is that they would generate at the same address and the one would simply overwrite the other or something like that.

1 Answers1

3

You're understanding of the contract address determined by the address of the message creator and the nonce is correct. However, in the example you posted, msg.sender is the address of the Test contract.

These are the steps that occured:

  1. You initiated the transaction to deploy Test from your external account (0x98081ce968e5643c15de9c024de96b18be8e5ace). According to the transaction information, the nonce of the account at that time was 639.
  2. This resulted in the Test contract having an address of 0xcb7d7e99e56406a675173f0ddbde7d8cc3392e5e.
  3. During the deployment of Test, the constructor then creates two new contracts through "internal transactions". Divert is deployed from the contract address at 0xcb7d7e99e56406a675173f0ddbde7d8cc3392e5e with nonce=1. OverRide is deployed from the same address with nonce=2.

You can view the details of the internal transaction here.

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • So even if I wanted to there is no way to override a contract and have two created at the same address? –  May 23 '18 at 22:49
  • Not that I'm aware of. It would be a bad thing if you could. You can submit two transactions from one address with the same nonce, but one of them is guaranteed to fail. – Adam Kipnis May 24 '18 at 00:10
  • 1
    Just FYI, these days contract nonces start at 1. – user94559 May 24 '18 at 01:28
  • 1
    @smarx...Yep. I updated the answer. I actually thought of that while I was typing and STILL put the wrong values in. – Adam Kipnis May 24 '18 at 02:27