Skip to content
On this page

Public Client โ€‹

A Public Client is an interface to "public" JSON-RPC API methods such as retrieving block numbers, transactions, reading from smart contracts, etc through Public Actions.

The createPublicClient function sets up a Public Client with a given Transport configured for a Chain.

Import โ€‹

ts
import { createPublicClient } from 'viem'

Usage โ€‹

Initialize a Client with your desired Chain (e.g. mainnet) and Transport (e.g. http).

ts
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'

const client = createPublicClient({ 
  chain: mainnet,
  transport: http()
})

Then you can consume Public Actions:

ts
const blockNumber = await client.getBlockNumber() 

Optimization โ€‹

The Public Client also supports eth_call Aggregation and JSON-RPC Batching (soon) for improved performance.

eth_call Aggregation (via Multicall) โ€‹

The Public Client supports the aggregation of eth_call requests into a single multicall (aggregate3) request.

This means for every Action that utilizes an eth_call request (ie. readContract), the Public Client will batch the requests (over a timed period) and send it to the RPC Provider in a single multicall request. This can dramatically improve network performance, and decrease the amount of Compute Units (CU) used by RPC Providers like Alchemy, Infura, etc.

The Public Client schedules the aggregation of eth_call requests over a given time period. By default, it executes the batch request at the end of the current JavaScript message queue (a zero delay), however, consumers can specify a custom wait period (in ms).

You can enable eth_call aggregation by setting the batch.multicall flag to true:

ts
const client = createPublicClient({
  batch: {
    multicall: true, 
  },
  chain: mainnet,
  transport: http(),
})

You can also customize the multicall options.

Now, when you start to utilize readContract Actions, the Public Client will batch and send over those requests at the end of the message queue (or custom time period) in a single eth_call multicall request:

ts
const contract = getContract({ address, abi })

// The below will send a single request to the RPC Provider.
const [name, totalSupply, symbol, tokenUri, balance] = await Promise.all([
  contract.read.name(),
  contract.read.totalSupply(),
  contract.read.symbol(),
  contract.read.tokenURI([420n]),
  contract.read.balanceOf([address]),
])

Read more on Contract Instances.

JSON-RPC Batching โ€‹

The Public Client will support JSON-RPC Batching. This is coming soon.

Parameters โ€‹

transport โ€‹

The Transport of the Public Client.

ts
const client = createPublicClient({
  chain: mainnet,
  transport: http(), 
})

chain (optional) โ€‹

The Chain of the Public Client.

ts
const client = createPublicClient({
  chain: mainnet, 
  transport: http(),
})

batch (optional) โ€‹

Flags for batch settings.

batch.multicall (optional) โ€‹

  • Type: boolean | MulticallBatchOptions
  • Default: false

Toggle to enable eth_call multicall aggregation.

ts
const client = createPublicClient({
  batch: {
    multicall: true, 
  },
  chain: mainnet,
  transport: http(),
})

batch.multicall.batchSize (optional) โ€‹

  • Type: number
  • Default: 1_024

The maximum size (in bytes) for each multicall (aggregate3) calldata chunk.

Note: Some RPC Providers limit the amount of calldata that can be sent in a single request. It is best to check with your RPC Provider to see if there are any calldata size limits to eth_call requests.

ts
const client = createPublicClient({
  batch: {
    multicall: {
      batchSize: 512, 
    },
  },
  chain: mainnet,
  transport: http(),
})

batch.multicall.wait (optional) โ€‹

The maximum number of milliseconds to wait before sending a batch.

ts
const client = createPublicClient({
  batch: {
    multicall: {
      wait: 16, 
    },
  },
  chain: mainnet,
  transport: http(),
})

key (optional) โ€‹

  • Type: string
  • Default: "public"

A key for the Client.

ts
const client = createPublicClient({
  chain: mainnet,
  key: 'public', 
  transport: http(),
})

name (optional) โ€‹

  • Type: string
  • Default: "Public Client"

A name for the Client.

ts
const client = createPublicClient({
  chain: mainnet,
  name: 'Public Client', 
  transport: http(),
})

pollingInterval (optional) โ€‹

  • Type: number
  • Default: 4_000

Frequency (in ms) for polling enabled Actions.

ts
const client = createPublicClient({
  chain: mainnet,
  pollingInterval: 10_000, 
  transport: http(),
})

Live Example โ€‹

Check out the usage of createPublicClient in the live Public Client Example below.

Released under the MIT License.