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?
6 Answers
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.
- 85,679
- 53
- 285
- 406
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
}
- 4,640
- 5
- 24
- 55
-
1What if I don't know my last transaction hash? Can I use the number from
eth.getTransactionCount? – Broseph Mar 31 '16 at 19:36 -
-
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
-
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.
- 333
- 5
- 15
-
1Warning: 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
Ethers it's pretty similar to web3 regarding getTransactionCount.
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
pendingblockTag 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 uselatestinternally ifpendingis 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
- 926
- 8
- 12
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.
- 691
- 3
- 11
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
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