20

I need to know my transaction nonce because I am doing fancy stuff with contract addresses (which are the keccak hash of my address + my transaction nonce). Is there a way to find this through the JavaScript console?

19

Your comment is correct: use web3.eth.getTransactionCount(accountAddress)

Note that the client (Geth) should be in sync with the blockchain.


If you don't have a node and want to trust a service, you can try Infura with eth_getTransactionCount.

  • What if you manage to set a nonce greater than your transaction count on a previous transaction? How do you get the nonce then? – 0xcaff Aug 19 '17 at 13:33
  • @0xcaff I don't understand your question, can you please clarify? If the client is synced, it will always provide the correct nonce... – eth Aug 29 '17 at 9:07
  • 1
    I recommend using web3.eth.getTransactionCount(accountAddress, 'pending') to avoid nonce collisions when performing multiple transactions in parallel. (If you don't you may get this confusing error message: "Returned error: known transaction") – joeytwiddle Jun 6 '18 at 8:51
  • 1
    Good point joeytwiddle and maybe what @0xcaff was asking. – eth Jun 21 '18 at 19:15
5

in geth you can :

eth.getTransaction('0x0b95eaccd6273fa35bd9e6102c8a7216ee62274940ff52348c8967c13e1c12de')
{
  blockHash: "0x24b5d26d4fca736ca095e4c85e0e8230c2e0b1056a5050824a4bccf7dd84e1f2",
  blockNumber: 1251275,
  from: "0x2a65aca4d5fc5b5c859090a6c34d164135398226",
  gas: 90000,
  gasPrice: 20000000000,
  hash: "0x0b95eaccd6273fa35bd9e6102c8a7216ee62274940ff52348c8967c13e1c12de",
  input: "0x",
  nonce: 377073,
  to: "0xeadefe2cfa52c759e274d6592a938be0217f5877",
  transactionIndex: 15,
  value: 1011030740000000000
}
  • 1
    What if I don't know my last transaction hash? Can I use the number from eth.getTransactionCount? – Broseph Mar 31 '16 at 19:36
  • I'd say more with eth.getTransactionFromBlock(block hash #) no ? – euri10 Mar 31 '16 at 19:42
  • But if I don't know the block of my last transaction either, then I have to look through all the blocks, which is not ideal. – Broseph Mar 31 '16 at 19:47
  • The yellow paper says that the transaction nonce is equal to the number of transactions sent by the address. – Broseph Mar 31 '16 at 20:13
  • you should reverse the correct answer to the one @eth did ;) – euri10 Apr 1 '16 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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