From 40222c8cf984db387251e1df5922e4b9715e2bf9 Mon Sep 17 00:00:00 2001 From: 0xrouss <55579471+0xrouss@users.noreply.github.com> Date: Mon, 6 Jan 2025 21:30:27 +0100 Subject: [PATCH] fix: avoid nonce gap in nonceManager (#3142) (#3153) * fix: avoid nonce gap in nonceManager (#3142) * Update prepareTransactionRequest.ts * Update brave-cooks-carry.md --------- Co-authored-by: jxom <7336481+jxom@users.noreply.github.com> --- .changeset/brave-cooks-carry.md | 5 +++ .../wallet/prepareTransactionRequest.ts | 40 +++++++++---------- 2 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 .changeset/brave-cooks-carry.md diff --git a/.changeset/brave-cooks-carry.md b/.changeset/brave-cooks-carry.md new file mode 100644 index 0000000000..cb7dc48d25 --- /dev/null +++ b/.changeset/brave-cooks-carry.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Fixed premature nonce increment by rearranging gas estimation logic. diff --git a/src/actions/wallet/prepareTransactionRequest.ts b/src/actions/wallet/prepareTransactionRequest.ts index fc12d2fee4..d99523d93c 100644 --- a/src/actions/wallet/prepareTransactionRequest.ts +++ b/src/actions/wallet/prepareTransactionRequest.ts @@ -316,26 +316,6 @@ export async function prepareTransactionRequest< if (parameters.includes('chainId')) request.chainId = await getChainId() - if (parameters.includes('nonce') && typeof nonce === 'undefined' && account) { - if (nonceManager) { - const chainId = await getChainId() - request.nonce = await nonceManager.consume({ - address: account.address, - chainId, - client, - }) - } else { - request.nonce = await getAction( - client, - getTransactionCount, - 'getTransactionCount', - )({ - address: account.address, - blockTag: 'pending', - }) - } - } - if ( (parameters.includes('fees') || parameters.includes('type')) && typeof type === 'undefined' @@ -415,6 +395,26 @@ export async function prepareTransactionRequest< : account, } as EstimateGasParameters) + if (parameters.includes('nonce') && typeof nonce === 'undefined' && account) { + if (nonceManager) { + const chainId = await getChainId() + request.nonce = await nonceManager.consume({ + address: account.address, + chainId, + client, + }) + } else { + request.nonce = await getAction( + client, + getTransactionCount, + 'getTransactionCount', + )({ + address: account.address, + blockTag: 'pending', + }) + } + } + assertRequest(request as AssertRequestParameters) delete request.parameters