prepareUserOperationRequest
Prepares a user operation request for signing by populating a sender, nonce, initCode, gas price values, gas limit values, and optionally requesting sponsorship from a paymaster.
Usage
import { smartAccountClient, simpleSmartAccount } from "./smartAccountClient"
import { pimlicoBundlerClient } from "./bundlerClient"
import { parseAbiItem, encodeFunctionData } from "viem"
const callData = await simpleSmartAccount.encodeCallData({
to: "0x0488bEE1Ec682db0F0E74AB52faFdDdEf10Af123",
data: encodeFunctionData({
abi: [parseAbiItem('function mint()')]
}),
value: 0n
})
// only if using pimlico
const gasPrices = await pimlicoBundlerClient.getUserOperationGasPrice()
const userOperation = await smartAccountClient.prepareUserOperationRequest({
userOperation: {
callData, // callData is the only required field in the partial user operation
maxFeePerGas: gasPrices.fast.maxFeePerGas,
maxPriorityFeePerGas: gasPrices.fast.maxPriorityFeePerGas
}
})import { ENTRYPOINT_ADDRESS_V07, createSmartAccountClient } from "permissionless"
import { privateKeyToSimpleSmartAccount } from "permissionless/accounts"
import { createPimlicoPaymasterClient } from "permissionless/clients/pimlico"
import { createPublicClient, http } from "viem"
import { sepolia } from "viem/chains"
export const publicClient = createPublicClient({
transport: http("https://sepolia.rpc.thirdweb.com"),
})
// if using a paymaster
const paymasterClient = createPimlicoPaymasterClient({
transport: http("https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_PIMLICO_API_KEY"),
entryPoint: ENTRYPOINT_ADDRESS_V07,
})
export const simpleSmartAccount = await privateKeyToSimpleSmartAccount(publicClient, {
privateKey: "0xPRIVATE_KEY",
factoryAddress: "0x91E60e0613810449d098b0b5Ec8b51A0FE8c8985", // simple version
entryPoint: ENTRYPOINT_ADDRESS_V07,
})
export const smartAccountClient = createSmartAccountClient({
account: simpleSmartAccount,
entryPoint: ENTRYPOINT_ADDRESS_V07,
chain: sepolia, // or whatever chain you are using
bundlerTransport: http("https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_PIMLICO_API_KEY"),
middleware: {
sponsorUserOperation: paymasterClient.sponsorUserOperation, // if using a paymaster
},
})import { ENTRYPOINT_ADDRESS_V07, bundlerActions } from "permissionless"
import { pimlicoBundlerActions } from "permissionless/actions/pimlico"
import { createClient, http } from "viem"
import { sepolia } from "viem/chains"
export const pimlicoBundlerClient = createClient({
chain: sepolia,
transport: http("https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_API_KEY_HERE"),
})
.extend(bundlerActions(ENTRYPOINT_ADDRESS_V07))
.extend(pimlicoBundlerActions(ENTRYPOINT_ADDRESS_V07))Returns
- Type:
UserOperation
The UserOperation with the remaining fields (except the signature) filled.
Parameters
userOperation
- Type:
Partial<UserOperation>
The partial user operation. Only the callData field is required.
sponsorUserOperation (optional)
- Type:
(args: { userOperation: UserOperation, entryPoint: Address }) => Promise<UserOperation>
The paymaster middleware function (if one is used).
account (optional)
- Type:
SmartAccount
The smart account that will be used to fetch the nonce, initCode, sender, and the gas price values if they are not already filled.