Skip to content

Commit

Permalink
fix: avoid nonce gap in nonceManager (#3142) (#3153)
Browse files Browse the repository at this point in the history
* fix: avoid nonce gap in nonceManager (#3142)

* Update prepareTransactionRequest.ts

* Update brave-cooks-carry.md

---------

Co-authored-by: jxom <[email protected]>
  • Loading branch information
0xrouss and jxom authored Jan 6, 2025
1 parent c1bc74f commit 40222c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-cooks-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed premature nonce increment by rearranging gas estimation logic.
40 changes: 20 additions & 20 deletions src/actions/wallet/prepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 40222c8

Please sign in to comment.