35

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?

eth
  • 85,679
  • 53
  • 285
  • 406
Broseph
  • 695
  • 1
  • 7
  • 12

6 Answers6

33

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.

eth
  • 85,679
  • 53
  • 285
  • 406
  • 1
    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 09:07
  • 2
    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 06 '18 at 08:51
  • 1
    Good point joeytwiddle and maybe what @0xcaff was asking. – eth Jun 21 '18 at 19:15
  • web3.py the function now is web3_eth.get_transaction_count(account, block_identifier=web3.eth.default_block) – jkr Oct 27 '22 at 04:27
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
}
euri10
  • 4,640
  • 5
  • 24
  • 55
1

Normally I get transaction count using

web3.eth.getTransactionCount(
    wallet_address
);

This is not enough when sendind few transactions while the old ones are not mined yet, so some people use getPendingTransaction method

When, like me, working with Infura RPC, this last method is NOT available at all, not implemented yet; absurd, but this is.

I discovered googling that we can use getTransactionCount in the following way

web3.eth.getTransactionCount(
   wallet_address,
   "pending"
);

This is actually not documented, but works with local geth installation and with Infura.

Using this system I can send a bunch of signed transactions one at time in a short time having them all pending and then mined in some minutes.

realtebo
  • 333
  • 5
  • 15
  • 1
    Warning: this function with "Pending" option can stuck your account, the same way mine got stucked. I couldn't deploy and mint. It's recommended using the function without this option. Explanation: https://github.com/ethers-io/ethers.js/issues/435#issuecomment-583808056 – Silvio Guedes Apr 19 '21 at 02:02
0

Ethers it's pretty similar to web3 regarding getTransactionCount.

Reference

await provider.getTransactionCount('ricmoo.eth');
// 26

Using the "pending" switch you can get the number of non mined tx for that address

await provider.getTransactionCount('ricmoo.eth', 'pending');
// 2

Beware though: it's quite unreliable.

The pending blockTag is not terribly reliable, and I won’t recommend it. The implementation for Parity and Geth is quite different, where one examines the total transaction pool and one examines the transactions expected to go into the next block (I forget which is which), and many backends simply ignore it and use latest internally if pending is used.

Basically, unless you are running your own infrastructure which does what you want it to do, I won’t depend on it. :)

https://github.com/ethers-io/ethers.js/issues/435#issuecomment-583808056

Diego Ferri
  • 926
  • 8
  • 12
0

Its node js code to get nonce of any account:-

const Web3 = require("web3");
const web3 = new Web3(<RPC-Provider-Link>)
var account = "0x60E7B364092B1f8D96C5C92Dxxxxxxxxxxxx"

web3.eth.getTransactionCount(account, (err, nonce) => { console.log(nonce,err) })

install web3 by npm i web3. also change account & RPC-Provider-Link before running codes above.

aakash4dev
  • 691
  • 3
  • 11
0

You can find out your transaction nonce through the help of Bitquery APIs. Please find the js console implementation for the same below

https://gist.github.com/GkM8te-BitQuery/bdb575749742a0ed03e8d968a20c89c1

You can additionally play around with the graphQL query in our ide to get any additional data points. https://ide.bitquery.io/TxHash_nonce