prepareTransactionRequest
Prepares a transaction request for signing by populating a nonce, gas limit, fee values, and a transaction type.
Usage
import { account, walletClient } from './config'
const request = await walletClient.prepareTransactionRequest({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
{ account: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', to: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8', maxFeePerGas: 150000000000n, maxPriorityFeePerGas: 1000000000n, nonce: 69, type: 'eip1559', value: 1000000000000000000n }
const signature = await walletClient.signTransaction(request)
const hash = await walletClient.sendRawTransaction(signature)
Account Hoisting
If you do not wish to pass an account
to every prepareTransactionRequest
, you can also hoist the Account on the Wallet Client (see config.ts
).
import { walletClient } from './config'
const request = await walletClient.prepareTransactionRequest({
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
{ account: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', to: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8', maxFeePerGas: 150000000000n, maxPriorityFeePerGas: 1000000000n, nonce: 69, type: 'eip1559', value: 1000000000000000000n }
const signature = await walletClient.signTransaction(request)
const hash = await walletClient.sendRawTransaction(signature)
Returns
The transaction request.
Parameters
account
- Type:
Account | Address
The Account to send the transaction from.
Accepts a JSON-RPC Account or Local Account (Private Key, etc).
const request = await walletClient.prepareTransactionRequest({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
to
- Type:
0x${string}
The transaction recipient or contract address.
const request = await walletClient.prepareTransactionRequest({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
nonce: 69
})
accessList (optional)
- Type:
AccessList
The access list.
const request = await walletClient.prepareTransactionRequest({
accessList: [
{
address: '0x1',
storageKeys: ['0x1'],
},
],
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})
blobs (optional)
- Type:
Hex[]
Blobs for Blob Transactions.
import * as kzg from 'c-kzg'
import { toBlobs, stringToHex } from 'viem'
import { mainnetTrustedSetupPath } from 'viem/node'
const kzg = setupKzg(cKzg, mainnetTrustedSetupPath)
const request = await walletClient.prepareTransactionRequest({
account,
blobs: toBlobs({ data: stringToHex('blobby blob!') }),
kzg,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
})
chain (optional)
- Type:
Chain
- Default:
walletClient.chain
The target chain. If there is a mismatch between the wallet's current chain & the target chain, an error will be thrown.
The chain is also used to infer its request type (e.g. the Celo chain has a gatewayFee
that you can pass through to prepareTransactionRequest
).
import { optimism } from 'viem/chains'
const request = await walletClient.prepareTransactionRequest({
chain: optimism,
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
data (optional)
- Type:
0x${string}
A contract hashed method call with encoded args.
const request = await walletClient.prepareTransactionRequest({
data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
gasPrice (optional)
- Type:
bigint
The price (in wei) to pay per gas. Only applies to Legacy Transactions.
import { parseEther, parseGwei } from 'viem'
const request = await walletClient.prepareTransactionRequest({
account,
gasPrice: parseGwei('20'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
kzg (optional)
- Type:
KZG
KZG implementation for Blob Transactions.
See setupKzg
for more information.
import * as kzg from 'c-kzg'
import { toBlobs, stringToHex } from 'viem'
import { mainnetTrustedSetupPath } from 'viem/node'
const kzg = setupKzg(cKzg, mainnetTrustedSetupPath)
const request = await walletClient.prepareTransactionRequest({
account,
blobs: toBlobs({ data: stringToHex('blobby blob!') }),
kzg,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
})
maxFeePerGas (optional)
- Type:
bigint
Total fee per gas (in wei), inclusive of maxPriorityFeePerGas
. Only applies to EIP-1559 Transactions
import { parseEther, parseGwei } from 'viem'
const request = await walletClient.prepareTransactionRequest({
account,
maxFeePerGas: parseGwei('20'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
maxPriorityFeePerGas (optional)
- Type:
bigint
Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions
import { parseEther, parseGwei } from 'viem'
const request = await walletClient.prepareTransactionRequest({
account,
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('2'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
nonce (optional)
- Type:
number
Unique number identifying this transaction.
const request = await walletClient.prepareTransactionRequest({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
nonce: 69
})
parameters (optional)
- Type:
("fees" | "gas" | "nonce" | "type")[]
- Default:
["fees", "gas", "nonce", "type"]
Parameters to prepare.
For instance, if ["gas", "nonce"]
is provided, then only the gas
and nonce
parameters will be prepared.
const request = await walletClient.prepareTransactionRequest({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
nonce: 69
})
value (optional)
- Type:
bigint
Value in wei sent with this transaction.
import { parseEther } from 'viem'
const request = await walletClient.prepareTransactionRequest({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1'),
nonce: 69
})