Skip to content

Commit

Permalink
fix: resolves #2669
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Aug 29, 2024
1 parent 06d7e19 commit dc3c0a0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-phones-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `nonceManager` parameter to `prepareTransactionRequest`.
17 changes: 17 additions & 0 deletions site/pages/docs/actions/wallet/prepareTransactionRequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,23 @@ const request = await walletClient.prepareTransactionRequest({
})
```

### nonceManager (optional)

- **Type:** `NonceManager | undefined`

Nonce Manager to consume and increment the Account nonce for the transaction request.

```ts twoslash
// [!include config.ts]
// ---cut---
const request = await walletClient.prepareTransactionRequest({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
nonceManager: account.nonceManager // [!code focus]
})
```

### parameters (optional)

- **Type:** `("fees" | "gas" | "nonce" | "type")[]`
Expand Down
1 change: 1 addition & 0 deletions src/actions/wallet/prepareTransactionRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ test('behavior: nonce manager', async () => {

const args = {
account,
nonceManager: account.nonceManager,
to: targetAccount.address,
value: parseEther('1'),
parameters: ['nonce'],
Expand Down
15 changes: 13 additions & 2 deletions src/actions/wallet/prepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
getTransactionType,
} from '../../utils/transaction/getTransactionType.js'
import { getChainId as getChainId_ } from '../public/getChainId.js'
import type { NonceManager } from '../../utils/nonceManager.js'

export const defaultParameters = [
'blobVersionedHashes',
Expand Down Expand Up @@ -96,6 +97,15 @@ export type PrepareTransactionRequestRequest<
_derivedChain extends Chain | undefined = DeriveChain<chain, chainOverride>,
> = UnionOmit<FormattedTransactionRequest<_derivedChain>, 'from'> &
GetTransactionRequestKzgParameter & {
/**
* Nonce manager to use for the transaction request.
*/
nonceManager?: NonceManager | undefined
/**
* Parameters to prepare for the transaction request.
*
* @default ['blobVersionedHashes', 'chainId', 'fees', 'gas', 'nonce', 'type']
*/
parameters?: readonly PrepareTransactionRequestParameterType[] | undefined
}

Expand Down Expand Up @@ -248,6 +258,7 @@ export async function prepareTransactionRequest<
gas,
kzg,
nonce,
nonceManager,
parameters = defaultParameters,
type,
} = args
Expand Down Expand Up @@ -306,9 +317,9 @@ export async function prepareTransactionRequest<
if (parameters.includes('chainId')) request.chainId = await getChainId()

if (parameters.includes('nonce') && typeof nonce === 'undefined' && account) {
if (account.nonceManager) {
if (nonceManager) {
const chainId = await getChainId()
request.nonce = await account.nonceManager.consume({
request.nonce = await nonceManager.consume({
address: account.address,
chainId,
client,
Expand Down
1 change: 1 addition & 0 deletions src/actions/wallet/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export async function sendTransaction<
maxFeePerGas,
maxPriorityFeePerGas,
nonce,
nonceManager: account.nonceManager,
parameters: [...defaultParameters, 'sidecars'],
value,
...rest,
Expand Down
1 change: 1 addition & 0 deletions src/zksync/actions/sendEip712Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export async function sendEip712Transaction<
// Prepare the request for signing (assign appropriate fees, etc.)
const request = await prepareTransactionRequest(client, {
...parameters,
nonceManager: account.nonceManager,
parameters: ['gas', 'nonce', 'fees'],
} as any)

Expand Down

0 comments on commit dc3c0a0

Please sign in to comment.