Clients

Factory functions for creating Viem clients. These are thin wrappers around Viem’s createPublicClient and createWalletClient that integrate with the built-in chain definitions.

import { createClient, createWalletClient } from '@web3marketlabs/sdk'

createClient(options)

Creates a Viem PublicClient for reading blockchain state. The client is configured with the chain definition from the built-in chain registry.

function createClient(options: CreateClientOptions): PublicClient<Transport, Chain>

CreateClientOptions

ParameterTypeDescription
chainId*numberThe numeric chain ID to connect to. Must be a supported chain (see Chains page).
Example: 1
rpcUrlstringOptional custom RPC URL. If omitted, uses the chain's default public RPC endpoint.
Example: https://eth-mainnet.g.alchemy.com/v2/...

Returns

A Viem PublicClient<Transport, Chain> configured with the resolved chain and HTTP transport.

Throws

Throws an Error if the chainId is not a supported chain in the registry:

Unknown chain ID: 999999. Use a known chain or provide a custom rpcUrl.

Examples

import { createClient } from '@web3marketlabs/sdk'
 
// Using default RPC
const client = createClient({ chainId: 1 })
const balance = await client.getBalance({ address: '0x...' })
 
// Using custom RPC URL
const alchemyClient = createClient({
  chainId: 1,
  rpcUrl: 'https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY',
})
 
// Reading contract data
const client = createClient({ chainId: 137 })
const result = await client.readContract({
  address: '0x...',
  abi: myAbi,
  functionName: 'balanceOf',
  args: ['0x...'],
})

createWalletClient(options)

Creates a Viem WalletClient for signing and sending transactions.

function createWalletClient(options: CreateWalletClientOptions): WalletClient<Transport, Chain, Account>

CreateWalletClientOptions

ParameterTypeDescription
chainId*numberThe numeric chain ID to connect to. Must be a supported chain.
Example: 1
account*AccountA Viem Account object (from privateKeyToAccount, mnemonicToAccount, or a custom account implementation).
rpcUrlstringOptional custom RPC URL. If omitted, uses the chain's default public RPC endpoint.

Returns

A Viem WalletClient<Transport, Chain, Account> configured with the resolved chain, HTTP transport, and the provided account.

Throws

Throws an Error if the chainId is not a supported chain in the registry.

Examples

import { createWalletClient } from '@web3marketlabs/sdk'
import { privateKeyToAccount } from 'viem/accounts'
 
const account = privateKeyToAccount('0x...')
const wallet = createWalletClient({ chainId: 1, account })
 
// Send a transaction
const hash = await wallet.sendTransaction({
  to: '0x...',
  value: 1000000000000000000n, // 1 ETH
})
 
// With custom RPC
const wallet = createWalletClient({
  chainId: 11155111,
  account,
  rpcUrl: 'https://sepolia.infura.io/v3/YOUR_KEY',
})
⚠️

Never hardcode private keys in your application code. Use environment variables or a secure key management solution.

Options Interfaces

interface CreateClientOptions {
  /** The numeric chain ID to connect to */
  chainId: number
  /** Optional custom RPC URL. Falls back to the chain's default public RPC. */
  rpcUrl?: string
}
 
interface CreateWalletClientOptions extends CreateClientOptions {
  /** The account (address or local account) to use for signing */
  account: Account
}

Internal Behavior

Both factory functions:

  1. Resolve the chainId to a ChainConfig using getChain().
  2. Convert the ChainConfig to a Viem-compatible Chain object.
  3. Create an HTTP transport with the provided rpcUrl (or the chain’s default).
  4. Return the configured Viem client.

Source

Defined in @web3marketlabs/sdksrc/client.ts.