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

Viem Contract Abstraction

A copyable implementation of viem’s contract abstraction patterns using Voltaire primitives.

Overview

This Skill provides a complete viem-compatible contract abstraction including:
  • getContract - Factory returning typed contract instance with proxied methods
  • readContract - Read view/pure functions via eth_call
  • writeContract - Execute state-changing functions via eth_sendTransaction
  • simulateContract - Dry-run write functions to validate and get return values
  • estimateContractGas - Estimate gas for write operations
  • watchContractEvent - Subscribe to contract events via polling

Quick Start

Using getContract

Standalone Actions

readContract

Call view/pure functions without needing a contract instance:

writeContract

Execute state-changing functions:

simulateContract

Validate a write operation before executing:

estimateContractGas

Estimate gas before writing:

watchContractEvent

Subscribe to contract events:

Type Inference

The abstraction provides full type inference from your ABI:

Error Handling

All actions wrap errors in specific error classes:

Split Clients

You can provide separate public and wallet clients:

Files

Copy these files into your codebase:
  • getContract.js - Main factory function
  • readContract.js - Read action
  • writeContract.js - Write action
  • simulateContract.js - Simulation action
  • estimateContractGas.js - Gas estimation action
  • watchContractEvent.js - Event watching action
  • ViemContractTypes.ts - TypeScript type definitions
  • errors.ts - Custom error classes
  • index.ts - Module exports

Differences from Viem

This implementation:
  1. Uses Voltaire’s Abi primitive for encoding/decoding
  2. Uses polling-only event watching (no WebSocket support)
  3. Does not include createContractEventFilter or getContractEvents
  4. Simplified parameter handling
For production use, consider adding:
  • WebSocket subscription support for events
  • Filter-based event watching
  • Request batching
  • Retry logic for RPC calls