0
    web3js.eth.getTransactionCount(CONTRACT_ADDRESS+'').then( txcount => { 
      console.log( "-> getTransactionCount: " + txcount );
    });

Always return 1, but my contract has lot of transactions. Any idea why? Thanks

Extrange planet
  • 685
  • 5
  • 11

1 Answers1

1

getTransactionCount returns the account nonce.

For an Externally Owned Account (EOA) the nonce is the number of previously sent transactions which starts at 0 and is incremented by 1 for each transaction sent hence the name of the function getTransactionCount.

However, for Contract Accounts (CA) the nonce is the number of contract created by this contract. CA's nonce starts at 1 (since EIP-161) and is also incremented by 1 for each CREATE or CREATE2 call which creates a new contract.

Your contract never sent a single transaction because contract cannot do that. They can do message calls but the transaction is coming from an EOA. A value of 1 in that case simply means that your contract never created other contracts by means of CREATE or CREATE2, it's nonce is therefore the default value : 1.

You could check the nonce of a contract factory this as the Uniswap V2 Factory Contract to see that its nonce is greater than 1, and is incremented everytime a contract is created by this factory contract.

hroussille
  • 7,661
  • 2
  • 6
  • 29
  • Thanks ! Exist any method to call the real transactions count of a contract to know what nonce i need to set? – Extrange planet Dec 15 '21 at 10:47
  • For a smart contract ? not that I know of, the nonce doesn't refer to transactions for SC as I explained above. – hroussille Dec 15 '21 at 12:06
  • Etherscan show a Tx List of contracts... for example the USDT contract: https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7 – Extrange planet Dec 15 '21 at 12:26
  • I remember reading somewhere that they were populating a bloom filter by iterating over blocks to find the tx involving the contract, but I cannot find it anymore and it's not sure at all, even though it does make sense, you could do something similar maybe ? or just use etherscan api directly as per this answer : https://ethereum.stackexchange.com/a/89565/84305 – hroussille Dec 15 '21 at 17:49
  • I understand my problem know. My contract address has not a correlative nonce because every owner address has their own nonce. Ok. Now i need to know if a contract list could has a repeating nonce... i think yes. – Extrange planet Dec 18 '21 at 12:33