ethers.js: Send Transaction error: "NOUNCE_EXPIRED"
Code
let tx = {
to: childData.address,
value: ethers.utils.parseEther('0.1')
};
wallet.sendTransaction(tx)
.then((tx) => {
console.log(tx.hash);
})
.catch((err) => {
console.log(err);
});
Error:
Error: nonce has already been used (version=4.0.23) { Error: nonce has already been used (version=4.0.23)
complete error log:
reason: 'nonce has already been used', code: 'NONCE_EXPIRED', transaction: { nonce: 97, gasPrice: BigNumber { _hex: '0x3b9aca00' }, gasLimit: BigNumber { _hex: '0x5208' }, to: '0xEB21076E53E905c70b60354cF17c2599eB2e35EB', value: BigNumber { _hex: '0x016345785d8a0000' }, data: '0x', chainId: 4, v: 43, r: '0x02c87a81367ec869eda61a7444afca53a528b8e1f7980f7e596c7312de85ffcf', s: '0x19b3cebbb190e2dcc9e49fb13de960067ab5e778ba1680b2818c47e95578f8e3', from: '0xa88744Cd5F813ae6ba275Ee1BbbE5219DfC1c1A1', hash: '0x1f1ce79022d0b4c38db8587b4137c5273a06eaaf04c0ecebe7ca4e03d2c8dd39' }, transactionHash: '0x1f1ce79022d0b4c38db8587b4137c5273a06eaaf04c0ecebe7ca4e03d2c8dd39' }
How can I resolve this ?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (6 by maintainers)
You can do it the same way (except with
provider.getTransactionCount(accountAddress, “pending”)
. But if you execute too many transactions too quickly, the “pending” will still get overrun.I would recommend for now, if you are executing many transactions, you can do something like:
And so on. Every call to getNonce gives you the next nonce. That’s basically all the NonceManager does. 😃
(typed on my iPhone; there may be typos, but you should get the gist)
This is likely caused by sending multiple transactions immediately after each other, before the network propagates the “pending” transaction count.
I have an example nonce manager I will be publishing soon, but basically you will need to pass the nonce in and manually increment it for each transaction.
Make sense? I’ll aim to get my example NonceManager (which extends Signer) up this week.
@gitpusha
There isn’t anything in the docs yet, and it is still part of the experimental package, but if you’d like to try it out, here is the code.
You should be able to use:
And then just use the managedSigner instead of the signer.
Make sure if you use the
@ethersproject/experimental
package, you lock in the exact version (i.e.5.0.0-beta.134
, not a ranged version, like^5.0.0-beta.134
) since that package could change wildly. 😃Hi, I am having this issue when I try to replace a transaction with the same nonce, and higher gas fees. How can I proceed, is it possible to replace tx with Ethers ?