Skip to content
LogoLogo

userOperation.prepare

Prepares a User Operation for execution and fills in missing properties.

Usage

import {  } from 'viem/utils'
import { ,  } from './config'
 
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }]
})

Pass either calls for Smart Account encoding or an already encoded callData. Viem fills the remaining version-specific User Operation fields selected by parameters.

Account Hoisting

If you do not wish to pass an account to every userOperation.prepare, you can also hoist the Account on the Bundler Client (see config.ts).

Learn more.

import {  } from 'viem/utils'
import {  } from './config'
 
const  = await ..({
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
})

Contract Calls

The calls property also accepts Contract Calls, and can be used via the abi, functionName, and args properties.

import {  } from './config'
import {  } from './abi'
 
const  = await ..({
  : [{
    : ,
    : 'mint',
    : '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  }],
})

Returns

UserOperation

The prepared User Operation.

Parameters

account

  • Type: SmartAccount
  • Default: bundlerClient.account

The Account to use for User Operation execution.

import {  } from 'viem/utils'
import { ,  } from './config'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }]
})

authorization

  • Type: Authorization.Signed

Signed EIP-7702 authorization for a delegated account.

callGasLimit

  • Type: bigint

The amount of gas to allocate the main execution call.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : 69420n,
})

calls

  • Type: { data: Hex, to: Address, value: bigint }[]

The calls to execute in the User Operation.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }]
})

dataSuffix

  • Type: Hex

Data to append to the end of User Operation calldata. Useful for adding transaction attribution.

If not provided, the dataSuffix configured on the Bundler Client (or inherited from the underlying client) will be used.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0xdeadbeef',
})

factory

  • Type: Address

Account Factory address.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0x1234567890123456789012345678901234567890',
  : '0xdeadbeef',
})

factoryData

  • Type: Hex

Call data to execute on the Account Factory to deploy a Smart Account.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0x1234567890123456789012345678901234567890',
  : '0xdeadbeef',
})

maxFeePerGas

  • Type: bigint

Maximum fee per gas for User Operation execution.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : 420n,
})

maxPriorityFeePerGas

  • Type: bigint

Maximum priority fee per gas for User Operation execution.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : 420n,
  : 10n,
})

nonce

  • Type: bigint

Nonce for the User Operation.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : 10n,
})

parameters

  • Type: ('authorization' | 'factory' | 'fees' | 'gas' | 'nonce' | 'paymaster' | 'signature')[]
  • Default: All parameters

Fields that Viem should populate. Fields excluded from this list remain optional in the return type.

paymaster

  • Type: Address | true | PaymasterClient | PaymasterActions

Sets Paymaster configuration for the User Operation.

  • If paymaster: Address, it will use the provided Paymaster contract address for sponsorship.
  • If paymaster: PaymasterClient, it will use the provided Paymaster Client for sponsorship.
  • If paymaster: true, it will be assumed that the Bundler Client also supports Paymaster RPC methods (e.g. pm_getPaymasterData), and use them for sponsorship.
  • If custom functions are provided to paymaster, it will use them for sponsorship.

Using a Paymaster Contract Address

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0x942fD5017c0F60575930D8574Eaca13BEcD6e1bB',
  : '0xdeadbeef',
})

Using a Paymaster Client

import { ,  } from './config'
import {  } from 'viem'
import {  } from 'viem/utils'
import {  } from 'viem/erc4337'
const  = .({
  : ('https://api.pimlico.io/v2/1/rpc?apikey={API_KEY}')
})
 
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : ,
})

Using the Bundler Client as Paymaster

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : true,
})

paymasterContext

  • Type: unknown

Paymaster specific fields.

import { ,  } from './config'
import {  } from 'viem'
import {  } from 'viem/utils'
import {  } from 'viem/erc4337'
const  = .({
  : ('https://api.pimlico.io/v2/1/rpc?apikey={API_KEY}')
})
 
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : ,
  : {
    : 'abc123'
  },
})

paymasterData

  • Type: Address

Call data to execute on the Paymaster contract.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0x942fD5017c0F60575930D8574Eaca13BEcD6e1bB',
  : '0xdeadbeef',
})

paymasterPostOpGasLimit

  • Type: bigint

The amount of gas to allocate for the Paymaster post-operation code.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0x942fD5017c0F60575930D8574Eaca13BEcD6e1bB',
  : '0xdeadbeef',
  : 69420n,
})

paymasterSignature

  • Type: Hex

EntryPoint 0.9 Paymaster signature. It is preserved separately from paymasterData and excluded from the account-signed User Operation hash.

paymasterVerificationGasLimit

  • Type: bigint

The amount of gas to allocate for the Paymaster validation code.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0x942fD5017c0F60575930D8574Eaca13BEcD6e1bB',
  : '0xdeadbeef',
  : 69420n,
})

preVerificationGas

  • Type: bigint

Extra gas to pay the Bundler.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : 69420n,
})

signature

  • Type: Hex

Signature for the User Operation.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : '0x...',
})

stateOverride

The state override set is an optional address-to-state mapping, where each entry specifies some state to be ephemerally overridden prior to executing the call.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : {
    '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC': {
      : .('1'),
      : {
        '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0': '0x00000000000000000000000000000000000000000000000000000000000001a4',
      },
    },
  },
})

verificationGasLimit

  • Type: bigint

The amount of gas to allocate for the verification step.

import { ,  } from './config'
import {  } from 'viem/utils'
const  = await ..({
  ,
  : [{
    : '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
    : .('1')
  }],
  : 69420n,
})