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?

5 Answers 5

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.

5
  • 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, 2017 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, 2017 at 9: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") Jun 6, 2018 at 8:51
  • 1
    Good point joeytwiddle and maybe what @0xcaff was asking.
    – eth
    Jun 21, 2018 at 19:15
  • web3.py the function now is web3_eth.get_transaction_count(account, block_identifier=web3.eth.default_block)
    – jkr
    Oct 27, 2022 at 4: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
}
5
  • 1
    What if I don't know my last transaction hash? Can I use the number from eth.getTransactionCount?
    – Broseph
    Mar 31, 2016 at 19:36
  • I'd say more with eth.getTransactionFromBlock(block hash #) no ?
    – euri10
    Mar 31, 2016 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, 2016 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, 2016 at 20:13
  • you should reverse the correct answer to the one @eth did ;)
    – euri10
    Apr 1, 2016 at 10:39
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.

1
  • 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: github.com/ethers-io/ethers.js/issues/… Apr 19, 2021 at 2: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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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