Skip to main content
Skill — Copyable reference implementation. Use as-is or customize. See Skills Philosophy.

Viem-style PublicClient

This Skill demonstrates how to build a viem-compatible PublicClient using Voltaire primitives. The implementation follows viem’s architecture patterns while leveraging Voltaire’s type-safe primitives.

Overview

The PublicClient provides access to public Ethereum JSON-RPC methods like fetching blocks, transactions, balances, and making calls. Key features:
  • Transport abstraction - HTTP, WebSocket, or custom transports
  • Extend pattern - Composable client extension for custom actions
  • Type safety - Full TypeScript types for all methods
  • Caching - Built-in request caching with configurable TTL

Quick Start

Architecture

Client Structure

Transport Layer

The transport layer handles JSON-RPC communication:

Extend Pattern

Extend the client with custom actions:
Chain extensions:

API Reference

createPublicClient

Creates a PublicClient with public actions.
Parameters:

Public Actions

getBlockNumber

Returns the current block number.

getBalance

Returns the balance of an address.

getBlock

Returns block information.

call

Executes a call without creating a transaction.

estimateGas

Estimates gas for a transaction.

getTransaction

Returns transaction by hash.

getTransactionReceipt

Returns transaction receipt.

getLogs

Returns logs matching filter.

getCode

Returns contract bytecode.

getStorageAt

Returns storage at slot.

getTransactionCount

Returns transaction count (nonce).

getChainId

Returns chain ID.

getGasPrice

Returns current gas price.

Chain Definitions

Pre-configured chain definitions:
Create custom chains:

Error Handling

The client throws typed errors:

Testing

Run the test suite:

Implementation Notes

Caching

Block number is cached to reduce RPC calls:

Retry Logic

HTTP transport automatically retries on network errors (not RPC errors):

Polling Interval

Polling interval is derived from chain block time:
  • Mainnet (12s blocks): 4000ms polling
  • Arbitrum (250ms blocks): 500ms polling

File Structure

Next Steps

Future enhancements:
  1. readContract - ABI-aware contract reads
  2. multicall - Batched contract calls
  3. watchBlockNumber - Block number subscription
  4. watchBlocks - Block subscription
  5. WebSocket transport - Real-time subscriptions
  6. ENS support - Name resolution