Documentation Index
Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
Use this file to discover all available pages before exploring further.
Viem is one of the most popular libraries for interacting with EVM blockchains.
A PublicClient is an interface to “public” JSON-RPC API methods
such as retrieving block numbers, transactions, reading from smart contracts, etc.
We provide a helper method to allow you to create a Viem PublicClient given some network data.
Usage
import { getActiveNetworkData } from '@dynamic-labs-sdk/client';
import { createPublicClientFromNetworkData } from '@dynamic-labs-sdk/evm/viem';
const someAction = async (walletAccount) => {
// you can use your custom NetworkData object or get the active network data from a wallet account
// with the getActiveNetworkData function
const { networkData } = await getActiveNetworkData({ walletAccount });
const publicClient = createPublicClientFromNetworkData({ networkData });
// PublicClient is now ready to use
};
Create the public client inside an effect or callback — it is derived from network data, which may change when the user switches networks:import { getActiveNetworkData } from '@dynamic-labs-sdk/client';
import { createPublicClientFromNetworkData } from '@dynamic-labs-sdk/evm/viem';
import { useEffect, useState } from 'react';
function useEvmPublicClient(walletAccount) {
const [publicClient, setPublicClient] = useState(null);
useEffect(() => {
if (!walletAccount) return;
getActiveNetworkData({ walletAccount }).then(({ networkData }) => {
setPublicClient(createPublicClientFromNetworkData({ networkData }));
});
}, [walletAccount]);
return publicClient;
}