Function Signature

createViemPublicClient(params: {
  chain: Chain;
  rpcUrl: string;
}): PublicClient

Description

Creates a viem public client for interacting with EVM blockchains. This client is used for read-only operations like getting balances, account information, and preparing transactions.

Parameters

Required Parameters

  • chain (Chain) - The viem chain configuration
  • rpcUrl (string) - The RPC URL for the blockchain network

Returns

  • PublicClient - A viem public client instance

Example

import { authenticatedEvmClient } from './client';
import { base } from 'viem/chains';

const evmClient = await authenticatedEvmClient();

const publicClient = evmClient.createViemPublicClient({
  chain: base,
  rpcUrl: 'https://mainnet.base.org',
});

// Use the public client for read operations
const balance = await publicClient.getBalance({
  address: '0xYourWalletAddress' as `0x${string}`,
});

console.log('Balance:', balance.toString());

Common Use Cases

Get Account Balance

const balance = await publicClient.getBalance({
  address: '0xYourWalletAddress' as `0x${string}`,
});

Prepare Transaction

const preparedTx = await publicClient.prepareTransactionRequest({
  to: '0xRecipientAddress' as `0x${string}`,
  value: parseEther('0.1'),
  chain: base,
  account: '0xYourWalletAddress' as `0x${string}`,
});

Get Account Information

const accountInfo = await publicClient.getAccount({
  address: '0xYourWalletAddress' as `0x${string}`,
});

Error Handling

try {
  const publicClient = evmClient.createViemPublicClient({
    chain: base,
    rpcUrl: 'https://mainnet.base.org',
  });
  console.log('Public client created successfully');
} catch (error) {
  console.error('Failed to create public client:', error);
}