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
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
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:
- Resolve the
chainIdto aChainConfigusinggetChain(). - Convert the
ChainConfigto a Viem-compatibleChainobject. - Create an HTTP transport with the provided
rpcUrl(or the chain’s default). - Return the configured Viem client.
Source
Defined in @web3marketlabs/sdk — src/client.ts.