Post
Share your knowledge.
Getting the latest nonce with web3.js
Hey folks! I've been using web3.eth.getTransactionCount with 'pending' to get the nonce, but it doesn't seem to return the latest one. Is there any way to get an accurate nonce, or should I consider using another service?
- Polygon Edge
Answers
2Here's a tip using Ethers.js: You can use the following code snippet to get the transaction count (nonce):
provider.getTransactionCount('xxxx', 'pending').then((nonce) => {
console.log("Nonce to Use:", nonce);
});
This could be a more reliable approach if web3.js isn't cutting it for you.
You could consider using external services like Alchemy, Infura, or QuickNode as they are known to provide more reliable data on pending transactions. Alternatively, you might want to try using the Ethers.js library for fetching the nonce as it might give better results in terms of accuracy.
Do you know the answer?
Please log in and share it.
Polygon is a decentralised Ethereum scaling platform that enables developers to build scalable user-friendly dApps with low transaction fees without ever sacrificing on security.