Skip to content

Bundler Client

A function to create a Bundler Client.

A Bundler Client is an interface to interact with ERC-4337 Bundlers and provides the ability to send and retrieve User Operations through Bundler Actions.

Import

import { createBundlerClient } from 'viem/account-abstraction'

Usage

import { createPublicClient, http } from 'viem'
import { createBundlerClient } from 'viem/account-abstraction'
import { mainnet } from 'viem/chains'
 
const client = createPublicClient({
  chain: mainnet,
  transport: http()
})
 
const bundlerClient = createBundlerClient({ 
  client, 
  transport: http('https://public.pimlico.io/v2/1/rpc') 
}) 

Parameters

account (optional)

  • Type: SmartAccount

The Smart Account to use for the Bundler Client. This will be used for Actions that require an account as an argument.

import { toCoinbaseSmartAccount } from 'viem/account-abstraction'
import { privateKeyToAccount } from 'viem/accounts'
 
const owner = privateKeyToAccount('0x...')
 
const account = await toCoinbaseSmartAccount({ 
  client, 
  owners: [owner] 
}) 
 
const bundlerClient = createBundlerClient({
  account, 
  client,
  transport: http('https://public.pimlico.io/v2/1/rpc'),
})

chain (optional)

The Chain of the Bundler Client.

import { mainnet } from 'viem/chains' 
 
const bundlerClient = createBundlerClient({
  chain: mainnet, 
  transport: http('https://public.pimlico.io/v2/1/rpc')
})

client (optional)

  • Type: Client

The Client (pointing to execution RPC) of the Bundler Client.

import { createPublicClient, http } from 'viem' 
import { mainnet } from 'viem/chains'
 
const client = createPublicClient({ 
  chain: mainnet, 
  transport: http() 
}) 
 
const bundlerClient = createBundlerClient({
  client, 
  transport: http('https://public.pimlico.io/v2/1/rpc')
})

key (optional)

  • Type: string
  • Default: "bundler"

A key for the Client.

const client = createBundlerClient({
  key: 'foo', 
  transport: http('https://public.pimlico.io/v2/1/rpc')
})

name (optional)

  • Type: string
  • Default: "Bundler Client"

A name for the Client.

const client = createBundlerClient({
  name: 'Foo Bundler Client', 
  transport: http('https://public.pimlico.io/v2/1/rpc')
})

paymaster (optional)

  • Type: true | PaymasterClient | { getPaymasterData: typeof getPaymasterData, getPaymasterStubData: typeof getPaymasterStubData }

Sets Paymaster configuration for the Bundler Client to be utilized on User Operations.

  • If paymaster: PaymasterClient, it will use the provided Paymaster Client for User Operation 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 User Operation sponsorship.
  • If custom functions are provided to paymaster, it will use them for User Operation sponsorship.

Using a Paymaster Client

const paymasterClient = createPaymasterClient({ 
  transport: http('https://public.pimlico.io/v2/11155111/rpc') 
}) 
 
const bundlerClient = createBundlerClient({
  chain: mainnet,
  paymaster: paymasterClient, 
  transport: http('https://public.pimlico.io/v2/1/rpc'),
})

Using the Bundler Client as Paymaster

const bundlerClient = createBundlerClient({
  chain: mainnet,
  paymaster: true, 
  transport: http('https://public.pimlico.io/v2/1/rpc'),
})

Using Custom Paymaster Functions

See the properties below for more information on how to use custom Paymaster functions.

paymaster.getPaymasterData (optional)

  • Type: (userOperation: UserOperation) => Promise<GetPaymasterDataReturnType>

Retrieves paymaster-related User Operation properties to be used for sending the User Operation.

Read more

const bundlerClient = createBundlerClient({
  chain: mainnet,
  paymaster: { 
    async getPaymasterData(userOperation) { 
      // Retrieve paymaster properties for the User Operation.
      return { 
        paymaster: '0x...', 
        paymasterData: '0x...', 
        paymasterVerificationGasLimit: 69420n, 
        paymasterPostOpGasLimit: 69420n, 
      } 
    } 
  } 
  transport: http('https://public.pimlico.io/v2/1/rpc'),
})

paymaster.getPaymasterStubData (optional)

  • Type: (userOperation: UserOperation) => Promise<GetPaymasterStubDataReturnType>

Retrieves paymaster-related User Operation properties to be used for gas estimation.

Read more

const bundlerClient = createBundlerClient({
  chain: mainnet,
  paymaster: { 
    async getPaymasterStubData(userOperation) { 
      // Retrieve paymaster properties for the User Operation.
      return { 
        paymaster: '0x...', 
        paymasterData: '0x...', 
        paymasterVerificationGasLimit: 69420n, 
        paymasterPostOpGasLimit: 69420n, 
      } 
    } 
    async getPaymasterData(userOperation) { /* ... */ }
  } 
  transport: http('https://public.pimlico.io/v2/1/rpc'),
})

paymasterContext (optional)

  • Type: unknown

Paymaster specific fields.

const paymasterClient = createPaymasterClient({
  transport: http('https://public.pimlico.io/v2/1/rpc')
})
 
const bundlerClient = createBundlerClient({
  chain: mainnet,
  paymaster: paymasterClient,
  paymasterContext: { 
    policyId: 'abc123'
  }, 
  transport: http('https://public.pimlico.io/v2/1/rpc'),
})

pollingInterval (optional)

  • Type: number
  • Default: 4_000

Frequency (in ms) for polling enabled Actions.

const client = createBundlerClient({
  pollingInterval: 10_000, 
  transport: http('https://public.pimlico.io/v2/1/rpc')
})

rpcSchema (optional)

  • Type: RpcSchema
  • Default: BundlerRpcSchema

Typed JSON-RPC schema for the client.

import { rpcSchema } from 'viem'
 
type CustomRpcSchema = [{ 
  Method: 'eth_wagmi', 
  Parameters: [string] 
  ReturnType: string
}] 
 
const client = createBundlerClient({
  rpcSchema: rpcSchema<CustomRpcSchema>(), 
  transport: http('https://public.pimlico.io/v2/1/rpc')
})
 
const result = await client.request({ 
  method: 'eth_wa
eth_wagmi
params: ['hello'], })

transport

  • Type: Transport

The Transport of the Bundler Client.

const bundlerClient = createBundlerClient({
  chain: mainnet,
  transport: http('https://public.pimlico.io/v2/1/rpc'), 
})

userOperation (optional)

Configuration for User Operations.

userOperation.estimateFeesPerGas

  • Type: ({ account: Account, bundlerClient: Client, userOperation: UserOperationRequest }) => Promise<{ maxFeePerGas: bigint, maxPriorityFeePerGas: bigint }>

Prepares fee properties for the User Operation request.

const bundlerClient = createBundlerClient({
  chain: mainnet,
  transport: http('https://public.pimlico.io/v2/1/rpc'),
  userOperation: { 
    async estimateFeesPerGas({ account, bundlerClient, userOperation }) { 
      // Estimate fees per gas for the User Operation.
      return { 
        maxFeePerGas: /* ... */, 
        maxPriorityFeePerGas: /* ... */, 
      } 
    } 
  } 
})