Public Client

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

You can read more about Public Client here.

In comp-kit, you are provided with publicClient for each chain you pass into supportedChains props to KitProvider. The reason why publicClient is different for each chain unlike walletClient is mentioned here.

Usage

In order to use publicClient, import usePublicClient from "@nazeeh2000/comp-kit". The return value of this hook will be a Record containing the publicClient corresponding to the network name.

You can use publicClient in your component as per the below example.

'use client'
import { usePublicClient } from "@nazeeh2000/comp-kit";
import { mainnet } from "viem/chains";

export default function Home() {
    const publicClient = usePublicClient();
    // publicClient for mainnet
    const mainnetPublicClient = publicClient[mainnet.name]

    return (
    // Your component
    )
}

You can apply all the methods to the mainnetPublicClient that are applicable to the publicClient of viem mentioned here.

Note: You need to add use client at the top of the component accessing these hooks as these hooks cant be run on the server side because they've React Context in the background.

Last updated