This is a follow-up to this question. The accepted answer says:

Yes, contracts have nonces. A nonce of a contract is only incremented when that contract creates another contract [...]. When a contract invokes a function on another contract, a so called "internal transaction" (in http://live.ether.camp), the nonce is not incremented.

The nonce can be obtained by using web3.eth.getTransactionCount.

So far, so good. But when I use web3.eth.getTransactionCount with a recently deployed contract, the value is 1, not 0, despite that contract not having created any other contract.

Why is that happening?

up vote 2 down vote accepted

From https://github.com/ethereum/EIPs/blob/master/EIPS/eip-161.md:

a. Account creation transactions and the CREATE operation SHALL, prior to the execution of the initialisation code, increment the nonce over and above its normal starting value by one (for normal networks, this will be simply 1, however test-nets with non-zero default starting nonces will be different).

This is a fairly recent change, which explains why you can find older contracts with a nonce of 0.

As to the why, my (limited) understanding is that it's an attempt to make sure that "empty" accounts and "non-existent" accounts are the same (zero nonce, zero balance, and empty code), which allows for clean-up of empty accounts.

@smarx found the answer with EIP 161.

Before EIP 161 contract nonces do start at 0. You are probably passing in your address instead of the contract's address to web3.eth.getTransactionCount (or the contract you passed in did create another contract).

Here's an example of a contract that has a nonce of 0.

> web3.eth.getTransactionCount('0xab7c74abc0c4d48d1bdad5dcb26153fc8780f83e')
0

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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